Repository navigation
fix(parser): fail on deep nesting instead of overflowing the stack - #5675
hatemosphere wants to merge 3 commits into
Conversation
The parser is recursive descent, so deeply nested input such as Function('('.repeat(N)) overflows the native stack of the parsing thread and aborts the process (boa-dev#4397). Cursor::peek now returns a "too much recursion" syntax error once less than 512 KiB of the thread's stack is left. The stack bounds come from pthread_getattr_np, so the guard is Linux-only for now; elsewhere it never trips.
|
Nice work tackling #4397! Bounding stack recursion via early fail-fast syntax errors without growing the stack is a great approach for hostile input. A couple of observations regarding stack bounds handling:
|
- Red zone is now min(128 KiB, stack / 4). A fixed 512 KiB put the limit above the stack top on stacks <= 512 KiB (musl's 128 KiB default), so even "1" failed. Measured worst case between two checks: ~32 KiB debug, ~16 KiB release. - musl's pthread_getattr_np reports only the mapped part of the main thread's stack; use RLIMIT_STACK there.
Bounds from pthread_get_stackaddr_np/pthread_get_stacksize_np and GetCurrentThreadStackLimits (not on win7 targets, which lack it). The test now runs on every CI OS.
Fixes #4397.
Cursor::peekreturns a "too much recursion" SyntaxError when less than min(128 KiB, stack / 4) of the thread's stack is left, instead of overflowing and aborting.pthread_getattr_npon Linux (RLIMIT_STACKfor the musl main thread),pthread_get_stackaddr_np/pthread_get_stacksize_npon Apple,GetCurrentThreadStackLimitson Windows. Other targets are unchanged.