summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2024-12-03 09:11:31 +0100
committerJean Boussier <[email protected]>2024-12-05 09:16:22 +0100
commit1510d72bec297047b5fd44eb89cf66cd4cb248e8 (patch)
treec2f84dcf646b1c5482f7ba136e1bdb5b7bb5c8a4
parent4b850ea05809689d29710067b7f0de6f2851765b (diff)
[ruby/json] Fix generate(script_safe: true) to not confuse unrelated characters
Fix: https://github.com/ruby/json/issues/715 The first byte check was missing. https://github.com/ruby/json/commit/93a7f8717d
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12267
-rw-r--r--ext/json/generator/generator.c2
-rwxr-xr-xtest/json/json_generator_test.rb4
2 files changed, 5 insertions, 1 deletions
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index 67d2ea32c4..d5c8bfd42a 100644
--- a/ext/json/generator/generator.c
+++ b/ext/json/generator/generator.c
@@ -155,7 +155,7 @@ static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE str, const char esca
}
case 3: {
unsigned char b2 = ptr[pos + 1];
- if (RB_UNLIKELY(out_script_safe && b2 == 0x80)) {
+ if (RB_UNLIKELY(out_script_safe && ch == 0xE2 && b2 == 0x80)) {
unsigned char b3 = ptr[pos + 2];
if (b3 == 0xA8) {
FLUSH_POS(3);
diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb
index 6e4e293db3..8dd3913d62 100755
--- a/test/json/json_generator_test.rb
+++ b/test/json/json_generator_test.rb
@@ -455,6 +455,10 @@ class JSONGeneratorTest < Test::Unit::TestCase
data = ["'"]
json = '["\\\'"]'
assert_equal '["\'"]', generate(data)
+ #
+ data = ["倩", "瀨"]
+ json = '["倩","瀨"]'
+ assert_equal json, generate(data, script_safe: true)
end
def test_string_subclass