fix hang on incomplete json ident

This commit is contained in:
Murphy 2023-12-15 23:41:20 -05:00
parent 8a4f45db71
commit 4760ed147e
No known key found for this signature in database
GPG key ID: 988032A5638EE799

View file

@ -170,30 +170,31 @@ static bool json_parse_string(tokendata_t *token, const stream_t *stream) {
return true; return true;
} }
static bool json_parse_ident(token_t *token, const stream_t *stream, char first) { static bool json_ident_assert(const stream_t *stream, const char *rest) {
char c;
char buf[4]; if (stream_read(stream, &c, 1) == false)
buf[3] = '\0';
if (stream_read(stream, buf, 3) == false)
return false; return false;
if (c != *rest)
return false;
rest += 1;
if (*rest == '\0')
return true;
else
return json_ident_assert(stream, rest);
}
if (first == 't' && strcmp(buf, "rue") == 0) { static bool json_parse_ident(token_t *token, const stream_t *stream, char first) {
if (first == 't' && json_ident_assert(stream, "true")) {
token->type = TOK_BOOL; token->type = TOK_BOOL;
token->data.b = true; token->data.b = true;
} else if (first == 'f' && strcmp(buf, "als") == 0) { } else if (first == 'f' && json_ident_assert(stream, "alse")) {
if (stream_read(stream, buf, 1) == false)
return false;
if (*buf != 'e')
return false;
token->type = TOK_BOOL; token->type = TOK_BOOL;
token->data.b = false; token->data.b = false;
} else if (first == 'n' && strcmp(buf, "ull") == 0) { } else if (first == 'n' && json_ident_assert(stream, "ull")) {
token->type = TOK_NULL; token->type = TOK_NULL;
} else { } else {
return false; return false;
} }
return true; return true;
} }