Skip to content

Commit 661ab4e

Browse files
committed
Fix some memory leaks associated with parsing json and manifests
Coverity complained about not freeing some memory associated with incrementally parsing backup manifests. To fix that, provide and use a new shutdown function for the JsonManifestParseIncrementalState object, in line with a suggestion from Tom Lane. While analysing the problem, I noticed a buglet in freeing memory for incremental json lexers. To fix that remove a bogus condition on freeing the memory allocated for them.
1 parent b9ecefe commit 661ab4e

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

src/backend/backup/basebackup_incremental.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ FinalizeIncrementalManifest(IncrementalBackupInfo *ib)
241241
pfree(ib->buf.data);
242242
ib->buf.data = NULL;
243243

244+
/* Done with inc_state, so release that memory too */
245+
json_parse_manifest_incremental_shutdown(ib->inc_state);
246+
244247
/* Switch back to previous memory context. */
245248
MemoryContextSwitchTo(oldcontext);
246249
}

src/bin/pg_combinebackup/load_manifest.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ load_backup_manifest(char *backup_directory)
208208
inc_state, buffer, rc, bytes_left == 0);
209209
}
210210

211+
/* Release the incremental state memory */
212+
json_parse_manifest_incremental_shutdown(inc_state);
213+
211214
close(fd);
212215
}
213216

src/bin/pg_verifybackup/pg_verifybackup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ parse_manifest_file(char *manifest_path)
484484
inc_state, buffer, rc, bytes_left == 0);
485485
}
486486

487+
/* Release the incremental state memory */
488+
json_parse_manifest_incremental_shutdown(inc_state);
489+
487490
close(fd);
488491
}
489492

src/common/jsonapi.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -488,19 +488,18 @@ freeJsonLexContext(JsonLexContext *lex)
488488
if (lex->errormsg)
489489
destroyStringInfo(lex->errormsg);
490490

491-
if (lex->flags & JSONLEX_FREE_STRUCT)
491+
if (lex->incremental)
492492
{
493-
if (lex->incremental)
494-
{
495-
pfree(lex->inc_state->partial_token.data);
496-
pfree(lex->inc_state);
497-
pfree(lex->pstack->prediction);
498-
pfree(lex->pstack->fnames);
499-
pfree(lex->pstack->fnull);
500-
pfree(lex->pstack);
501-
}
502-
pfree(lex);
493+
pfree(lex->inc_state->partial_token.data);
494+
pfree(lex->inc_state);
495+
pfree(lex->pstack->prediction);
496+
pfree(lex->pstack->fnames);
497+
pfree(lex->pstack->fnull);
498+
pfree(lex->pstack);
503499
}
500+
501+
if (lex->flags & JSONLEX_FREE_STRUCT)
502+
pfree(lex);
504503
}
505504

506505
/*

src/common/parse_manifest.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ static bool parse_xlogrecptr(XLogRecPtr *result, char *input);
123123

124124
/*
125125
* Set up for incremental parsing of the manifest.
126-
*
127126
*/
128127

129128
JsonManifestParseIncrementalState *
@@ -163,6 +162,18 @@ json_parse_manifest_incremental_init(JsonManifestParseContext *context)
163162
return incstate;
164163
}
165164

165+
/*
166+
* Free an incremental state object and its contents.
167+
*/
168+
void
169+
json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate)
170+
{
171+
pfree(incstate->sem.semstate);
172+
freeJsonLexContext(&(incstate->lex));
173+
/* incstate->manifest_ctx has already been freed */
174+
pfree(incstate);
175+
}
176+
166177
/*
167178
* parse the manifest in pieces.
168179
*

src/include/common/parse_manifest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ extern JsonManifestParseIncrementalState *json_parse_manifest_incremental_init(J
5353
extern void json_parse_manifest_incremental_chunk(
5454
JsonManifestParseIncrementalState *incstate, char *chunk, int size,
5555
bool is_last);
56+
extern void json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate);
5657

5758
#endif

0 commit comments

Comments
 (0)