Skip to content

Commit 69b4b2f

Browse files
committed
iop-json/parsers: Make parsers strict again about invalid \x and \u seqs
This is a revert of 838013bdf162 and 5d0f7222d1b704. These patches were made after 487bf9ec11bfcc1 to not make the parsers stricter on a stable branch, at the risk of breaking something on prod platforms. But we ultimately want to be strict, so let's do this on 2026 major release before it is released. Change-Id: I0a00abb6bb705482638b2b19d9e3f10e42e9d46e Priv-Id: 8c93d1ce544ee89517ea8c2e6be9f273a4d20657
1 parent 147f795 commit 69b4b2f

3 files changed

Lines changed: 8 additions & 43 deletions

File tree

src/core/parsing-helpers.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ parse_backslash(pstream_t *ps, sb_t *buf, int *line, int *col)
102102
}
103103
break;
104104
case 'x':
105-
if (ps_has(ps, 4) && (a = hexdecode(ps->s + 2)) >= 0) {
106-
sb_addc(buf, a);
105+
if (ps_has(ps, 4)) {
106+
sb_addc(buf, PS_CHECK(hexdecode(ps->s + 2)));
107107
SKIP(4);
108108
return 0;
109109
}
110110
break;
111111
case 'u': {
112-
if (ps_has(ps, 6) && (a = hexdecode(ps->s + 2)) >= 0
113-
&& (b = hexdecode(ps->s + 4)) >= 0)
114-
{
112+
if (ps_has(ps, 6)) {
115113
int codepoint, skip_len;
116114

115+
a = PS_CHECK(hexdecode(ps->s + 2));
116+
b = PS_CHECK(hexdecode(ps->s + 4));
117117
codepoint = (a << 8) | b;
118118

119119
/* Handle Unicode character (BMP or surrogate pair) */

src/iop/json.blk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,15 +639,15 @@ static int iop_json_lex_char(iop_json_lex_t *ll, int terminator)
639639
}
640640
break;
641641
case HEXADECIMAL:
642-
if (HAS(4) && (a = hexdecode(PS->s + 2)) >= 0) {
642+
if (HAS(4) && (a = PS_CHECK(hexdecode(PS->s + 2))) >= 0) {
643643
ll->ctx->u.i = a;
644644
SKIP(2);
645645
}
646646
break;
647647
case UNICODE:
648648
if (HAS(6)
649-
&& (a = hexdecode(PS->s + 2)) >= 0
650-
&& (b = hexdecode(PS->s + 4)) >= 0)
649+
&& (a = PS_CHECK(hexdecode(PS->s + 2))) >= 0
650+
&& (b = PS_CHECK(hexdecode(PS->s + 4))) >= 0)
651651
{
652652
int codepoint = (a << 8) | b;
653653

tests/zchk-iop.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,41 +3120,6 @@ Z_GROUP_EXPORT(iop)
31203120

31213121
} Z_TEST_END
31223122
/* }}} */
3123-
Z_TEST(json_invalid_hex_escape, "test JSON with invalid \\x escapes") {
3124-
/* {{{ */
3125-
t_scope;
3126-
const iop_struct_t *st_string = &tstiop__string_test__s;
3127-
3128-
/* Invalid hex escape sequences after \x should be kept literally
3129-
* instead of causing a parse error.
3130-
* Regression test for a bug where PS_CHECK(hexdecode()) would
3131-
* propagate the error instead of falling through to the default
3132-
* literal copy. */
3133-
{
3134-
struct {
3135-
const char *json_input;
3136-
const char *expected_string;
3137-
const char *test_name;
3138-
} tests[] = {
3139-
{"\"\\x\\y\"", "\\x\\y", "two invalid hex escapes"},
3140-
{"\"\\xZZ\"", "\\xZZ", "invalid hex digits"},
3141-
{"\"\\x\"", "\\x", "truncated hex escape"},
3142-
};
3143-
3144-
carray_for_each_ptr(t, tests) {
3145-
tstiop__string_test__t string_test;
3146-
const char *json_buf;
3147-
3148-
json_buf = t_fmt("{\"testString\": %s}", t->json_input);
3149-
string_test.test_string = LSTR(t->expected_string);
3150-
3151-
Z_HELPER_RUN(iop_json_test_json(st_string, json_buf,
3152-
&string_test,
3153-
t->test_name));
3154-
}
3155-
}
3156-
} Z_TEST_END
3157-
/* }}} */
31583123
Z_TEST(json_big_integer, "test JSON packing with big integers") { /* {{{ */
31593124
SB_1k(sb);
31603125
tstiop__my_struct_n__t sn = {

0 commit comments

Comments
 (0)