Skip to content

Commit 33708f2

Browse files
committed
[ruby/json] Fix a regression in the parser with leading /
Ref: ruby#12598 This could lead to an infinite loop. ruby/json@f8cfa2696a
1 parent 4404688 commit 33708f2

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

ext/json/parser/parser.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static const bool whitespace[256] = {
476476
['/'] = 1,
477477
};
478478

479-
static void
479+
static bool
480480
json_eat_comments(JSON_ParserState *state)
481481
{
482482
if (state->cursor + 1 < state->end) {
@@ -508,9 +508,10 @@ json_eat_comments(JSON_ParserState *state)
508508
break;
509509
}
510510
default:
511-
return;
511+
return false;
512512
}
513513
}
514+
return true;
514515
}
515516

516517
static inline void
@@ -520,7 +521,9 @@ json_eat_whitespace(JSON_ParserState *state)
520521
if (RB_LIKELY(*state->cursor != '/')) {
521522
state->cursor++;
522523
} else {
523-
json_eat_comments(state);
524+
if (!json_eat_comments(state)) {
525+
return;
526+
}
524527
}
525528
}
526529
}

test/json/json_parser_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,13 @@ def test_parse_error_incomplete_hash
629629
end
630630
end
631631

632+
def test_parse_leading_slash
633+
# ref: https://github.com/ruby/ruby/pull/12598
634+
assert_raise(JSON::ParserError) do
635+
JSON.parse("/foo/bar")
636+
end
637+
end
638+
632639
private
633640

634641
def string_deduplication_available?

0 commit comments

Comments
 (0)