From 58417523b4d5612430a3ea424251de5f59bf1fb4 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sat, 14 Nov 2026 13:23:09 -0500 Subject: [PATCH 1/7] Ensure char width does need read beyond end in single byte encodings --- src/encoding.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/encoding.c b/src/encoding.c index 4f061b395e..4d1e7cd53c 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -1537,7 +1537,7 @@ pm_encoding_char_width(const pm_encoding_t *encoding, const uint8_t *b, ptrdiff_ case PM_ENCODING_KIND_ASCII: return ((n > 0) && (*b < 0x80)) ? 1 : 0; case PM_ENCODING_KIND_SINGLE_BYTE: - return 1; + return n > 0 ? 1 : 0; case PM_ENCODING_KIND_EUC_JP: return pm_encoding_euc_jp_char_width(b, n); case PM_ENCODING_KIND_SHIFT_JIS: From 917bf3170188dce46d831defde2c4a182f122fa5 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sat, 14 Nov 2026 13:31:11 -0500 Subject: [PATCH 2/7] Fix signed overflow in line number arithmetic --- include/prism/internal/line_offset_list.h | 10 ++++++++++ src/line_offset_list.c | 8 ++++---- src/prism.c | 2 +- test/cpp/parser_test.cpp | 16 ++++++++++++++++ test/prism/result/warnings_test.rb | 19 +++++++++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/include/prism/internal/line_offset_list.h b/include/prism/internal/line_offset_list.h index 440055330e..deea0d1e1b 100644 --- a/include/prism/internal/line_offset_list.h +++ b/include/prism/internal/line_offset_list.h @@ -17,6 +17,16 @@ void pm_line_offset_list_clear(pm_line_offset_list_t *list); /* Append a new offset to the list (slow path with resize). */ void pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor); +/* Add the zero-based line index to the starting line with defined 32-bit + * wraparound. Keep both the addition and conversion free of signed overflow. */ +static PRISM_FORCE_INLINE int32_t +pm_line_offset_list_line_number(size_t index, int32_t start_line) { + uint32_t line = (uint32_t) index + (uint32_t) start_line; + if (line <= INT32_MAX) return (int32_t) line; + + return INT32_MIN + (int32_t) (line - ((uint32_t) INT32_MAX + 1)); +} + /* Append a new offset to the list. */ static PRISM_FORCE_INLINE void pm_line_offset_list_append(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) { diff --git a/src/line_offset_list.c b/src/line_offset_list.c index 035851e049..d3d6984224 100644 --- a/src/line_offset_list.c +++ b/src/line_offset_list.c @@ -55,7 +55,7 @@ pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int size_t mid = left + (right - left) / 2; if (list->offsets[mid] == cursor) { - return ((int32_t) mid) + start_line; + return pm_line_offset_list_line_number(mid, start_line); } if (list->offsets[mid] < cursor) { @@ -65,7 +65,7 @@ pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int } } - return ((int32_t) left) + start_line - 1; + return pm_line_offset_list_line_number(left - 1, start_line); } /** @@ -82,7 +82,7 @@ pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t curs size_t mid = left + (right - left) / 2; if (list->offsets[mid] == cursor) { - return ((pm_line_column_t) { ((int32_t) mid) + start_line, 0 }); + return ((pm_line_column_t) { pm_line_offset_list_line_number(mid, start_line), 0 }); } if (list->offsets[mid] < cursor) { @@ -93,7 +93,7 @@ pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t curs } return ((pm_line_column_t) { - .line = ((int32_t) left) + start_line - 1, + .line = pm_line_offset_list_line_number(left - 1, start_line), .column = cursor - list->offsets[left - 1] }); } diff --git a/src/prism.c b/src/prism.c index b0baad56a5..f146206ee1 100644 --- a/src/prism.c +++ b/src/prism.c @@ -15219,7 +15219,7 @@ parser_warn_indentation_mismatch(pm_parser_t *parser, size_t opening_newline_ind (const char *) closing_token->start, (int) (opening_token->end - opening_token->start), (const char *) opening_token->start, - ((int32_t) opening_newline_index) + parser->start_line + pm_line_offset_list_line_number(opening_newline_index, parser->start_line) ); } diff --git a/test/cpp/parser_test.cpp b/test/cpp/parser_test.cpp index 31b5d8d1ae..cc42d9bfe6 100644 --- a/test/cpp/parser_test.cpp +++ b/test/cpp/parser_test.cpp @@ -65,6 +65,22 @@ PRISM_TEST(line_column_locates_an_offset) { CHECK(second.line == 2 && second.column == 2); } +PRISM_TEST(line_column_wraps_at_the_line_number_boundary) { + prism_test::parse_result result("a\nb\nc"); + const pm_line_offset_list_t *offsets = pm_parser_line_offsets(result.parser.get()); + + for (int32_t start_line : { INT32_MIN, INT32_MAX }) { + for (uint32_t cursor = 0; cursor <= 5; cursor++) { + int64_t expected = static_cast(start_line) + cursor / 2; + if (expected > INT32_MAX) expected -= INT64_C(4294967296); + + pm_line_column_t position = pm_line_offset_list_line_column(offsets, cursor, start_line); + CHECK(position.line == expected); + CHECK(position.column == cursor % 2); + } + } +} + PRISM_TEST(errors_are_all_visited) { std::string_view source = "def (; 1 +; ["; prism_test::parse_result result(source); diff --git a/test/prism/result/warnings_test.rb b/test/prism/result/warnings_test.rb index 4bfca20fb6..d4c123f83f 100644 --- a/test/prism/result/warnings_test.rb +++ b/test/prism/result/warnings_test.rb @@ -95,6 +95,25 @@ def test_duplicated_when_clause assert_warning("case 1; when 1, 1; end", "when' clause") end + def test_line_number_overflow + result = Prism.parse("{\n__LINE__ => 1, -2147483648 => 2\n}", line: 2**31 - 1) + warning = result.warnings.find { |item| item.type == :duplicated_hash_key } + assert_not_nil warning + assert_equal "key -2147483648 is duplicated and overwritten on line -2147483648", warning.message + + result = Prism.parse("\nif true\n end", line: 2**31 - 1) + warning = result.warnings.find { |item| item.type == :indentation_mismatch } + assert_not_nil warning + assert_equal "mismatched indentations at 'end' with 'if' at -2147483648", warning.message + end + + def test_line_number_at_maximum_without_overflow + result = Prism.parse("{__LINE__ => 1, 2147483647 => 2}", line: 2**31 - 1) + warning = result.warnings.find { |item| item.type == :duplicated_hash_key } + assert_not_nil warning + assert_equal "key 2147483647 is duplicated and overwritten on line 2147483647", warning.message + end + # Negative zero and positive zero are the same key: Float#eql? treats them # as equal and Float#hash gives them the same hash, so the second one # overwrites the first. From aec0cd0c6a3098cc32b8c2ffc09ba4c8632c0025 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sat, 14 Nov 2026 13:36:37 -0500 Subject: [PATCH 3/7] Update node locations in the same place --- include/prism/internal/node.h | 13 +++++ src/prism.c | 69 ++++++----------------- test/prism/result/source_location_test.rb | 17 ++++++ 3 files changed, 46 insertions(+), 53 deletions(-) diff --git a/include/prism/internal/node.h b/include/prism/internal/node.h index 54bc96cb43..6647210f68 100644 --- a/include/prism/internal/node.h +++ b/include/prism/internal/node.h @@ -13,6 +13,19 @@ /* The offset just past the end of the given location. */ #define PM_LOCATION_END(location_) ((location_)->start + (location_)->length) +/* Return the span covering two initialized locations. Read both endpoints + * before changing either field, so moving the start preserves the old end. + * An unset location must be initialized separately from the first child. */ +static PRISM_FORCE_INLINE pm_location_t +pm_location_join(pm_location_t left, pm_location_t right) { + uint32_t start = left.start < right.start ? left.start : right.start; + uint32_t left_end = PM_LOCATION_END(&left); + uint32_t right_end = PM_LOCATION_END(&right); + uint32_t end = left_end > right_end ? left_end : right_end; + pm_location_t location = { .start = start, .length = end - start }; + return location; +} + /* * Slow path for pm_node_list_append_inline: grow the list and append the node. * Do not call directly — use pm_node_list_append_inline instead. diff --git a/src/prism.c b/src/prism.c index f146206ee1..2f7afd78bb 100644 --- a/src/prism.c +++ b/src/prism.c @@ -4967,12 +4967,7 @@ pm_interpolated_regular_expression_node_create(pm_parser_t *parser, const pm_tok static PRISM_INLINE void pm_interpolated_regular_expression_node_append(pm_arena_t *arena, pm_interpolated_regular_expression_node_t *node, pm_node_t *part) { - if (PM_NODE_START(node) > PM_NODE_START(part)) { - PM_NODE_START_SET_NODE(node, part); - } - if (PM_NODE_END(node) < PM_NODE_END(part)) { - PM_NODE_LENGTH_SET_NODE(node, part); - } + node->base.location = pm_location_join(node->base.location, part->location); pm_interpolated_node_append(arena, UP(node), &node->parts, part); } @@ -5016,13 +5011,9 @@ pm_interpolated_string_node_append(pm_parser_t *parser, pm_interpolated_string_n #define MUTABLE_FLAGS(node) \ node->base.flags = (pm_node_flags_t) ((FL(node) | PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE) & ~PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN); - if (node->parts.size == 0 && node->opening_loc.length == 0) { - PM_NODE_START_SET_NODE(node, part); - } - - if (PM_NODE_END(part) > PM_NODE_END(node)) { - PM_NODE_LENGTH_SET_NODE(node, part); - } + node->base.location = node->parts.size == 0 && node->opening_loc.length == 0 + ? part->location + : pm_location_join(node->base.location, part->location); switch (PM_NODE_TYPE(part)) { case PM_STRING_NODE: @@ -5154,15 +5145,11 @@ pm_interpolated_string_node_closing_set(const pm_parser_t *parser, pm_interpolat static void pm_interpolated_symbol_node_append(pm_arena_t *arena, pm_interpolated_symbol_node_t *node, pm_node_t *part) { - if (node->parts.size == 0 && node->opening_loc.length == 0) { - PM_NODE_START_SET_NODE(node, part); - } + node->base.location = node->parts.size == 0 && node->opening_loc.length == 0 + ? part->location + : pm_location_join(node->base.location, part->location); pm_interpolated_node_append(arena, UP(node), &node->parts, part); - - if (PM_NODE_END(part) > PM_NODE_END(node)) { - PM_NODE_LENGTH_SET_NODE(node, part); - } } static void @@ -5641,13 +5628,9 @@ pm_multi_target_node_targets_append(pm_parser_t *parser, pm_multi_target_node_t pm_node_list_append_inline(parser->arena, &node->rights, target); } - if (PM_NODE_LENGTH(node) == 0 || (PM_NODE_START(node) > PM_NODE_START(target))) { - PM_NODE_START_SET_NODE(node, target); - } - - if (PM_NODE_LENGTH(node) == 0 || (PM_NODE_END(node) < PM_NODE_END(target))) { - PM_NODE_LENGTH_SET_NODE(node, target); - } + node->base.location = PM_NODE_LENGTH(node) == 0 + ? target->location + : pm_location_join(node->base.location, target->location); } /** @@ -5885,13 +5868,9 @@ pm_parameters_node_create(pm_parser_t *parser) { */ static void pm_parameters_node_location_set(pm_parameters_node_t *params, pm_node_t *param) { - if ((params->base.location.length == 0) || PM_NODE_START(params) > PM_NODE_START(param)) { - PM_NODE_START_SET_NODE(params, param); - } - - if ((params->base.location.length == 0) || (PM_NODE_END(params) < PM_NODE_END(param))) { - PM_NODE_LENGTH_SET_NODE(params, param); - } + params->base.location = params->base.location.length == 0 + ? param->location + : pm_location_join(params->base.location, param->location); } /** @@ -6425,13 +6404,9 @@ pm_statements_node_body_length(pm_statements_node_t *node) { */ static PRISM_INLINE void pm_statements_node_body_update(pm_statements_node_t *node, pm_node_t *statement) { - if (pm_statements_node_body_length(node) == 0 || PM_NODE_START(statement) < PM_NODE_START(node)) { - PM_NODE_START_SET_NODE(node, statement); - } - - if (PM_NODE_END(statement) > PM_NODE_END(node)) { - PM_NODE_LENGTH_SET_NODE(node, statement); - } + node->base.location = pm_statements_node_body_length(node) == 0 + ? statement->location + : pm_location_join(node->base.location, statement->location); } /** @@ -20342,7 +20317,6 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) { break; } case PM_TOKEN_EMBVAR: { - bool start_location_set = false; if (current == NULL) { /* If we hit an embedded variable and the current node is * NULL, then this is the start of a new string. We'll set @@ -20357,8 +20331,6 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) { current = UP(pm_symbol_node_to_string_node(parser, (pm_symbol_node_t *) current)); pm_interpolated_symbol_node_append(parser->arena, interpolated, current); - PM_NODE_START_SET_NODE(interpolated, current); - start_location_set = true; current = UP(interpolated); } else { /* If we hit an embedded variable and the current node is an @@ -20368,13 +20340,9 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) { pm_node_t *part = parse_string_part(parser, (uint16_t) (depth + 1)); pm_interpolated_symbol_node_append(parser->arena, (pm_interpolated_symbol_node_t *) current, part); - if (!start_location_set) { - PM_NODE_START_SET_NODE(current, part); - } break; } case PM_TOKEN_EMBEXPR_BEGIN: { - bool start_location_set = false; if (current == NULL) { /* If we hit an embedded expression and the current node is * NULL, then this is the start of a new string. We'll set @@ -20389,8 +20357,6 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) { current = UP(pm_symbol_node_to_string_node(parser, (pm_symbol_node_t *) current)); pm_interpolated_symbol_node_append(parser->arena, interpolated, current); - PM_NODE_START_SET_NODE(interpolated, current); - start_location_set = true; current = UP(interpolated); } else if (PM_NODE_TYPE_P(current, PM_INTERPOLATED_SYMBOL_NODE)) { /* If we hit an embedded expression and the current node is @@ -20401,9 +20367,6 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) { pm_node_t *part = parse_string_part(parser, (uint16_t) (depth + 1)); pm_interpolated_symbol_node_append(parser->arena, (pm_interpolated_symbol_node_t *) current, part); - if (!start_location_set) { - PM_NODE_START_SET_NODE(current, part); - } break; } default: diff --git a/test/prism/result/source_location_test.rb b/test/prism/result/source_location_test.rb index 493ce7729c..3220c7c275 100644 --- a/test/prism/result/source_location_test.rb +++ b/test/prism/result/source_location_test.rb @@ -531,6 +531,23 @@ def test_InterpolatedSymbolNode assert_location(InterpolatedSymbolNode, ':"#{foo}bar"') end + def test_interpolated_symbol_array_locations + ['#{1}#{2}', '#@a#@b', '#{1}#@a', '#@a#{1}', 'a#{1}#{2}', '#{1}a#{2}'].each do |element| + source = "%I[#{element} second]" + result = Prism.parse(source) + assert_predicate result, :success? + + array = result.value.statements.body.first + symbol, second = array.elements + assert_kind_of InterpolatedSymbolNode, symbol + assert_equal 3, symbol.location.start_offset + assert_equal 3 + element.bytesize, symbol.location.end_offset + assert_equal element, symbol.location.slice + assert_equal "second", second.location.slice + assert_equal source, array.location.slice + end + end + def test_InterpolatedXStringNode assert_location(InterpolatedXStringNode, '`foo #{bar} baz`') end From a3f74c31ad681c69102ab9043c10a7e1d3000011 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sat, 14 Nov 2026 13:51:37 -0500 Subject: [PATCH 4/7] Fix heredoc ownership and token restoration during error recovery --- src/prism.c | 41 +++++++++++---- test/prism/errors/heredoc_unterminated.txt | 2 - test/prism/heredoc_recovery_test.rb | 61 ++++++++++++++++++++++ 3 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 test/prism/heredoc_recovery_test.rb diff --git a/src/prism.c b/src/prism.c index 2f7afd78bb..40ea33591c 100644 --- a/src/prism.c +++ b/src/prism.c @@ -12993,6 +12993,21 @@ expect1_opening(pm_parser_t *parser, pm_token_type_t type, pm_diagnostic_id_t di static pm_node_t * parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, uint8_t flags, pm_diagnostic_id_t diag_id, uint16_t depth); +static pm_node_t * +parse_heredoc(pm_parser_t *parser, uint16_t depth); + +/* Discard an unexpected token without abandoning lexer state owned by it. + * A heredoc opener has already pushed its lex mode and scheduled a source + * jump, so its body must be consumed before resuming on the declaration line. */ +static void +parse_discard_token(pm_parser_t *parser, uint16_t depth) { + if (match1(parser, PM_TOKEN_HEREDOC_START)) { + (void) parse_heredoc(parser, depth); + } else { + parser_lex(parser); + } +} + /** * This is a wrapper of parse_expression, which also checks whether the * resulting node is a value expression. @@ -13804,7 +13819,7 @@ parse_statements(pm_parser_t *parser, pm_context_t context, uint16_t depth) { // token and continue parsing the statements list. Otherwise we'll add // an error and continue parsing the statements list. if (PM_NODE_TYPE_P(node, PM_ERROR_RECOVERY_NODE)) { - parser_lex(parser); + parse_discard_token(parser, depth); // If we are at the end of the file, then we need to stop parsing // the statements entirely at this point. Mark the parser as @@ -16419,7 +16434,7 @@ parse_string_part(pm_parser_t *parser, uint16_t depth) { return UP(pm_embedded_variable_node_create(parser, &operator, variable)); } default: - parser_lex(parser); + parse_discard_token(parser, depth); pm_parser_err_previous(parser, PM_ERR_CANNOT_PARSE_STRING_PART); return NULL; } @@ -17611,7 +17626,7 @@ parse_pattern_primitive(pm_parser_t *parser, pm_constant_id_set_t *captures, pm_ break; default: { PM_PARSER_ERR_TOKEN_FORMAT(parser, &parser->current, PM_ERR_PATTERN_HASH_KEY, pm_token_str(parser->current.type)); - parser_lex(parser); + parse_discard_token(parser, depth); first_node = UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->previous), PM_TOKEN_LENGTH(&parser->previous))); break; @@ -19596,10 +19611,9 @@ parse_heredoc(pm_parser_t *parser, uint16_t depth) { whitespace_mode = whitespace_mode->prev; } while (whitespace_mode != NULL); - if (match1(parser, PM_TOKEN_STRING_BEGIN)) { - return parse_strings(parser, node, false, (uint16_t) (depth + 1)); - } - + /* The body and terminator are out of source order. The caller resumes + * after the opener, so missing tokens must be anchored there as well. */ + parser->previous = opening; return node; } @@ -20371,7 +20385,7 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) { } default: expect1(parser, PM_TOKEN_STRING_CONTENT, PM_ERR_LIST_I_UPPER_ELEMENT); - parser_lex(parser); + parse_discard_token(parser, depth); break; } } @@ -20557,7 +20571,7 @@ parse_interpolated_string_array(pm_parser_t *parser, uint16_t depth) { } default: expect1(parser, PM_TOKEN_STRING_CONTENT, PM_ERR_LIST_W_UPPER_ELEMENT); - parser_lex(parser); + parse_discard_token(parser, depth); break; } } @@ -21041,8 +21055,13 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, u case PM_TOKEN_IDENTIFIER: case PM_TOKEN_METHOD_NAME: return parse_identifier(parser, binding_power, flags, depth); - case PM_TOKEN_HEREDOC_START: - return parse_heredoc(parser, depth); + case PM_TOKEN_HEREDOC_START: { + pm_node_t *node = parse_heredoc(parser, depth); + if (match1(parser, PM_TOKEN_STRING_BEGIN)) { + return parse_strings(parser, node, false, (uint16_t) (depth + 1)); + } + return node; + } case PM_TOKEN_CLASS_VARIABLE: parser_lex(parser); return parse_multi_target(parser, UP(pm_class_variable_read_node_create(parser, &parser->previous)), binding_power, depth); diff --git a/test/prism/errors/heredoc_unterminated.txt b/test/prism/errors/heredoc_unterminated.txt index 56bd162998..514218fb97 100644 --- a/test/prism/errors/heredoc_unterminated.txt +++ b/test/prism/errors/heredoc_unterminated.txt @@ -4,6 +4,4 @@ a=>{< Date: Mon, 16 Nov 2026 09:59:35 -0500 Subject: [PATCH 5/7] Fix heredoc recovery at EOF and parser resource limits --- config.yml | 4 +- src/prism.c | 72 ++++++++++++++----- .../unterminated_heredoc_and_embexpr.txt | 8 +-- test/prism/heredoc_recovery_test.rb | 19 +++++ 4 files changed, 76 insertions(+), 27 deletions(-) diff --git a/config.yml b/config.yml index 198eb3f597..b0ad271a11 100644 --- a/config.yml +++ b/config.yml @@ -339,6 +339,8 @@ tokens: comment: "," - name: EMBEXPR_END comment: "}" + - name: HEREDOC_END + comment: "the end of a heredoc" - name: KEYWORD_DO comment: "do" - name: KEYWORD_ELSE @@ -464,8 +466,6 @@ tokens: comment: ">>" - name: GREATER_GREATER_EQUAL comment: ">>=" - - name: HEREDOC_END - comment: "the end of a heredoc" - name: HEREDOC_START comment: "the start of a heredoc" - name: IDENTIFIER diff --git a/src/prism.c b/src/prism.c index 40ea33591c..80ab17997c 100644 --- a/src/prism.c +++ b/src/prism.c @@ -7853,7 +7853,7 @@ static const uint32_t context_terminators[] = { [PM_CONTEXT_DEFAULT_PARAMS] = (1U << PM_TOKEN_COMMA) | (1U << PM_TOKEN_PARENTHESIS_RIGHT), [PM_CONTEXT_ELSE] = (1U << PM_TOKEN_KEYWORD_END), [PM_CONTEXT_ELSIF] = (1U << PM_TOKEN_KEYWORD_ELSE) | (1U << PM_TOKEN_KEYWORD_ELSIF) | (1U << PM_TOKEN_KEYWORD_END), - [PM_CONTEXT_EMBEXPR] = (1U << PM_TOKEN_EMBEXPR_END), + [PM_CONTEXT_EMBEXPR] = (1U << PM_TOKEN_EMBEXPR_END) | (1U << PM_TOKEN_HEREDOC_END), [PM_CONTEXT_FOR] = (1U << PM_TOKEN_KEYWORD_END), [PM_CONTEXT_FOR_INDEX] = (1U << PM_TOKEN_KEYWORD_IN), [PM_CONTEXT_IF] = (1U << PM_TOKEN_KEYWORD_ELSE) | (1U << PM_TOKEN_KEYWORD_ELSIF) | (1U << PM_TOKEN_KEYWORD_END), @@ -9745,12 +9745,13 @@ lex_embdoc(pm_parser_t *parser) { // Now, loop until we find the end of the embedded documentation or the end // of the file. - while (parser->current.end + 4 <= parser->end) { + while (parser->current.end < parser->end) { parser->current.start = parser->current.end; // If we've hit the end of the embedded documentation then we'll return // that token here. if ( + (parser->end - parser->current.end >= 4) && (memcmp(parser->current.end, "=end", 4) == 0) && ( (parser->current.end + 4 == parser->end) || // end of file @@ -10146,6 +10147,7 @@ parser_lex(pm_parser_t *parser) { // We'll check if we're at the end of the file. If we are, then we // need to return the EOF token. if (parser->current.end >= parser->end) { + lex_eof: // We may be missing closing tokens. We should pop modes one by one // to do the appropriate cleanup like moving next_start for heredocs. // Only when no mode is remaining will we actually emit the EOF token. @@ -10661,7 +10663,8 @@ parser_lex(pm_parser_t *parser) { ) { pm_token_type_t type = lex_embdoc(parser); if (type == PM_TOKEN_EOF) { - LEX(type); + parser->current.start = parser->current.end; + goto lex_eof; } goto lex_next_token; @@ -11439,7 +11442,8 @@ parser_lex(pm_parser_t *parser) { parser->data_loc.start = PM_TOKEN_START(parser, &parser->current); parser->data_loc.length = PM_TOKEN_LENGTH(&parser->current); - LEX(PM_TOKEN_EOF); + parser->current.start = parser->current.end; + goto lex_eof; } pm_lex_state_t last_state = parser->lex_state; @@ -12935,6 +12939,7 @@ expect2(pm_parser_t *parser, pm_token_type_t type1, pm_token_type_t type2, pm_di static void expect1_heredoc_term(pm_parser_t *parser, const uint8_t *ident_start, size_t ident_length) { if (match1(parser, PM_TOKEN_HEREDOC_END)) { + lex_state_set(parser, PM_LEX_STATE_END); parser_lex(parser); } else { pm_parser_err_heredoc_term(parser, ident_start, ident_length); @@ -12996,6 +13001,44 @@ parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, uint8_t static pm_node_t * parse_heredoc(pm_parser_t *parser, uint16_t depth); +/* Resource failure must preserve the same token/context ownership as ordinary + * syntax recovery. In particular, do not consume an enclosing terminator or + * leave an unparsed heredoc's lexer mode pending. */ +static pm_node_t * +parse_resource_recovery(pm_parser_t *parser, uint16_t depth) { + if (depth >= PRISM_DEPTH_MAXIMUM) { + pm_parser_err_current(parser, PM_ERR_NESTING_TOO_DEEP); + } else { + pm_parser_err_stack_exhausted(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current)); + } + + if (context_recoverable(parser, &parser->current)) { + parser->recovering = true; + } + + if (match1(parser, PM_TOKEN_HEREDOC_START)) { + pm_token_t opening = parser->current; + PRISM_ASSERT(parser->lex_modes.current->mode == PM_LEX_HEREDOC); + + /* Discard the heredoc without recursing. */ + parser->lex_modes.current->as.heredoc.base.quote = PM_HEREDOC_QUOTE_SINGLE; + + do { + parser_lex(parser); + } while (!match2(parser, PM_TOKEN_HEREDOC_END, PM_TOKEN_EOF)); + + if (match1(parser, PM_TOKEN_HEREDOC_END)) { + lex_state_set(parser, PM_LEX_STATE_END); + parser_lex(parser); + } + + parser->previous = opening; + return UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &opening), PM_TOKEN_LENGTH(&opening))); + } + + return UP(pm_error_recovery_node_create(parser, PM_TOKEN_END(parser, &parser->previous), 0)); +} + /* Discard an unexpected token without abandoning lexer state owned by it. * A heredoc opener has already pushed its lex mode and scheduled a source * jump, so its body must be consumed before resuming on the declaration line. */ @@ -13051,6 +13094,7 @@ token_begins_expression_p(pm_token_type_t type) { case PM_TOKEN_COMMA: case PM_TOKEN_EMBEXPR_END: case PM_TOKEN_EOF: + case PM_TOKEN_HEREDOC_END: case PM_TOKEN_LAMBDA_BEGIN: case PM_TOKEN_KEYWORD_DO: case PM_TOKEN_KEYWORD_DO_BLOCK: @@ -17932,13 +17976,7 @@ parse_pattern(pm_parser_t *parser, pm_constant_id_set_t *captures, uint8_t flags /* Nested patterns recurse back into parse_pattern without passing through * parse_expression, so this cycle needs its own depth check. */ if (PRISM_UNLIKELY(depth >= PRISM_DEPTH_MAXIMUM || pm_parser_stack_exhausted(parser))) { - if (depth >= PRISM_DEPTH_MAXIMUM) { - pm_parser_err_current(parser, PM_ERR_NESTING_TOO_DEEP); - } else { - pm_parser_err_stack_exhausted(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current)); - } - - return UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + return parse_resource_recovery(parser, depth); } pm_node_t *node = NULL; @@ -19495,6 +19533,10 @@ parse_hash(pm_parser_t *parser, uint16_t depth) { static pm_node_t * parse_heredoc(pm_parser_t *parser, uint16_t depth) { + if (PRISM_UNLIKELY(depth >= PRISM_DEPTH_MAXIMUM || pm_parser_stack_exhausted(parser))) { + return parse_resource_recovery(parser, depth); + } + PRISM_ASSERT(parser->lex_modes.current->mode == PM_LEX_HEREDOC); pm_heredoc_lex_mode_t lex_mode = parser->lex_modes.current->as.heredoc.base; @@ -22898,13 +22940,7 @@ parse_expression_terminator(pm_parser_t *parser, pm_node_t *node) { static pm_node_t * parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, uint8_t flags, pm_diagnostic_id_t diag_id, uint16_t depth) { if (PRISM_UNLIKELY(depth >= PRISM_DEPTH_MAXIMUM || pm_parser_stack_exhausted(parser))) { - if (depth >= PRISM_DEPTH_MAXIMUM) { - pm_parser_err_current(parser, PM_ERR_NESTING_TOO_DEEP); - } else { - pm_parser_err_stack_exhausted(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current)); - } - - return UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + return parse_resource_recovery(parser, depth); } pm_node_t *node = parse_expression_prefix(parser, binding_power, flags, diag_id, depth); diff --git a/test/prism/errors/unterminated_heredoc_and_embexpr.txt b/test/prism/errors/unterminated_heredoc_and_embexpr.txt index bed7fcd24e..1d45f8ca74 100644 --- a/test/prism/errors/unterminated_heredoc_and_embexpr.txt +++ b/test/prism/errors/unterminated_heredoc_and_embexpr.txt @@ -1,11 +1,5 @@ < Date: Mon, 16 Nov 2026 10:43:20 -0500 Subject: [PATCH 6/7] Fix up error recovery node bounds --- src/prism.c | 12 ++++++------ test/prism/result/source_location_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/prism.c b/src/prism.c index 80ab17997c..dafbb49bae 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16471,7 +16471,7 @@ parse_string_part(pm_parser_t *parser, uint16_t depth) { // missing node. default: expect1(parser, PM_TOKEN_IDENTIFIER, PM_ERR_EMBVAR_INVALID); - variable = UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + variable = UP(pm_error_recovery_node_create(parser, PM_TOKEN_END(parser, &parser->previous), 0)); break; } @@ -16680,7 +16680,7 @@ parse_undef_argument(pm_parser_t *parser, uint16_t depth) { return parse_symbol(parser, PM_LEX_STATE_NONE, (uint16_t) (depth + 1)); default: pm_parser_err_current(parser, PM_ERR_UNDEF_ARGUMENT); - return UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + return UP(pm_error_recovery_node_create(parser, PM_TOKEN_END(parser, &parser->previous), 0)); } } @@ -16721,7 +16721,7 @@ parse_alias_argument(pm_parser_t *parser, bool first, uint16_t depth) { return UP(pm_global_variable_read_node_create(parser, &parser->previous)); default: pm_parser_err_current(parser, PM_ERR_ALIAS_ARGUMENT); - return UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + return UP(pm_error_recovery_node_create(parser, PM_TOKEN_END(parser, &parser->previous), 0)); } } @@ -17820,7 +17820,7 @@ parse_pattern_primitive(pm_parser_t *parser, pm_constant_id_set_t *captures, pm_ } default: pm_parser_err_current(parser, diag_id); - return UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + return UP(pm_error_recovery_node_create(parser, PM_TOKEN_END(parser, &parser->previous), 0)); } } @@ -17926,7 +17926,7 @@ parse_pattern_primitives(pm_parser_t *parser, pm_constant_id_set_t *captures, pm } default: { pm_parser_err_current(parser, diag_id); - pm_node_t *right = UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + pm_node_t *right = UP(pm_error_recovery_node_create(parser, PM_TOKEN_END(parser, &parser->previous), 0)); if (!alternation) { node = right; @@ -20726,7 +20726,7 @@ parse_not(pm_parser_t *parser, pm_binding_power_t binding_power, uint8_t flags, pm_parser_err_current(parser, PM_ERR_EXPECT_LPAREN_AFTER_NOT_OTHER); } - return UP(pm_error_recovery_node_create(parser, PM_TOKEN_START(parser, &parser->current), PM_TOKEN_LENGTH(&parser->current))); + return UP(pm_error_recovery_node_create(parser, PM_TOKEN_END(parser, &parser->previous), 0)); } accept1(parser, PM_TOKEN_NEWLINE); diff --git a/test/prism/result/source_location_test.rb b/test/prism/result/source_location_test.rb index 3220c7c275..f8faa0eac9 100644 --- a/test/prism/result/source_location_test.rb +++ b/test/prism/result/source_location_test.rb @@ -49,6 +49,29 @@ def test_AssocNode assert_location(AssocNode, "foo(bar: :baz)", 4...13) { |node| node.arguments.arguments.first.elements.first } end + def test_unconsumed_error_recovery_locations + ["{not %", "{not ", "{not \x01", "alias ", "undef ", "case x; in [ ", "case x; in a | "].each do |source| + result = Prism.parse(source) + assert_predicate result, :failure? + + nodes = [result.value] + until nodes.empty? + node = nodes.pop + location = node.location + assert_operator location.start_offset, :>=, 0, source.inspect + assert_operator location.end_offset, :<=, source.bytesize, source.inspect + assert_operator location.length, :>=, 0, source.inspect + nodes.concat(node.compact_child_nodes) + end + end + + assoc = Prism.parse_statement("{not %").receiver.elements.first + assert_equal 4, assoc.location.start_offset + assert_equal 0, assoc.location.length + assert_equal 4, assoc.key.location.start_offset + assert_equal 0, assoc.key.location.length + end + def test_AssocSplatNode assert_location(AssocSplatNode, "{ **foo }", 2...7) { |node| node.elements.first } assert_location(AssocSplatNode, "foo(**bar)", 4...9) { |node| node.arguments.arguments.first.elements.first } From 93ea7aec020a3c0533ff1abb9770501350f589ce Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 11:02:30 -0500 Subject: [PATCH 7/7] Bump template CI version --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2c9ea5782f..08498f38a3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -33,7 +33,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: "2.7.0" + ruby-version: "2.7" bundler-cache: true - name: Generate templates run: bundle exec rake templates