Skip to content

Commit

Permalink
Merge pull request #602 from byroot/pure-string-to-json
Browse files Browse the repository at this point in the history
JSONPure: String#to_json should raise on invalid encoding
  • Loading branch information
byroot authored Oct 8, 2024
2 parents 2ebfc07 + f69f21e commit fa43c6f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/json/pure/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ module String
def to_json(state = nil, *args)
state = State.from_state(state)
if encoding == ::Encoding::UTF_8
unless valid_encoding?
raise GeneratorError, "source sequence is illegal/malformed utf-8"
end
string = self
else
string = encode(::Encoding::UTF_8)
Expand Down
7 changes: 7 additions & 0 deletions tests/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ def to_s; self; end
end
end

def test_invalid_encoding_string
error = assert_raise(JSON::GeneratorError) do
"\x82\xAC\xEF".to_json
end
assert_includes error.message, "source sequence is illegal/malformed utf-8"
end

if defined?(JSON::Ext::Generator) and RUBY_PLATFORM != "java"
def test_string_ext_included_calls_super
included = false
Expand Down

0 comments on commit fa43c6f

Please sign in to comment.