diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2024-09-23 12:30:51 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2024-09-23 12:30:51 -0400 |
commit | 75240f65e700d7d157be27653c70fe75108f25c1 (patch) | |
tree | c7cd8a40107813461946bcd976a748705cfb7827 /src/common/jsonapi.c | |
parent | a7e5237f268ea378c514635d65a55aa47621958a (diff) | |
download | postgresql-75240f65e700d7d157be27653c70fe75108f25c1.tar.gz postgresql-75240f65e700d7d157be27653c70fe75108f25c1.zip |
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 0785d1b8b, so no need for back-patch.
Diffstat (limited to 'src/common/jsonapi.c')
-rw-r--r-- | src/common/jsonapi.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c index 6892a4be4e0..45838d8a184 100644 --- a/src/common/jsonapi.c +++ b/src/common/jsonapi.c @@ -544,19 +544,28 @@ inc_lex_level(JsonLexContext *lex) new_prediction = REALLOC(lex->pstack->prediction, new_stack_size * JS_MAX_PROD_LEN); +#ifdef JSONAPI_USE_PQEXPBUFFER + if (!new_prediction) + return false; +#endif + lex->pstack->prediction = new_prediction; + new_fnames = REALLOC(lex->pstack->fnames, new_stack_size * sizeof(char *)); - new_fnull = REALLOC(lex->pstack->fnull, new_stack_size * sizeof(bool)); +#ifdef JSONAPI_USE_PQEXPBUFFER + if (!new_fnames) + return false; +#endif + lex->pstack->fnames = new_fnames; + new_fnull = REALLOC(lex->pstack->fnull, new_stack_size * sizeof(bool)); #ifdef JSONAPI_USE_PQEXPBUFFER - if (!new_prediction || !new_fnames || !new_fnull) + if (!new_fnull) return false; #endif + lex->pstack->fnull = new_fnull; lex->pstack->stack_size = new_stack_size; - lex->pstack->prediction = new_prediction; - lex->pstack->fnames = new_fnames; - lex->pstack->fnull = new_fnull; } lex->lex_level += 1; |