1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

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
This commit is contained in:
Peter Eisentraut
2022-07-03 20:11:05 +02:00
parent 02c408e21a
commit 5faef9d582
13 changed files with 29 additions and 48 deletions

View File

@ -157,7 +157,6 @@ dblink_res_internalerror(PGconn *conn, PGresult *res, const char *p2)
{ {
char *msg = pchomp(PQerrorMessage(conn)); char *msg = pchomp(PQerrorMessage(conn));
if (res)
PQclear(res); PQclear(res);
elog(ERROR, "%s: %s", p2, msg); elog(ERROR, "%s: %s", p2, msg);
} }
@ -2756,7 +2755,6 @@ dblink_res_error(PGconn *conn, const char *conname, PGresult *res,
* leaking all the strings too, but those are in palloc'd memory that will * leaking all the strings too, but those are in palloc'd memory that will
* get cleaned up eventually. * get cleaned up eventually.
*/ */
if (res)
PQclear(res); PQclear(res);
/* /*

View File

@ -2790,7 +2790,6 @@ postgresEndDirectModify(ForeignScanState *node)
return; return;
/* Release PGresult */ /* Release PGresult */
if (dmstate->result)
PQclear(dmstate->result); PQclear(dmstate->result);
/* Release remote connection */ /* Release remote connection */
@ -3604,7 +3603,6 @@ get_remote_estimate(const char *sql, PGconn *conn,
} }
PG_FINALLY(); PG_FINALLY();
{ {
if (res)
PQclear(res); PQclear(res);
} }
PG_END_TRY(); PG_END_TRY();
@ -3853,7 +3851,6 @@ fetch_more_data(ForeignScanState *node)
} }
PG_FINALLY(); PG_FINALLY();
{ {
if (res)
PQclear(res); PQclear(res);
} }
PG_END_TRY(); PG_END_TRY();
@ -4338,7 +4335,6 @@ store_returning_result(PgFdwModifyState *fmstate,
} }
PG_CATCH(); PG_CATCH();
{ {
if (res)
PQclear(res); PQclear(res);
PG_RE_THROW(); PG_RE_THROW();
} }
@ -4627,7 +4623,6 @@ get_returning_data(ForeignScanState *node)
} }
PG_CATCH(); PG_CATCH();
{ {
if (dmstate->result)
PQclear(dmstate->result); PQclear(dmstate->result);
PG_RE_THROW(); PG_RE_THROW();
} }
@ -4957,7 +4952,6 @@ postgresAnalyzeForeignTable(Relation relation,
} }
PG_FINALLY(); PG_FINALLY();
{ {
if (res)
PQclear(res); PQclear(res);
} }
PG_END_TRY(); PG_END_TRY();
@ -5114,7 +5108,6 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
} }
PG_CATCH(); PG_CATCH();
{ {
if (res)
PQclear(res); PQclear(res);
PG_RE_THROW(); PG_RE_THROW();
} }
@ -5496,7 +5489,6 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
} }
PG_FINALLY(); PG_FINALLY();
{ {
if (res)
PQclear(res); PQclear(res);
} }
PG_END_TRY(); PG_END_TRY();

View File

@ -3628,6 +3628,9 @@ char *PQresultErrorField(const PGresult *res, int fieldcode);
<synopsis> <synopsis>
void PQclear(PGresult *res); void PQclear(PGresult *res);
</synopsis> </synopsis>
If the argument is a <symbol>NULL</symbol> pointer, no operation is
performed.
</para> </para>
<para> <para>
@ -6670,6 +6673,8 @@ void PQfreemem(void *ptr);
<synopsis> <synopsis>
void PQconninfoFree(PQconninfoOption *connOptions); void PQconninfoFree(PQconninfoOption *connOptions);
</synopsis> </synopsis>
If the argument is a <symbol>NULL</symbol> pointer, no operation is
performed.
</para> </para>
<para> <para>

View File

@ -197,7 +197,6 @@ GetConnection(void)
PQfinish(tmpconn); PQfinish(tmpconn);
free(values); free(values);
free(keywords); free(keywords);
if (conn_opts)
PQconninfoFree(conn_opts); PQconninfoFree(conn_opts);
return NULL; return NULL;
} }
@ -205,7 +204,6 @@ GetConnection(void)
/* Connection ok! */ /* Connection ok! */
free(values); free(values);
free(keywords); free(keywords);
if (conn_opts)
PQconninfoFree(conn_opts); PQconninfoFree(conn_opts);
/* /*

View File

@ -1502,7 +1502,6 @@ connectDatabase(const char *dbname, const char *connection_string,
free(keywords); free(keywords);
free(values); free(values);
if (conn_opts)
PQconninfoFree(conn_opts); PQconninfoFree(conn_opts);
/* /*

View File

@ -3476,7 +3476,6 @@ do_connect(enum trivalue reuse_previous_specification,
/* Release locally allocated data, whether we succeeded or not */ /* Release locally allocated data, whether we succeeded or not */
pg_free(password); pg_free(password);
if (cinfo)
PQconninfoFree(cinfo); PQconninfoFree(cinfo);
if (!success) if (!success)

View File

@ -463,7 +463,6 @@ ClearOrSaveResult(PGresult *result)
{ {
case PGRES_NONFATAL_ERROR: case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR: case PGRES_FATAL_ERROR:
if (pset.last_error_result)
PQclear(pset.last_error_result); PQclear(pset.last_error_result);
pset.last_error_result = result; pset.last_error_result = result;
break; break;

View File

@ -3492,7 +3492,6 @@ error_return:
free(view_def); free(view_def);
if (res)
PQclear(res); PQclear(res);
return retval; return retval;

View File

@ -85,7 +85,6 @@ executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
r = (res && PQresultStatus(res) == PGRES_COMMAND_OK); r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
if (res)
PQclear(res); PQclear(res);
return r; return r;

View File

@ -923,7 +923,6 @@ ECPGdescribe(int line, int compat, bool input, const char *connection_name, cons
if (!ecpg_check_PQresult(res, line, con->connection, compat)) if (!ecpg_check_PQresult(res, line, con->connection, compat))
break; break;
if (desc->result != NULL)
PQclear(desc->result); PQclear(desc->result);
desc->result = res; desc->result = res;

View File

@ -1714,7 +1714,6 @@ ecpg_process_output(struct statement *stmt, bool clear_result)
status = false; status = false;
else else
{ {
if (desc->result)
PQclear(desc->result); PQclear(desc->result);
desc->result = stmt->results; desc->result = stmt->results;
clear_result = false; clear_result = false;

View File

@ -3766,7 +3766,6 @@ keep_going: /* We will come back to here until there is
} }
/* Something went wrong with "SHOW transaction_read_only". */ /* Something went wrong with "SHOW transaction_read_only". */
if (res)
PQclear(res); PQclear(res);
/* Append error report to conn->errorMessage. */ /* Append error report to conn->errorMessage. */
@ -3818,7 +3817,6 @@ keep_going: /* We will come back to here until there is
} }
/* Something went wrong with "SELECT pg_is_in_recovery()". */ /* Something went wrong with "SELECT pg_is_in_recovery()". */
if (res)
PQclear(res); PQclear(res);
/* Append error report to conn->errorMessage. */ /* Append error report to conn->errorMessage. */

View File

@ -775,11 +775,9 @@ PQclear(PGresult *res)
void void
pqClearAsyncResult(PGconn *conn) pqClearAsyncResult(PGconn *conn)
{ {
if (conn->result)
PQclear(conn->result); PQclear(conn->result);
conn->result = NULL; conn->result = NULL;
conn->error_result = false; conn->error_result = false;
if (conn->next_result)
PQclear(conn->next_result); PQclear(conn->next_result);
conn->next_result = NULL; conn->next_result = NULL;
} }
@ -2437,7 +2435,6 @@ PQexecFinish(PGconn *conn)
lastResult = NULL; lastResult = NULL;
while ((result = PQgetResult(conn)) != NULL) while ((result = PQgetResult(conn)) != NULL)
{ {
if (lastResult)
PQclear(lastResult); PQclear(lastResult);
lastResult = result; lastResult = result;
if (result->resultStatus == PGRES_COPY_IN || if (result->resultStatus == PGRES_COPY_IN ||