Skip to content
Merged
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
132 changes: 67 additions & 65 deletions src/errors_format.c

Large diffs are not rendered by default.

125 changes: 79 additions & 46 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -7625,8 +7625,6 @@ parser_magic_comment_key_p(const uint8_t *key, size_t key_length, const char *na
*/
static PRISM_INLINE bool
parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
bool result = true;

const uint8_t *start = parser->current.start + 1;
const uint8_t *end = parser->current.end;
if (end - start <= 7) return false;
Expand All @@ -7645,24 +7643,23 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
// have a magic comment.
return false;
}
} else {
// Non-emacs magic comments must contain a colon for `key: value`.
// Reject early if there is no colon to avoid scanning the entire
// comment character-by-character.
if (pm_memchr(start, ':', (size_t) (end - start), parser->encoding_changed, parser->encoding) == NULL) {
}

if (!indicator) {
/* The encoding-comment line needs the full scan because its result
* controls the fallback encoding scan. Other comments without a colon
* cannot contain a magic directive, so reject them early. */
if ((parser->current.start != parser->encoding_comment_start || parser->encoding_locked) &&
pm_memchr(start, ':', (size_t) (end - start), parser->encoding_changed, parser->encoding) == NULL) {
return false;
}

// Advance start past leading whitespace so the main loop begins
// directly at the key, avoiding a redundant whitespace scan.
start += pm_strspn_whitespace(start, end - start);
}

cursor = start;
while (cursor < end) {
if (indicator) {
while (cursor < end && (pm_char_is_magic_comment_key_delimiter(*cursor) || pm_char_is_whitespace(*cursor))) cursor++;
}
while (cursor < end && (pm_char_is_magic_comment_key_delimiter(*cursor) || pm_char_is_whitespace(*cursor))) cursor++;

const uint8_t *key_start = cursor;
while (cursor < end && (!pm_char_is_magic_comment_key_delimiter(*cursor) && !pm_char_is_whitespace(*cursor))) cursor++;
Expand Down Expand Up @@ -7711,22 +7708,15 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {

switch (key_length) {
case 6:
/* We only want to attempt to compare against encoding comments
* if it is the first line in the file (or the second in the
* case of a shebang). */
if (parser->current.start == parser->encoding_comment_start && !parser->encoding_locked) {
if (parser_magic_comment_key_p(key_start, key_length, "coding")) {
result = parser_lex_magic_comment_encoding_value(parser, value_start, value_end);
}
}
break;
case 8:
/* We only want to attempt to compare against encoding comments
* if it is the first line in the file (or the second in the
* case of a shebang). */
if (parser->current.start == parser->encoding_comment_start && !parser->encoding_locked) {
if (parser_magic_comment_key_p(key_start, key_length, "encoding")) {
result = parser_lex_magic_comment_encoding_value(parser, value_start, value_end);
if (parser_magic_comment_key_p(key_start, key_length, key_length == 6 ? "coding" : "encoding")) {
if (!parser_lex_magic_comment_encoding_value(parser, value_start, value_end)) {
pm_parser_err(parser, U32(value_start - parser->start), value_length, PM_ERR_INVALID_ENCODING_MAGIC_COMMENT);
}
}
}
break;
Expand Down Expand Up @@ -7820,7 +7810,7 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
pm_list_append(&parser->magic_comment_list, (pm_list_node_t *) magic_comment);
}

return result;
return true;
}

/******************************************************************************/
Expand Down Expand Up @@ -8726,11 +8716,20 @@ lex_identifier(pm_parser_t *parser, bool previous_command_start) {

/**
* Returns true if the current token that the parser is considering is at the
* beginning of a line or the beginning of the source.
* beginning of a line or the beginning of the source, after an optional BOM.
*/
static bool
current_token_starts_line(pm_parser_t *parser) {
return (parser->current.start == parser->start) || (parser->current.start[-1] == '\n');
if (parser->current.start == parser->start) return true;

switch (parser->current.start[-1]) {
case '\n':
return true;
case 0xbf:
return (parser->current.start - parser->start == 3 && parser->start[0] == 0xef && parser->start[1] == 0xbb);
default:
return false;
}
}

/**
Expand Down Expand Up @@ -9361,7 +9360,7 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, uint8_t flags) {
// ?\u{nnnn} character literal should contain only one codepoint
// and cannot be like ?\u{nnnn mmmm}.
if (flags & PM_ESCAPE_FLAG_SINGLE && codepoints_count > 1) {
pm_parser_err(parser, U32(extra_codepoints_start - parser->start), U32(parser->current.end - 1 - extra_codepoints_start), PM_ERR_ESCAPE_INVALID_UNICODE_LITERAL);
pm_parser_err(parser, U32(extra_codepoints_start - parser->start), U32(parser->current.end - extra_codepoints_start), PM_ERR_ESCAPE_INVALID_UNICODE_LITERAL);
}

if (parser->current.end == parser->end) {
Expand Down Expand Up @@ -10661,7 +10660,12 @@ parser_lex(pm_parser_t *parser) {
current_token_starts_line(parser) &&
(parser->current.end + 5 <= parser->end) &&
memcmp(parser->current.end, "begin", 5) == 0 &&
(pm_char_is_whitespace(peek_offset(parser, 5)) || (peek_offset(parser, 5) == '\0'))
(
pm_char_is_whitespace(peek_offset(parser, 5)) ||
(peek_offset(parser, 5) == '\0') ||
(peek_offset(parser, 5) == '\004') || /* Ctrl-D */
(peek_offset(parser, 5) == '\032') /* Ctrl-Z */
)
) {
pm_token_type_t type = lex_embdoc(parser);
if (type == PM_TOKEN_EOF) {
Expand Down Expand Up @@ -12372,11 +12376,13 @@ parser_lex(pm_parser_t *parser) {
uint8_t breakpoints[PM_STRPBRK_CACHE_SIZE] = "\r\n\\#";

pm_heredoc_quote_t quote = heredoc_lex_mode->quote;
/* Single-quoted heredocs preserve bytes without validating them. */
bool validate = quote != PM_HEREDOC_QUOTE_SINGLE;
if (quote == PM_HEREDOC_QUOTE_SINGLE) {
breakpoints[3] = '\0';
}

const uint8_t *breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
const uint8_t *breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, validate);
pm_token_buffer_t token_buffer = { 0 };
bool was_line_continuation = false;

Expand All @@ -12385,13 +12391,13 @@ parser_lex(pm_parser_t *parser) {
case '\0':
// Skip directly past the null character.
parser->current.end = breakpoint + 1;
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, validate);
break;
case '\r':
parser->current.end = breakpoint + 1;

if (peek_at(parser, breakpoint + 1) != '\n') {
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, validate);
break;
}

Expand Down Expand Up @@ -12482,7 +12488,7 @@ parser_lex(pm_parser_t *parser) {
// Otherwise we hit a newline and it wasn't followed by
// a terminator, so we can continue parsing.
parser->current.end = breakpoint + 1;
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, validate);
break;
}
case '\\': {
Expand Down Expand Up @@ -12589,7 +12595,7 @@ parser_lex(pm_parser_t *parser) {
}

token_buffer.cursor = parser->current.end;
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, validate);
break;
}
case '#': {
Expand All @@ -12601,7 +12607,7 @@ parser_lex(pm_parser_t *parser) {
// or instance variable like "#@" but wasn't
// actually. In this case we'll just skip to the
// next breakpoint.
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, validate);
break;
}

Expand Down Expand Up @@ -14610,10 +14616,14 @@ parse_arguments(pm_parser_t *parser, pm_arguments_t *arguments, bool accepts_for
* end
*
*
* It can recurse infinitely down, and splats are allowed to group arguments.
* Destructuring can be nested, and splats are allowed to group arguments.
*/
static pm_multi_target_node_t *
parse_required_destructured_parameter(pm_parser_t *parser) {
static pm_node_t *
parse_required_destructured_parameter(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);
}

expect1(parser, PM_TOKEN_PARENTHESIS_LEFT_GROUPING, PM_ERR_EXPECT_LPAREN_REQ_PARAMETER);

pm_multi_target_node_t *node = pm_multi_target_node_create(parser);
Expand All @@ -14634,7 +14644,7 @@ parse_required_destructured_parameter(pm_parser_t *parser) {
}

if (match1(parser, PM_TOKEN_PARENTHESIS_LEFT_GROUPING)) {
param = UP(parse_required_destructured_parameter(parser));
param = parse_required_destructured_parameter(parser, (uint16_t) (depth + 1));
} else if (accept1(parser, PM_TOKEN_USTAR)) {
pm_token_t star = parser->previous;
pm_node_t *value = NULL;
Expand Down Expand Up @@ -14667,7 +14677,7 @@ parse_required_destructured_parameter(pm_parser_t *parser) {
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_EXPECT_RPAREN_REQ_PARAMETER);
pm_multi_target_node_closing_set(parser, node, &parser->previous);

return node;
return UP(node);
}

/**
Expand Down Expand Up @@ -14804,7 +14814,7 @@ parse_parameters(
switch (parser->current.type) {
case PM_TOKEN_PARENTHESIS_LEFT_GROUPING: {
update_parameter_state(parser, &parser->current, &order);
pm_node_t *param = UP(parse_required_destructured_parameter(parser));
pm_node_t *param = parse_required_destructured_parameter(parser, (uint16_t) (depth + 1));

if (order > PM_PARAMETERS_ORDER_AFTER_OPTIONAL) {
pm_parameters_node_requireds_append(parser->arena, params, param);
Expand Down Expand Up @@ -20308,6 +20318,30 @@ parse_uninterpolated_symbol_array(pm_parser_t *parser) {
return array;
}

/**
* Validate completed static symbols before adding them to a symbol array.
* Interpolated symbols are validated at runtime instead.
*/
static void
parse_interpolated_symbol_array_element(pm_parser_t *parser, pm_array_node_t *array, pm_node_t *element) {
if (PM_NODE_TYPE_P(element, PM_SYMBOL_NODE) && !PM_NODE_FLAG_P(element, PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING)) {
const pm_symbol_node_t *symbol = (const pm_symbol_node_t *) element;
pm_token_t content = {
.type = PM_TOKEN_STRING_CONTENT,
.start = parser->start + symbol->content_loc.start,
.end = parser->start + PM_LOCATION_END(&symbol->content_loc)
};

if (PM_NODE_FLAG_P(element, PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING)) {
parse_symbol_encoding_validate_utf8(parser, &content, &symbol->unescaped);
} else {
parse_symbol_encoding_validate_other(parser, &content, &symbol->unescaped);
}
}

pm_array_node_elements_append(parser->arena, array, element);
}

static pm_array_node_t *
parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) {
parser_lex(parser);
Expand All @@ -20332,7 +20366,7 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) {
/* If we hit a separator after we've hit content, then we
* need to append that content to the list and reset the
* current node. */
pm_array_node_elements_append(parser->arena, array, current);
parse_interpolated_symbol_array_element(parser, array, current);
current = NULL;
}

Expand Down Expand Up @@ -20444,7 +20478,7 @@ parse_interpolated_symbol_array(pm_parser_t *parser, uint16_t depth) {

/* If we have a current node, then we need to append it to the list. */
if (current) {
pm_array_node_elements_append(parser->arena, array, current);
parse_interpolated_symbol_array_element(parser, array, current);
}

pm_token_t closing = parser->current;
Expand Down Expand Up @@ -23505,13 +23539,12 @@ pm_parser_init(pm_arena_t *arena, pm_parser_t *parser, const uint8_t *source, si
const uint8_t *newline = next_newline(parser->current.end, parser->end - parser->current.end);
size_t length = (size_t) ((newline != NULL ? newline : parser->end) - parser->current.end);

if (length > 2 && parser->current.end[0] == '#' && parser->current.end[1] == '!') {
if (length >= 2 && parser->current.end[0] == '#' && parser->current.end[1] == '!') {
parser->encoding_comment_start = newline != NULL ? newline + 1 : parser->end;
const char *engine;

if ((engine = pm_strnstr((const char *) parser->start, "ruby", length)) != NULL) {
if (newline != NULL) {
parser->encoding_comment_start = newline + 1;

if (options == NULL || options->main_script) {
pm_parser_warn_shebang_carriage_return(parser, parser->start, length + 1);
}
Expand All @@ -23522,7 +23555,7 @@ pm_parser_init(pm_arena_t *arena, pm_parser_t *parser, const uint8_t *source, si
}

search_shebang = false;
} else if (options != NULL && options->main_script && !parser->parsing_eval) {
} else if (length > 2 && options != NULL && options->main_script && !parser->parsing_eval) {
search_shebang = true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/strncasecmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) {
size_t offset = 0;
int difference = 0;

while (offset < length && string1[offset] != '\0') {
if (string2[offset] == '\0') return string1[offset];
while (offset < length) {
if ((difference = pm_tolower(string1[offset]) - pm_tolower(string2[offset])) != 0) return difference;
if (string1[offset] == '\0') return 0;
offset++;
}

Expand Down
6 changes: 3 additions & 3 deletions src/strpbrk.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ pm_strpbrk_ascii_8bit(pm_parser_t *parser, const uint8_t *source, const uint8_t
return source + index;
}

if (validate && source[index] >= 0x80) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source - parser->start), 1);
if (validate && source[index] >= 0x80) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source + index - parser->start), 1);
index++;
}

Expand All @@ -343,7 +343,7 @@ pm_strpbrk_multi_byte(pm_parser_t *parser, const uint8_t *source, const uint8_t
index++;
} else {
size_t width = pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index));
if (validate) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source - parser->start), (uint32_t) width);
if (validate) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source + index - parser->start), (uint32_t) (width == 0 ? 1 : width));

if (width > 0) {
index += width;
Expand Down Expand Up @@ -385,7 +385,7 @@ pm_strpbrk_single_byte(pm_parser_t *parser, const uint8_t *source, const uint8_t
index++;
} else {
size_t width = pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index));
pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source - parser->start), (uint32_t) width);
pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source + index - parser->start), (uint32_t) (width == 0 ? 1 : width));

if (width > 0) {
index += width;
Expand Down
24 changes: 24 additions & 0 deletions test/prism/api/raise_error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ def test_syntax_error_context_line_numbers
assert_equal [gutters.first], gutters.uniq
end

def test_syntax_error_extreme_line_numbers
[-(2**31), 2**31 - 1].each do |line|
error = assert_raise(SyntaxError) { Prism.parse("}\n", raise_error: :plain, line: line) }
assert_include error.message, "> #{line} | }\n"
end
end

def test_syntax_error_context_across_line_number_wraparound
source = "\n}\n\n\n"
error = assert_raise(SyntaxError) { Prism.parse(source, raise_error: :plain, line: 2**31 - 4) }
assert_include error.message, "> 2147483645 | }\n"
assert_include error.message, " 2147483647 | \n"

source = "1 2\n3 4\n5 6\n"
error = assert_raise(SyntaxError) { Prism.parse(source, raise_error: :plain, line: 2**31 - 1) }
source_lines = error.message.lines.grep(/^> /)
assert_equal [
"> 2147483647 | 1 2\n",
"> -2147483648 | 3 4\n",
"> -2147483647 | 5 6\n"
], source_lines
assert_equal [14], error.message.lines.grep(/\|/).map { |line| line.index("|") }.uniq
end

def test_syntax_error_not_utf8_in_context
# A line of context is displayed, so it must be valid UTF-8 for the
# snippet to be displayed, even though it has no error of its own.
Expand Down
Loading
Loading