Skip to content

Commit

Permalink
unescape: Try to fill out with \uFFFD for invalid sequence
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
  • Loading branch information
cosmo0920 committed Jan 8, 2025
1 parent dfe15aa commit daee871
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/flb_unescape.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ static int u8_read_escape_sequence(const char *str, int size, uint32_t *dest)
}
if (dno != 4) {
/* Incomplete \u escape sequence */
ch = L'\uFFFD';
goto invalid_sequence;
}
ch = strtol(digs, NULL, 16);
if (u8_low_surrogate(ch)) {
/* Invalid: low surrogate without preceding high surrogate */
ch = L'\uFFFD';
goto invalid_sequence;
}
else if (u8_high_surrogate(ch)) {
Expand All @@ -139,6 +141,7 @@ static int u8_read_escape_sequence(const char *str, int size, uint32_t *dest)
}
if (dno != 4) {
/* Incomplete low surrogate */
ch = L'\uFFFD';
goto invalid_sequence;
}
uint32_t low = strtol(digs, NULL, 16);
Expand All @@ -147,11 +150,13 @@ static int u8_read_escape_sequence(const char *str, int size, uint32_t *dest)
}
else {
/* Invalid: high surrogate not followed by low surrogate */
ch = L'\uFFFD';
goto invalid_sequence;
}
}
else {
/* Invalid: high surrogate not followed by \u */
ch = L'\uFFFD';
goto invalid_sequence;
}
}
Expand All @@ -166,10 +171,9 @@ static int u8_read_escape_sequence(const char *str, int size, uint32_t *dest)
}
*dest = ch;

return i;

invalid_sequence:
return -1;

return i;
}

int flb_unescape_string_utf8(const char *in_buf, int sz, char *out_buf)
Expand Down Expand Up @@ -223,6 +227,10 @@ int flb_unescape_string_utf8(const char *in_buf, int sz, char *out_buf)
size = end - next;
if (size > 0) {
esc_in = u8_read_escape_sequence(next, size, &ch) + 1;
if (esc_in == -1) {
flb_error("invalid sequence detected");
break;
}
}
else {
/* because char is unsigned char by default on arm, so we need to do a explicit conversion */
Expand Down

0 comments on commit daee871

Please sign in to comment.