Skip to content

Commit 9bd2370

Browse files
matzclaude
andcommitted
Fix stack-use-after-return with unterminated heredocs
The heredoc handler in parse_expression_prefix stores the address of its stack-local common_whitespace into the heredoc lex mode. When the heredoc terminator is missing, expect1_heredoc_term reports an error without popping the lex mode, so the stored pointer outlives the stack frame and subsequent lexing reads dead stack memory. Clear the pointer before leaving the handler; the lexer already guards against NULL. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 880833d commit 9bd2370

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/prism.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19712,6 +19712,18 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, u
1971219712
}
1971319713
}
1971419714

19715+
// If a missing terminator left this heredoc's lex mode on the
19716+
// stack, it still points at our stack-local common_whitespace.
19717+
// Clear the pointer so that subsequent lexing cannot read from
19718+
// this function's dead stack frame.
19719+
pm_lex_mode_t *whitespace_mode = parser->lex_modes.current;
19720+
do {
19721+
if (whitespace_mode->mode == PM_LEX_HEREDOC && whitespace_mode->as.heredoc.common_whitespace == &common_whitespace) {
19722+
whitespace_mode->as.heredoc.common_whitespace = NULL;
19723+
}
19724+
whitespace_mode = whitespace_mode->prev;
19725+
} while (whitespace_mode != NULL);
19726+
1971519727
if (match1(parser, PM_TOKEN_STRING_BEGIN)) {
1971619728
return parse_strings(parser, node, false, (uint16_t) (depth + 1));
1971719729
}

0 commit comments

Comments
 (0)