Skip to content

Commit 75240f6

Browse files
committed
jsonapi: fix memory leakage during OOM error recovery.
Coverity pointed out that inc_lex_level() would leak memory (not to mention corrupt the pstack data structure) if some but not all of its three REALLOC's failed. To fix, store successfully-updated pointers back into the pstack struct immediately. Oversight in 0785d1b, so no need for back-patch.
1 parent a7e5237 commit 75240f6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/common/jsonapi.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,19 +544,28 @@ inc_lex_level(JsonLexContext *lex)
544544

545545
new_prediction = REALLOC(lex->pstack->prediction,
546546
new_stack_size * JS_MAX_PROD_LEN);
547+
#ifdef JSONAPI_USE_PQEXPBUFFER
548+
if (!new_prediction)
549+
return false;
550+
#endif
551+
lex->pstack->prediction = new_prediction;
552+
547553
new_fnames = REALLOC(lex->pstack->fnames,
548554
new_stack_size * sizeof(char *));
549-
new_fnull = REALLOC(lex->pstack->fnull, new_stack_size * sizeof(bool));
555+
#ifdef JSONAPI_USE_PQEXPBUFFER
556+
if (!new_fnames)
557+
return false;
558+
#endif
559+
lex->pstack->fnames = new_fnames;
550560

561+
new_fnull = REALLOC(lex->pstack->fnull, new_stack_size * sizeof(bool));
551562
#ifdef JSONAPI_USE_PQEXPBUFFER
552-
if (!new_prediction || !new_fnames || !new_fnull)
563+
if (!new_fnull)
553564
return false;
554565
#endif
566+
lex->pstack->fnull = new_fnull;
555567

556568
lex->pstack->stack_size = new_stack_size;
557-
lex->pstack->prediction = new_prediction;
558-
lex->pstack->fnames = new_fnames;
559-
lex->pstack->fnull = new_fnull;
560569
}
561570

562571
lex->lex_level += 1;

0 commit comments

Comments
 (0)