From 5015e1e1b58f81a036e4ad16291ef4b3bb7a596c Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 12 Sep 2022 08:31:56 +0200 Subject: Assorted examples of expanded type-safer palloc/pg_malloc API This adds some uses of the new palloc/pg_malloc variants here and there as a demonstration and test. This is kept separate from the actual API patch, since the latter might be backpatched at some point. Reviewed-by: Tom Lane Discussion: https://www.postgresql.org/message-id/flat/bb755632-2a43-d523-36f8-a1e7a389a907@enterprisedb.com --- src/backend/tcop/postgres.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/backend/tcop/postgres.c') diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 7bec4e4ff58..c6ca3b5b3d4 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -1660,7 +1660,7 @@ exec_bind_message(StringInfo input_message) numPFormats = pq_getmsgint(input_message, 2); if (numPFormats > 0) { - pformats = (int16 *) palloc(numPFormats * sizeof(int16)); + pformats = palloc_array(int16, numPFormats); for (int i = 0; i < numPFormats; i++) pformats[i] = pq_getmsgint(input_message, 2); } @@ -1848,8 +1848,7 @@ exec_bind_message(StringInfo input_message) oldcxt = MemoryContextSwitchTo(MessageContext); if (knownTextValues == NULL) - knownTextValues = - palloc0(numParams * sizeof(char *)); + knownTextValues = palloc0_array(char *, numParams); if (log_parameter_max_length_on_error < 0) knownTextValues[paramno] = pstrdup(pstring); @@ -1958,7 +1957,7 @@ exec_bind_message(StringInfo input_message) numRFormats = pq_getmsgint(input_message, 2); if (numRFormats > 0) { - rformats = (int16 *) palloc(numRFormats * sizeof(int16)); + rformats = palloc_array(int16, numRFormats); for (int i = 0; i < numRFormats; i++) rformats[i] = pq_getmsgint(input_message, 2); } @@ -4517,7 +4516,7 @@ PostgresMain(const char *dbname, const char *username) numParams = pq_getmsgint(&input_message, 2); if (numParams > 0) { - paramTypes = (Oid *) palloc(numParams * sizeof(Oid)); + paramTypes = palloc_array(Oid, numParams); for (int i = 0; i < numParams; i++) paramTypes[i] = pq_getmsgint(&input_message, 4); } -- cgit v1.2.3