File tree 2 files changed +18
-0
lines changed
2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -339,6 +339,7 @@ def generate(obj)
339
339
private def fast_serialize_string ( string , buf ) # :nodoc:
340
340
buf << '"'
341
341
string = string . encode ( ::Encoding ::UTF_8 ) unless string . encoding == ::Encoding ::UTF_8
342
+ raise GeneratorError , "source sequence is illegal/malformed utf-8" unless string . valid_encoding?
342
343
343
344
if /["\\ \x0 -\x1f ]/n . match? ( string )
344
345
buf << string . gsub ( /["\\ \x0 -\x1f ]/n , MAP )
Original file line number Diff line number Diff line change @@ -443,6 +443,23 @@ def test_invalid_encoding_string
443
443
"\x82 \xAC \xEF " . to_json
444
444
end
445
445
assert_includes error . message , "source sequence is illegal/malformed utf-8"
446
+
447
+ error = assert_raise ( JSON ::GeneratorError ) do
448
+ JSON . dump ( "\x82 \xAC \xEF " )
449
+ end
450
+ assert_includes error . message , "source sequence is illegal/malformed utf-8"
451
+
452
+ # These pass on the pure-Ruby generator but not with the native extension
453
+ # https://github.com/ruby/json/issues/634
454
+ if defined? ( JSON ::Pure )
455
+ assert_raise ( Encoding ::UndefinedConversionError ) do
456
+ "\x82 \xAC \xEF " . b . to_json
457
+ end
458
+
459
+ assert_raise ( Encoding ::UndefinedConversionError ) do
460
+ JSON . dump ( "\x82 \xAC \xEF " . b )
461
+ end
462
+ end
446
463
end
447
464
448
465
if defined? ( JSON ::Ext ::Generator ) and RUBY_PLATFORM != "java"
You can’t perform that action at this time.
0 commit comments