summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2025-01-20 08:34:40 +0100
committerJean Boussier <[email protected]>2025-01-20 10:31:56 +0100
commit33708f2dc4f8327aa70fe10a53423b13da85e69e (patch)
treedbdca13fbfcaecf8ae8bffaa98bcdff8df1f7372
parent4404688a0e9e606aea870d79f5e8be6ac1524335 (diff)
[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
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12600
-rw-r--r--ext/json/parser/parser.c9
-rw-r--r--test/json/json_parser_test.rb7
2 files changed, 13 insertions, 3 deletions
diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c
index 1398b6b31d..907bd0477b 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/parser/parser.c
@@ -476,7 +476,7 @@ static const bool whitespace[256] = {
['/'] = 1,
};
-static void
+static bool
json_eat_comments(JSON_ParserState *state)
{
if (state->cursor + 1 < state->end) {
@@ -508,9 +508,10 @@ json_eat_comments(JSON_ParserState *state)
break;
}
default:
- return;
+ return false;
}
}
+ return true;
}
static inline void
@@ -520,7 +521,9 @@ json_eat_whitespace(JSON_ParserState *state)
if (RB_LIKELY(*state->cursor != '/')) {
state->cursor++;
} else {
- json_eat_comments(state);
+ if (!json_eat_comments(state)) {
+ return;
+ }
}
}
}
diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb
index 5956200893..c5ce02320f 100644
--- a/test/json/json_parser_test.rb
+++ b/test/json/json_parser_test.rb
@@ -629,6 +629,13 @@ class JSONParserTest < Test::Unit::TestCase
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
def string_deduplication_available?