From 4760ed147e57fbf38fd0ecbbf7abaa41e9a751b9 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 15 Dec 2023 23:41:20 -0500 Subject: [PATCH] fix hang on incomplete json ident --- src/json/read.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/json/read.c b/src/json/read.c index 1401633..ea6e106 100644 --- a/src/json/read.c +++ b/src/json/read.c @@ -170,30 +170,31 @@ static bool json_parse_string(tokendata_t *token, const stream_t *stream) { return true; } -static bool json_parse_ident(token_t *token, const stream_t *stream, char first) { - - char buf[4]; - buf[3] = '\0'; - - if (stream_read(stream, buf, 3) == false) +static bool json_ident_assert(const stream_t *stream, const char *rest) { + char c; + if (stream_read(stream, &c, 1) == 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->data.b = true; - } else if (first == 'f' && strcmp(buf, "als") == 0) { - if (stream_read(stream, buf, 1) == false) - return false; - if (*buf != 'e') - return false; + } else if (first == 'f' && json_ident_assert(stream, "alse")) { token->type = TOK_BOOL; 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; } else { return false; } - return true; }