From 93a7f8717d92013aca42a337e26ba9b85a7da317 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 3 Dec 2024 09:11:31 +0100 Subject: [PATCH] Fix generate(script_safe: true) to not confuse unrelated characters Fix: https://github.com/ruby/json/issues/715 The first byte check was missing. --- ext/json/ext/generator/generator.c | 2 +- test/json/json_generator_test.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 67d2ea32..d5c8bfd4 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/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 6e4e293d..8dd3913d 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -455,6 +455,10 @@ def test_backslash data = ["'"] json = '["\\\'"]' assert_equal '["\'"]', generate(data) + # + data = ["倩", "瀨"] + json = '["倩","瀨"]' + assert_equal json, generate(data, script_safe: true) end def test_string_subclass