Skip to content

Commit

Permalink
Fix sign extension bug when parsing JSON utf-8 symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkelfj committed Nov 16, 2023
1 parent 6c3fe81 commit 88e2bc1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions include/flatcc/flatcc_json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,39 +240,40 @@ static inline uint64_t flatcc_json_parser_symbol_part_ext(const char *buf, const
if (n > 8) {
n = 8;
}
/* uint8_t cast is needed to avoid sign extension for utf8 symbols */
/* This can bloat inlining for a rarely executed case. */
#if 1
switch (n) {
case 8:
w |= ((uint64_t)buf[7]) << (0 * 8);
w |= ((uint64_t)(uint8_t)buf[7]) << (0 * 8);
goto lbl_n_7;
case 7:
lbl_n_7:
w |= ((uint64_t)buf[6]) << (1 * 8);
w |= ((uint64_t)(uint8_t)buf[6]) << (1 * 8);
goto lbl_n_6;
case 6:
lbl_n_6:
w |= ((uint64_t)buf[5]) << (2 * 8);
w |= ((uint64_t)(uint8_t)buf[5]) << (2 * 8);
goto lbl_n_5;
case 5:
lbl_n_5:
w |= ((uint64_t)buf[4]) << (3 * 8);
w |= ((uint64_t)(uint8_t)buf[4]) << (3 * 8);
goto lbl_n_4;
case 4:
lbl_n_4:
w |= ((uint64_t)buf[3]) << (4 * 8);
w |= ((uint64_t)(uint8_t)buf[3]) << (4 * 8);
goto lbl_n_3;
case 3:
lbl_n_3:
w |= ((uint64_t)buf[2]) << (5 * 8);
w |= ((uint64_t)(uint8_t)buf[2]) << (5 * 8);
goto lbl_n_2;
case 2:
lbl_n_2:
w |= ((uint64_t)buf[1]) << (6 * 8);
w |= ((uint64_t)(uint8_t)buf[1]) << (6 * 8);
goto lbl_n_1;
case 1:
lbl_n_1:
w |= ((uint64_t)buf[0]) << (7 * 8);
w |= ((uint64_t)(uint8_t)buf[0]) << (7 * 8);
break;
case 0:
break;
Expand All @@ -284,7 +285,7 @@ static inline uint64_t flatcc_json_parser_symbol_part_ext(const char *buf, const
for (i = 0; i < n; ++i) {
w <<= 8;
if (i < n) {
w = buf[i];
w = (uint8_t)buf[i];
}
}
}
Expand Down

0 comments on commit 88e2bc1

Please sign in to comment.