Skip to content
Myr

Fix signed-int overflow in `tokenadd` to prevent buffer overflow

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by itchyny · committed by itchyny · parent 5f2a14d · Browse files · 1 file+8 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+8 added−4 removed

  • other:+8 added−4 removed

1 file+8 added−4 removed

modified:src/jv_parse.c+8 added-4 removed
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <limits.h>5#include <assert.h>6#include "jv.h"7#include "jv_dtoa.h"8#include "jv_unicode.h"
410 unmodified lines
419 }420 return 0;421}422422static void tokenadd(struct jv_parser* p, char c) {423static pfunc tokenadd(struct jv_parser* p, char c) {424 assert(p->tokenpos <= p->tokenlen);425 if (p->tokenpos >= (p->tokenlen - 1)) {425 p->tokenlen = p->tokenlen*2 + 256;426 if (p->tokenlen > INT_MAX / 2)427 return "Token too long";428 p->tokenlen *= 2;429 p->tokenbuf = jv_mem_realloc(p->tokenbuf, p->tokenlen);430 }431 assert(p->tokenpos < p->tokenlen);432 p->tokenbuf[p->tokenpos++] = c;433 return 0;434}435436static int unhex4(char* hex) {437 int r = 0;
242 unmodified lines
680 if (check_done(p, out)) answer = OK;681 }682 switch (cls) {683 case LITERAL:680 tokenadd(p, ch);684 TRY(tokenadd(p, ch));685 break;686 case WHITESPACE:687 break;688 case QUOTE:
11 unmodified lines
700 TRY(found_string(p));701 p->st = JV_PARSER_NORMAL;702 if (check_done(p, out)) answer = OK;703 } else {700 tokenadd(p, ch);704 TRY(tokenadd(p, ch));705 if (ch == '\\' && p->st == JV_PARSER_STRING) {706 p->st = JV_PARSER_STRING_ESCAPE;707 } else {708 p->st = JV_PARSER_STRING;
211 unmodified lines