File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -339,6 +339,7 @@ def generate(obj)
339339 private def fast_serialize_string ( string , buf ) # :nodoc:
340340 buf << '"'
341341 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?
342343
343344 if /["\\ \x0 -\x1f ]/n . match? ( string )
344345 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
443443 "\x82 \xAC \xEF " . to_json
444444 end
445445 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
446463 end
447464
448465 if defined? ( JSON ::Ext ::Generator ) and RUBY_PLATFORM != "java"
You can’t perform that action at this time.
0 commit comments