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
14 changes: 14 additions & 0 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ static inline char peek(JSON_ParserState *state)

static void cursor_position(JSON_ParserState *state, long *line_out, long *column_out)
{
JSON_ASSERT(state->cursor <= state->end);

// Redundant but helpful for hardening
if (RB_UNLIKELY(state->cursor > state->end)) {
state->cursor = state->end;
}

const char *cursor = state->cursor;
long column = 0;
long line = 1;
Expand Down Expand Up @@ -1022,6 +1029,13 @@ ALWAYS_INLINE(static) bool string_scan(JSON_ParserState *state)
}
state->cursor++;
}

// If the string ended with an unterminated escape sequence, we might
// have gone past the end.
if (RB_UNLIKELY(state->cursor > state->end)) {
state->cursor = state->end;
}

return false;
}

Expand Down
12 changes: 8 additions & 4 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -545,22 +545,26 @@ def test_nesting
end

def test_backslash
assert_raise(JSON::ParserError) do
JSON.parse('"\\')
end

data = [ '\\.(?i:gif|jpe?g|png)$' ]
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
assert_equal data, parse(json)
#

data = [ '\\"' ]
json = '["\\\\\""]'
assert_equal data, parse(json)
#

json = '["/"]'
data = [ '/' ]
assert_equal data, parse(json)
#

json = '["\""]'
data = ['"']
assert_equal data, parse(json)
#

json = '["\\/"]'
data = ["/"]
assert_equal data, parse(json)
Expand Down
Loading