Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed a stack overflow abort on the reported case of about 100–200 nested `function` bodies, including that nesting inside a one-line Luau interpolated string or split by a `\z` string that contains the word `end`. Valid input formats. The same nesting with the closing `end`s missing reports a parse error instead of aborting. `format_code` switches to one fixed 16MiB stack when the caller has less than that free (about 8MiB covers the release build of the reported range; debug formatting needs the rest). A few thousand nested blocks can still overflow ([#1189](https://github.com/JohnnyMorganz/StyLua/issues/1189))

## [2.6.0] - 2026-10-10

### Added
Expand Down
91 changes: 85 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ lsp-textdocument = { version = "0.4.2", optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2.108", optional = true }

# Non-wasm `format_code` switches to a fixed stack segment when the caller
# does not already have enough room for nested blocks. wasm has no portable
# stack switch; the host supplies that call stack.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
stacker = "0.1.25"

# full-moon should be compiled to release optimisations in debug mode due to stack overflows
# https://github.com/Kampfkarren/full-moon/issues/140
[profile.dev.package.full_moon]
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod context;
#[cfg(feature = "editorconfig")]
pub mod editorconfig;
mod formatters;
mod recursion;
mod shape;
mod sort_requires;
mod verify_ast;
Expand Down Expand Up @@ -457,6 +458,19 @@ pub fn format_code(
config: Config,
range: Option<Range>,
verify_output: OutputVerification,
) -> Result<String, Error> {
// Parsing and formatting recurse once per nested block, and the parser
// keeps a large frame live across that recursion. A 2MiB worker thread
// aborts around 100 nested functions; give the call a fixed segment when
// the caller does not already have that much stack.
recursion::with_sufficient_stack(|| format_code_impl(code, config, range, verify_output))
}

fn format_code_impl(
code: &str,
config: Config,
range: Option<Range>,
verify_output: OutputVerification,
) -> Result<String, Error> {
let input_ast = match full_moon::parse_fallible(code, config.syntax.into()).into_result() {
Ok(ast) => ast,
Expand Down
58 changes: 58 additions & 0 deletions src/recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Stack space for recursive parsing and formatting.
//!
//! `full_moon`'s `parse_stmt` keeps one large frame live while it parses a
//! nested block (about 23KiB per nested function on an optimized build with
//! every syntax feature). The CLI calls [`crate::format_code`] on a
//! `threadpool` worker whose stack is Rust's default 2MiB, so about 100 nested
//! functions abort the process. Debug builds of the formatter use several
//! times that much per level.
//!
//! [`with_sufficient_stack`] switches to one fixed segment when the caller has
//! less than that left. Release parsing of the reported 100–200 nested
//! functions needs only a few megabytes. The segment is 16MiB so a debug
//! build of the formatter — several times the release frame size — can also
//! run the 160-level regression without aborting. It does not grow with the
//! input, so a few thousand nested blocks can still overflow. Shallow calls
//! on a thread that already has this much free stay put.

/// Stack reserved for one `format_code` call.
const REQUIRED_STACK: usize = 16 * 1024 * 1024;

/// Run `f` with at least [`REQUIRED_STACK`] bytes of stack available.
pub(crate) fn with_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
#[cfg(not(target_arch = "wasm32"))]
{
let enough =
matches!(stacker::remaining_stack(), Some(remaining) if remaining >= REQUIRED_STACK);
if enough {
f()
} else {
stacker::grow(REQUIRED_STACK, f)
}
}
#[cfg(target_arch = "wasm32")]
{
f()
}
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
#[test]
fn worker_thread_has_less_than_the_reserved_stack() {
// The CLI pool uses this size. If the probe over-reports it, the
// guard skips the switch and the overflow comes back.
let remaining = std::thread::Builder::new()
.stack_size(2 * 1024 * 1024)
.spawn(stacker::remaining_stack)
.unwrap()
.join()
.unwrap();
let remaining = remaining.expect("stack bounds should be visible");
assert!(
remaining < super::REQUIRED_STACK,
"2MiB thread reported {} bytes free",
remaining
);
}
}
Loading