Skip to content

Commit 5faef9d

Browse files
committed
Remove redundant null pointer checks before PQclear and PQconninfoFree
These functions already had the free()-like behavior of handling null pointers as a no-op. But it wasn't documented, so add it explicitly to the documentation, too. Discussion: https://www.postgresql.org/message-id/flat/dac5d2d0-98f5-94d9-8e69-46da2413593d%40enterprisedb.com
1 parent 02c408e commit 5faef9d

File tree

13 files changed

+29
-48
lines changed

13 files changed

+29
-48
lines changed

contrib/dblink/dblink.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ dblink_res_internalerror(PGconn *conn, PGresult *res, const char *p2)
157157
{
158158
char *msg = pchomp(PQerrorMessage(conn));
159159

160-
if (res)
161-
PQclear(res);
160+
PQclear(res);
162161
elog(ERROR, "%s: %s", p2, msg);
163162
}
164163

@@ -2756,8 +2755,7 @@ dblink_res_error(PGconn *conn, const char *conname, PGresult *res,
27562755
* leaking all the strings too, but those are in palloc'd memory that will
27572756
* get cleaned up eventually.
27582757
*/
2759-
if (res)
2760-
PQclear(res);
2758+
PQclear(res);
27612759

27622760
/*
27632761
* Format the basic errcontext string. Below, we'll add on something

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,8 +2790,7 @@ postgresEndDirectModify(ForeignScanState *node)
27902790
return;
27912791

27922792
/* Release PGresult */
2793-
if (dmstate->result)
2794-
PQclear(dmstate->result);
2793+
PQclear(dmstate->result);
27952794

27962795
/* Release remote connection */
27972796
ReleaseConnection(dmstate->conn);
@@ -3604,8 +3603,7 @@ get_remote_estimate(const char *sql, PGconn *conn,
36043603
}
36053604
PG_FINALLY();
36063605
{
3607-
if (res)
3608-
PQclear(res);
3606+
PQclear(res);
36093607
}
36103608
PG_END_TRY();
36113609
}
@@ -3853,8 +3851,7 @@ fetch_more_data(ForeignScanState *node)
38533851
}
38543852
PG_FINALLY();
38553853
{
3856-
if (res)
3857-
PQclear(res);
3854+
PQclear(res);
38583855
}
38593856
PG_END_TRY();
38603857

@@ -4338,8 +4335,7 @@ store_returning_result(PgFdwModifyState *fmstate,
43384335
}
43394336
PG_CATCH();
43404337
{
4341-
if (res)
4342-
PQclear(res);
4338+
PQclear(res);
43434339
PG_RE_THROW();
43444340
}
43454341
PG_END_TRY();
@@ -4627,8 +4623,7 @@ get_returning_data(ForeignScanState *node)
46274623
}
46284624
PG_CATCH();
46294625
{
4630-
if (dmstate->result)
4631-
PQclear(dmstate->result);
4626+
PQclear(dmstate->result);
46324627
PG_RE_THROW();
46334628
}
46344629
PG_END_TRY();
@@ -4957,8 +4952,7 @@ postgresAnalyzeForeignTable(Relation relation,
49574952
}
49584953
PG_FINALLY();
49594954
{
4960-
if (res)
4961-
PQclear(res);
4955+
PQclear(res);
49624956
}
49634957
PG_END_TRY();
49644958

@@ -5114,8 +5108,7 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
51145108
}
51155109
PG_CATCH();
51165110
{
5117-
if (res)
5118-
PQclear(res);
5111+
PQclear(res);
51195112
PG_RE_THROW();
51205113
}
51215114
PG_END_TRY();
@@ -5496,8 +5489,7 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
54965489
}
54975490
PG_FINALLY();
54985491
{
5499-
if (res)
5500-
PQclear(res);
5492+
PQclear(res);
55015493
}
55025494
PG_END_TRY();
55035495

doc/src/sgml/libpq.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,6 +3628,9 @@ char *PQresultErrorField(const PGresult *res, int fieldcode);
36283628
<synopsis>
36293629
void PQclear(PGresult *res);
36303630
</synopsis>
3631+
3632+
If the argument is a <symbol>NULL</symbol> pointer, no operation is
3633+
performed.
36313634
</para>
36323635

36333636
<para>
@@ -6670,6 +6673,8 @@ void PQfreemem(void *ptr);
66706673
<synopsis>
66716674
void PQconninfoFree(PQconninfoOption *connOptions);
66726675
</synopsis>
6676+
If the argument is a <symbol>NULL</symbol> pointer, no operation is
6677+
performed.
66736678
</para>
66746679

66756680
<para>

src/bin/pg_basebackup/streamutil.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,14 @@ GetConnection(void)
197197
PQfinish(tmpconn);
198198
free(values);
199199
free(keywords);
200-
if (conn_opts)
201-
PQconninfoFree(conn_opts);
200+
PQconninfoFree(conn_opts);
202201
return NULL;
203202
}
204203

205204
/* Connection ok! */
206205
free(values);
207206
free(keywords);
208-
if (conn_opts)
209-
PQconninfoFree(conn_opts);
207+
PQconninfoFree(conn_opts);
210208

211209
/*
212210
* Set always-secure search path, so malicious users can't get control.

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,7 @@ connectDatabase(const char *dbname, const char *connection_string,
15021502

15031503
free(keywords);
15041504
free(values);
1505-
if (conn_opts)
1506-
PQconninfoFree(conn_opts);
1505+
PQconninfoFree(conn_opts);
15071506

15081507
/*
15091508
* Merge the connection info inputs given in form of connection string

src/bin/psql/command.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3476,8 +3476,7 @@ do_connect(enum trivalue reuse_previous_specification,
34763476

34773477
/* Release locally allocated data, whether we succeeded or not */
34783478
pg_free(password);
3479-
if (cinfo)
3480-
PQconninfoFree(cinfo);
3479+
PQconninfoFree(cinfo);
34813480

34823481
if (!success)
34833482
{

src/bin/psql/common.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,7 @@ ClearOrSaveResult(PGresult *result)
463463
{
464464
case PGRES_NONFATAL_ERROR:
465465
case PGRES_FATAL_ERROR:
466-
if (pset.last_error_result)
467-
PQclear(pset.last_error_result);
466+
PQclear(pset.last_error_result);
468467
pset.last_error_result = result;
469468
break;
470469

src/bin/psql/describe.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,8 +3492,7 @@ describeOneTableDetails(const char *schemaname,
34923492

34933493
free(view_def);
34943494

3495-
if (res)
3496-
PQclear(res);
3495+
PQclear(res);
34973496

34983497
return retval;
34993498
}

src/fe_utils/query_utils.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
8585

8686
r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
8787

88-
if (res)
89-
PQclear(res);
88+
PQclear(res);
9089

9190
return r;
9291
}

src/interfaces/ecpg/ecpglib/descriptor.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,7 @@ ECPGdescribe(int line, int compat, bool input, const char *connection_name, cons
923923
if (!ecpg_check_PQresult(res, line, con->connection, compat))
924924
break;
925925

926-
if (desc->result != NULL)
927-
PQclear(desc->result);
926+
PQclear(desc->result);
928927

929928
desc->result = res;
930929
ret = true;

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,8 +1714,7 @@ ecpg_process_output(struct statement *stmt, bool clear_result)
17141714
status = false;
17151715
else
17161716
{
1717-
if (desc->result)
1718-
PQclear(desc->result);
1717+
PQclear(desc->result);
17191718
desc->result = stmt->results;
17201719
clear_result = false;
17211720
ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",

src/interfaces/libpq/fe-connect.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3766,8 +3766,7 @@ PQconnectPoll(PGconn *conn)
37663766
}
37673767

37683768
/* Something went wrong with "SHOW transaction_read_only". */
3769-
if (res)
3770-
PQclear(res);
3769+
PQclear(res);
37713770

37723771
/* Append error report to conn->errorMessage. */
37733772
appendPQExpBuffer(&conn->errorMessage,
@@ -3818,8 +3817,7 @@ PQconnectPoll(PGconn *conn)
38183817
}
38193818

38203819
/* Something went wrong with "SELECT pg_is_in_recovery()". */
3821-
if (res)
3822-
PQclear(res);
3820+
PQclear(res);
38233821

38243822
/* Append error report to conn->errorMessage. */
38253823
appendPQExpBuffer(&conn->errorMessage,

src/interfaces/libpq/fe-exec.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,10 @@ PQclear(PGresult *res)
775775
void
776776
pqClearAsyncResult(PGconn *conn)
777777
{
778-
if (conn->result)
779-
PQclear(conn->result);
778+
PQclear(conn->result);
780779
conn->result = NULL;
781780
conn->error_result = false;
782-
if (conn->next_result)
783-
PQclear(conn->next_result);
781+
PQclear(conn->next_result);
784782
conn->next_result = NULL;
785783
}
786784

@@ -2437,8 +2435,7 @@ PQexecFinish(PGconn *conn)
24372435
lastResult = NULL;
24382436
while ((result = PQgetResult(conn)) != NULL)
24392437
{
2440-
if (lastResult)
2441-
PQclear(lastResult);
2438+
PQclear(lastResult);
24422439
lastResult = result;
24432440
if (result->resultStatus == PGRES_COPY_IN ||
24442441
result->resultStatus == PGRES_COPY_OUT ||

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy