[ruby/json] Fix a regression in the parser with leading /

Ref: https://github.com/ruby/ruby/pull/12598

This could lead to an infinite loop.

https://github.com/ruby/json/commit/f8cfa2696a
This commit is contained in:
Jean Boussier 2025-01-20 08:34:40 +01:00
parent 4404688a0e
commit 33708f2dc4
Notes: git 2025-01-20 09:32:14 +00:00
2 changed files with 13 additions and 3 deletions

View File

@ -476,7 +476,7 @@ static const bool whitespace[256] = {
['/'] = 1, ['/'] = 1,
}; };
static void static bool
json_eat_comments(JSON_ParserState *state) json_eat_comments(JSON_ParserState *state)
{ {
if (state->cursor + 1 < state->end) { if (state->cursor + 1 < state->end) {
@ -508,9 +508,10 @@ json_eat_comments(JSON_ParserState *state)
break; break;
} }
default: default:
return; return false;
} }
} }
return true;
} }
static inline void static inline void
@ -520,7 +521,9 @@ json_eat_whitespace(JSON_ParserState *state)
if (RB_LIKELY(*state->cursor != '/')) { if (RB_LIKELY(*state->cursor != '/')) {
state->cursor++; state->cursor++;
} else { } else {
json_eat_comments(state); if (!json_eat_comments(state)) {
return;
}
} }
} }
} }

View File

@ -629,6 +629,13 @@ class JSONParserTest < Test::Unit::TestCase
end end
end end
def test_parse_leading_slash
# ref: https://github.com/ruby/ruby/pull/12598
assert_raise(JSON::ParserError) do
JSON.parse("/foo/bar")
end
end
private private
def string_deduplication_available? def string_deduplication_available?