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
This commit is contained in:
Daniel Gustafsson 2024-03-16 23:18:28 +01:00
parent 927332b95e
commit b783186515
8 changed files with 31 additions and 17 deletions

View File

@ -397,8 +397,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink,
endtli = backup_state->stoptli;
/* Deallocate backup-related variables. */
pfree(tablespace_map->data);
pfree(tablespace_map);
destroyStringInfo(tablespace_map);
pfree(backup_state);
}
PG_END_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));

View File

@ -506,8 +506,7 @@ check_publications(WalReceiverConn *wrconn, List *publications)
appendStringInfoChar(cmd, ')');
res = walrcv_exec(wrconn, cmd->data, 1, tableRow);
pfree(cmd->data);
pfree(cmd);
destroyStringInfo(cmd);
if (res->status != WALRCV_OK_TUPLES)
ereport(ERROR,

View File

@ -133,8 +133,7 @@ jsonb_send(PG_FUNCTION_ARGS)
pq_begintypsend(&buf);
pq_sendint8(&buf, version);
pq_sendtext(&buf, jtext->data, jtext->len);
pfree(jtext->data);
pfree(jtext);
destroyStringInfo(jtext);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

View File

@ -2163,8 +2163,7 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
appendBinaryStringInfo(&xmlerrcxt->err_buf, errorBuf->data,
errorBuf->len);
pfree(errorBuf->data);
pfree(errorBuf);
destroyStringInfo(errorBuf);
return;
}
@ -2195,8 +2194,7 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
(errmsg_internal("%s", errorBuf->data)));
}
pfree(errorBuf->data);
pfree(errorBuf);
destroyStringInfo(errorBuf);
}

View File

@ -526,10 +526,7 @@ check_backup_label_files(int n_backups, char **backup_dirs)
/* Free memory that we don't need any more. */
if (lastbuf != buf)
{
pfree(buf->data);
pfree(buf);
}
destroyStringInfo(buf);
/*
* Return the data from the first backup_info that we read (which is the

View File

@ -350,3 +350,19 @@ enlargeStringInfo(StringInfo str, int needed)
str->maxlen = newlen;
}
/*
* destroyStringInfo
*
* Frees a StringInfo and its buffer (opposite of makeStringInfo()).
* This must only be called on palloc'd StringInfos.
*/
void
destroyStringInfo(StringInfo str)
{
/* don't allow destroys of read-only StringInfos */
Assert(str->maxlen != 0);
pfree(str->data);
pfree(str);
}

View File

@ -87,7 +87,8 @@ typedef StringInfoData *StringInfo;
* to be len + 1 in size.
*
* To destroy a StringInfo, pfree() the data buffer, and then pfree() the
* StringInfoData if it was palloc'd. There's no special support for this.
* StringInfoData if it was palloc'd. For StringInfos created with
* makeStringInfo(), destroyStringInfo() is provided for this purpose.
* However, if the StringInfo was initialized using initReadOnlyStringInfo()
* then the caller will need to consider if it is safe to pfree the data
* buffer.
@ -233,4 +234,10 @@ extern void appendBinaryStringInfoNT(StringInfo str,
*/
extern void enlargeStringInfo(StringInfo str, int needed);
/*------------------------
* destroyStringInfo
* Frees a StringInfo and its buffer (opposite of makeStringInfo()).
*/
extern void destroyStringInfo(StringInfo str);
#endif /* STRINGINFO_H */

View File

@ -1174,8 +1174,7 @@ psql_end_command(StringInfo buf, const char *database)
}
/* Clean up */
pfree(buf->data);
pfree(buf);
destroyStringInfo(buf);
}
/*