Skip to content

Commit 850bd07

Browse files
committed
Add test for parsing broken strings
1 parent 7af612b commit 850bd07

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

test/json/json_parser_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ def test_parse_some_strings
196196
)
197197
end
198198

199+
def test_parse_broken_string
200+
# https://github.com/ruby/json/issues/138
201+
s = parse(%{["\x80"]})[0]
202+
assert_equal("\x80", s)
203+
assert_equal Encoding::UTF_8, s.encoding
204+
assert_equal false, s.valid_encoding?
205+
206+
s = parse(%{["\x80"]}.b)[0]
207+
assert_equal("\x80", s)
208+
assert_equal Encoding::UTF_8, s.encoding
209+
assert_equal false, s.valid_encoding?
210+
211+
input = %{["\x80"]}.dup.force_encoding(Encoding::US_ASCII)
212+
assert_raise(Encoding::InvalidByteSequenceError) { parse(input) }
213+
end
214+
199215
def test_parse_big_integers
200216
json1 = JSON(orig = (1 << 31) - 1)
201217
assert_equal orig, parse(json1)

0 commit comments

Comments
 (0)