Skip to content

Commit 70519ed

Browse files
matzclaude
authored 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 7ee1e61 commit 70519ed

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
@@ -19816,6 +19816,18 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, u
1981619816
}
1981719817
}
1981819818

19819+
/* If a missing terminator left this heredoc's lex mode on the
19820+
* stack, it still points at our stack-local common_whitespace.
19821+
* Clear the pointer so that subsequent lexing cannot read from
19822+
* this function's dead stack frame. */
19823+
pm_lex_mode_t *whitespace_mode = parser->lex_modes.current;
19824+
do {
19825+
if (whitespace_mode->mode == PM_LEX_HEREDOC && whitespace_mode->as.heredoc.common_whitespace == &common_whitespace) {
19826+
whitespace_mode->as.heredoc.common_whitespace = NULL;
19827+
}
19828+
whitespace_mode = whitespace_mode->prev;
19829+
} while (whitespace_mode != NULL);
19830+
1981919831
if (match1(parser, PM_TOKEN_STRING_BEGIN)) {
1982019832
return parse_strings(parser, node, false, (uint16_t) (depth + 1));
1982119833
}

0 commit comments

Comments
 (0)