Skip to content

Commit b783186

Browse files
Add destroyStringInfo function for cleaning up StringInfos
destroyStringInfo() is a counterpart to makeStringInfo(), freeing a palloc'd StringInfo and its data. This is a convenience function to align the StringInfo API with the PQExpBuffer API. Originally added in the OAuth patchset, it was extracted and committed separately in order to aid upcoming JSON work. Author: Daniel Gustafsson <daniel@yesql.se> Author: Jacob Champion <jacob.champion@enterprisedb.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/CAOYmi+mWdTd6ujtyF7MsvXvk7ToLRVG_tYAcaGbQLvf=N4KrQw@mail.gmail.com
1 parent 927332b commit b783186

File tree

8 files changed

+31
-17
lines changed

8 files changed

+31
-17
lines changed

src/backend/backup/basebackup.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink,
397397
endtli = backup_state->stoptli;
398398

399399
/* Deallocate backup-related variables. */
400-
pfree(tablespace_map->data);
401-
pfree(tablespace_map);
400+
destroyStringInfo(tablespace_map);
402401
pfree(backup_state);
403402
}
404403
PG_END_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));

src/backend/commands/subscriptioncmds.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,7 @@ check_publications(WalReceiverConn *wrconn, List *publications)
506506
appendStringInfoChar(cmd, ')');
507507

508508
res = walrcv_exec(wrconn, cmd->data, 1, tableRow);
509-
pfree(cmd->data);
510-
pfree(cmd);
509+
destroyStringInfo(cmd);
511510

512511
if (res->status != WALRCV_OK_TUPLES)
513512
ereport(ERROR,

src/backend/utils/adt/jsonb.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ jsonb_send(PG_FUNCTION_ARGS)
133133
pq_begintypsend(&buf);
134134
pq_sendint8(&buf, version);
135135
pq_sendtext(&buf, jtext->data, jtext->len);
136-
pfree(jtext->data);
137-
pfree(jtext);
136+
destroyStringInfo(jtext);
138137

139138
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
140139
}

src/backend/utils/adt/xml.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,8 +2163,7 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
21632163
appendBinaryStringInfo(&xmlerrcxt->err_buf, errorBuf->data,
21642164
errorBuf->len);
21652165

2166-
pfree(errorBuf->data);
2167-
pfree(errorBuf);
2166+
destroyStringInfo(errorBuf);
21682167
return;
21692168
}
21702169

@@ -2195,8 +2194,7 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
21952194
(errmsg_internal("%s", errorBuf->data)));
21962195
}
21972196

2198-
pfree(errorBuf->data);
2199-
pfree(errorBuf);
2197+
destroyStringInfo(errorBuf);
22002198
}
22012199

22022200

src/bin/pg_combinebackup/pg_combinebackup.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,7 @@ check_backup_label_files(int n_backups, char **backup_dirs)
526526

527527
/* Free memory that we don't need any more. */
528528
if (lastbuf != buf)
529-
{
530-
pfree(buf->data);
531-
pfree(buf);
532-
}
529+
destroyStringInfo(buf);
533530

534531
/*
535532
* Return the data from the first backup_info that we read (which is the

src/common/stringinfo.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,19 @@ enlargeStringInfo(StringInfo str, int needed)
350350

351351
str->maxlen = newlen;
352352
}
353+
354+
/*
355+
* destroyStringInfo
356+
*
357+
* Frees a StringInfo and its buffer (opposite of makeStringInfo()).
358+
* This must only be called on palloc'd StringInfos.
359+
*/
360+
void
361+
destroyStringInfo(StringInfo str)
362+
{
363+
/* don't allow destroys of read-only StringInfos */
364+
Assert(str->maxlen != 0);
365+
366+
pfree(str->data);
367+
pfree(str);
368+
}

src/include/lib/stringinfo.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ typedef StringInfoData *StringInfo;
8787
* to be len + 1 in size.
8888
*
8989
* To destroy a StringInfo, pfree() the data buffer, and then pfree() the
90-
* StringInfoData if it was palloc'd. There's no special support for this.
90+
* StringInfoData if it was palloc'd. For StringInfos created with
91+
* makeStringInfo(), destroyStringInfo() is provided for this purpose.
9192
* However, if the StringInfo was initialized using initReadOnlyStringInfo()
9293
* then the caller will need to consider if it is safe to pfree the data
9394
* buffer.
@@ -233,4 +234,10 @@ extern void appendBinaryStringInfoNT(StringInfo str,
233234
*/
234235
extern void enlargeStringInfo(StringInfo str, int needed);
235236

237+
/*------------------------
238+
* destroyStringInfo
239+
* Frees a StringInfo and its buffer (opposite of makeStringInfo()).
240+
*/
241+
extern void destroyStringInfo(StringInfo str);
242+
236243
#endif /* STRINGINFO_H */

src/test/regress/pg_regress.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,8 +1174,7 @@ psql_end_command(StringInfo buf, const char *database)
11741174
}
11751175

11761176
/* Clean up */
1177-
pfree(buf->data);
1178-
pfree(buf);
1177+
destroyStringInfo(buf);
11791178
}
11801179

11811180
/*

0 commit comments

Comments
 (0)