Skip to content

Commit 1da3fd9

Browse files
committed
Add a specific error for unescaped newlines
It's the most likely control character so it's worth giving a better error message for it.
1 parent cf3993c commit 1da3fd9

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

ext/json/ext/parser/parser.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,9 @@ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_Parser
752752
break;
753753
default:
754754
if ((unsigned char)*pe < 0x20) {
755+
if (*pe == '\n') {
756+
raise_parse_error_at("Invalid unescaped newline character (\\n) in string: %s", state, pe - 1);
757+
}
755758
raise_parse_error_at("invalid ASCII control character in string: %s", state, pe - 1);
756759
}
757760
raise_parse_error_at("invalid escape character in string: %s", state, pe - 1);

test/json/json_parser_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ def test_parse_complex_objects
164164
end
165165
end
166166

167+
def test_parse_control_chars_in_string
168+
0.upto(31) do |ord|
169+
assert_raise JSON::ParserError do
170+
parse(%("#{ord.chr}"))
171+
end
172+
end
173+
end
174+
167175
def test_parse_arrays
168176
assert_equal([1,2,3], parse('[1,2,3]'))
169177
assert_equal([1.2,2,3], parse('[1.2,2,3]'))

0 commit comments

Comments
 (0)