Skip to content

Commit 281a000

Browse files
committed
[Bug #19558] Allow ASCII range to mix with Unicode dump
1 parent dc41cf3 commit 281a000

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

string.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7576,17 +7576,19 @@ undump_after_backslash(VALUE undumped, const char **ss, const char *s_end, rb_en
75767576
}
75777577
break;
75787578
case 'x':
7579-
if (*utf8) {
7580-
rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
7581-
}
7582-
*binary = true;
75837579
if (++s >= s_end) {
75847580
rb_raise(rb_eRuntimeError, "invalid hex escape");
75857581
}
75867582
*buf = scan_hex(s, 2, &hexlen);
75877583
if (hexlen != 2) {
75887584
rb_raise(rb_eRuntimeError, "invalid hex escape");
75897585
}
7586+
if (!ISASCII(*buf)) {
7587+
if (*utf8) {
7588+
rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
7589+
}
7590+
*binary = true;
7591+
}
75907592
rb_str_cat(undumped, (char *)buf, 1);
75917593
s += hexlen;
75927594
break;

test/ruby/test_string.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,10 @@ def test_undump
872872
assert_equal('\#', S('"\\\\#"').undump)
873873
assert_equal('\#{', S('"\\\\\#{"').undump)
874874

875+
assert_undump("\0\u{ABCD}")
876+
assert_undump(S('"\x00\u3042"'.force_encoding("SJIS")))
877+
assert_undump(S('"\u3042\x7E"'.force_encoding("SJIS")))
878+
875879
assert_raise(RuntimeError) { S('\u3042').undump }
876880
assert_raise(RuntimeError) { S('"\x82\xA0\u3042"'.force_encoding("SJIS")).undump }
877881
assert_raise(RuntimeError) { S('"\u3042\x82\xA0"'.force_encoding("SJIS")).undump }
@@ -4027,6 +4031,10 @@ def assert_byteindex(expected, string, match, *rest)
40274031
def assert_byterindex(expected, string, match, *rest)
40284032
assert_index_like(:byterindex, expected, string, match, *rest)
40294033
end
4034+
4035+
def assert_undump(str, *rest)
4036+
assert_equal(str, str.dump.undump, *rest)
4037+
end
40304038
end
40314039

40324040
class TestString2 < TestString

0 commit comments

Comments
 (0)