Remove redundant null pointer checks before PQclear and PQconninfoFree
authorPeter Eisentraut <peter@eisentraut.org>
Sun, 3 Jul 2022 18:11:05 +0000 (20:11 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Sun, 3 Jul 2022 18:11:05 +0000 (20:11 +0200)
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

13 files changed:
contrib/dblink/dblink.c
contrib/postgres_fdw/postgres_fdw.c
doc/src/sgml/libpq.sgml
src/bin/pg_basebackup/streamutil.c
src/bin/pg_dump/pg_dumpall.c
src/bin/psql/command.c
src/bin/psql/common.c
src/bin/psql/describe.c
src/fe_utils/query_utils.c
src/interfaces/ecpg/ecpglib/descriptor.c
src/interfaces/ecpg/ecpglib/execute.c
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/fe-exec.c

index a561d1d65244c2f8f21fc521ec884b4a02dae05f..e323fdd0e67ea354e08450c0c1afb247486cc1ef 100644 (file)
@@ -157,8 +157,7 @@ dblink_res_internalerror(PGconn *conn, PGresult *res, const char *p2)
 {
        char       *msg = pchomp(PQerrorMessage(conn));
 
-       if (res)
-               PQclear(res);
+       PQclear(res);
        elog(ERROR, "%s: %s", p2, msg);
 }
 
@@ -2756,8 +2755,7 @@ dblink_res_error(PGconn *conn, const char *conname, PGresult *res,
         * leaking all the strings too, but those are in palloc'd memory that will
         * get cleaned up eventually.
         */
-       if (res)
-               PQclear(res);
+       PQclear(res);
 
        /*
         * Format the basic errcontext string.  Below, we'll add on something
index d56951153bb93f6a569aaf89e92a2e8db6ad5199..955a428e3dab0b1b8fc4a6b9c552460170663a28 100644 (file)
@@ -2790,8 +2790,7 @@ postgresEndDirectModify(ForeignScanState *node)
                return;
 
        /* Release PGresult */
-       if (dmstate->result)
-               PQclear(dmstate->result);
+       PQclear(dmstate->result);
 
        /* Release remote connection */
        ReleaseConnection(dmstate->conn);
@@ -3604,8 +3603,7 @@ get_remote_estimate(const char *sql, PGconn *conn,
        }
        PG_FINALLY();
        {
-               if (res)
-                       PQclear(res);
+               PQclear(res);
        }
        PG_END_TRY();
 }
@@ -3853,8 +3851,7 @@ fetch_more_data(ForeignScanState *node)
        }
        PG_FINALLY();
        {
-               if (res)
-                       PQclear(res);
+               PQclear(res);
        }
        PG_END_TRY();
 
@@ -4338,8 +4335,7 @@ store_returning_result(PgFdwModifyState *fmstate,
        }
        PG_CATCH();
        {
-               if (res)
-                       PQclear(res);
+               PQclear(res);
                PG_RE_THROW();
        }
        PG_END_TRY();
@@ -4627,8 +4623,7 @@ get_returning_data(ForeignScanState *node)
                }
                PG_CATCH();
                {
-                       if (dmstate->result)
-                               PQclear(dmstate->result);
+                       PQclear(dmstate->result);
                        PG_RE_THROW();
                }
                PG_END_TRY();
@@ -4957,8 +4952,7 @@ postgresAnalyzeForeignTable(Relation relation,
        }
        PG_FINALLY();
        {
-               if (res)
-                       PQclear(res);
+               PQclear(res);
        }
        PG_END_TRY();
 
@@ -5114,8 +5108,7 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
        }
        PG_CATCH();
        {
-               if (res)
-                       PQclear(res);
+               PQclear(res);
                PG_RE_THROW();
        }
        PG_END_TRY();
@@ -5496,8 +5489,7 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
        }
        PG_FINALLY();
        {
-               if (res)
-                       PQclear(res);
+               PQclear(res);
        }
        PG_END_TRY();
 
index 37ec3cb4e5f0f9c6e2772775b7f92f644b37b173..74456aa69d73d47bfae377e7fb51fb7595bd708f 100644 (file)
@@ -3628,6 +3628,9 @@ char *PQresultErrorField(const PGresult *res, int fieldcode);
 <synopsis>
 void PQclear(PGresult *res);
 </synopsis>
+
+        If the argument is a <symbol>NULL</symbol> pointer, no operation is
+        performed.
        </para>
 
        <para>
@@ -6670,6 +6673,8 @@ void PQfreemem(void *ptr);
 <synopsis>
 void PQconninfoFree(PQconninfoOption *connOptions);
 </synopsis>
+      If the argument is a <symbol>NULL</symbol> pointer, no operation is
+      performed.
      </para>
 
      <para>
index 8e820c0c71cb81cb9c801742218c6d4b5cb711e1..919ec9c29c2d599cc7c6511e1de28f1b346e580a 100644 (file)
@@ -197,16 +197,14 @@ GetConnection(void)
                PQfinish(tmpconn);
                free(values);
                free(keywords);
-               if (conn_opts)
-                       PQconninfoFree(conn_opts);
+               PQconninfoFree(conn_opts);
                return NULL;
        }
 
        /* Connection ok! */
        free(values);
        free(keywords);
-       if (conn_opts)
-               PQconninfoFree(conn_opts);
+       PQconninfoFree(conn_opts);
 
        /*
         * Set always-secure search path, so malicious users can't get control.
index da5cf85272c305cb12ea65c9cb7bf3b490d13b36..26d3d53809ba46ae1d1f76ccb4783e856ed3f79e 100644 (file)
@@ -1502,8 +1502,7 @@ connectDatabase(const char *dbname, const char *connection_string,
 
                free(keywords);
                free(values);
-               if (conn_opts)
-                       PQconninfoFree(conn_opts);
+               PQconninfoFree(conn_opts);
 
                /*
                 * Merge the connection info inputs given in form of connection string
index f3c5196c9018dbe2bb78ba3ac792aea3a097b0a1..c562c04afee5e9812ca84d71ea5559e1a70ab3ce 100644 (file)
@@ -3476,8 +3476,7 @@ do_connect(enum trivalue reuse_previous_specification,
 
        /* Release locally allocated data, whether we succeeded or not */
        pg_free(password);
-       if (cinfo)
-               PQconninfoFree(cinfo);
+       PQconninfoFree(cinfo);
 
        if (!success)
        {
index 974959c5959e0bd6b894f754b7e2c8170389ed68..9f95869eca6bc07c2973c8736570dd86954748ca 100644 (file)
@@ -463,8 +463,7 @@ ClearOrSaveResult(PGresult *result)
                {
                        case PGRES_NONFATAL_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;
                                break;
 
index 2df95bc16548b3381f60c022f6177b8974e5f595..88d92a08ae660817b28f5e86f838d2529bc10596 100644 (file)
@@ -3492,8 +3492,7 @@ error_return:
 
        free(view_def);
 
-       if (res)
-               PQclear(res);
+       PQclear(res);
 
        return retval;
 }
index 2fc6e2405b48789ad62076eeb031cf0497e13ae1..6575b24c78f6d242aa7d9466b6e9d3a7a9fa7cea 100644 (file)
@@ -85,8 +85,7 @@ executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
 
        r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
 
-       if (res)
-               PQclear(res);
+       PQclear(res);
 
        return r;
 }
index f1898dec6a6e35d054eba0d52e9230fe7e17188a..649a71c286ca2ff6ee59cf391f97c6a47fc755c1 100644 (file)
@@ -923,8 +923,7 @@ ECPGdescribe(int line, int compat, bool input, const char *connection_name, cons
                                        if (!ecpg_check_PQresult(res, line, con->connection, compat))
                                                break;
 
-                                       if (desc->result != NULL)
-                                               PQclear(desc->result);
+                                       PQclear(desc->result);
 
                                        desc->result = res;
                                        ret = true;
index 2ebe6656d6382b15c51f1305b5fbb58ad1431b3b..bd94bd4e6c6cda15cc3a57ce37bdc0692ac9fe4d 100644 (file)
@@ -1714,8 +1714,7 @@ ecpg_process_output(struct statement *stmt, bool clear_result)
                                        status = false;
                                else
                                {
-                                       if (desc->result)
-                                               PQclear(desc->result);
+                                       PQclear(desc->result);
                                        desc->result = stmt->results;
                                        clear_result = false;
                                        ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
index 057c9da0edeba7d8c9206a0e5d57c3fa6f121d7f..dc49387d6c5c64c3bab56ab959843fa3042de065 100644 (file)
@@ -3766,8 +3766,7 @@ keep_going:                                               /* We will come back to here until there is
                                }
 
                                /* Something went wrong with "SHOW transaction_read_only". */
-                               if (res)
-                                       PQclear(res);
+                               PQclear(res);
 
                                /* Append error report to conn->errorMessage. */
                                appendPQExpBuffer(&conn->errorMessage,
@@ -3818,8 +3817,7 @@ keep_going:                                               /* We will come back to here until there is
                                }
 
                                /* Something went wrong with "SELECT pg_is_in_recovery()". */
-                               if (res)
-                                       PQclear(res);
+                               PQclear(res);
 
                                /* Append error report to conn->errorMessage. */
                                appendPQExpBuffer(&conn->errorMessage,
index 1750d647a8dbeb89fd2fd8d7c7879baa5bbe1f4c..51e9a362f6a3667ffc2bcce715f5a6299a781d48 100644 (file)
@@ -775,12 +775,10 @@ PQclear(PGresult *res)
 void
 pqClearAsyncResult(PGconn *conn)
 {
-       if (conn->result)
-               PQclear(conn->result);
+       PQclear(conn->result);
        conn->result = NULL;
        conn->error_result = false;
-       if (conn->next_result)
-               PQclear(conn->next_result);
+       PQclear(conn->next_result);
        conn->next_result = NULL;
 }
 
@@ -2437,8 +2435,7 @@ PQexecFinish(PGconn *conn)
        lastResult = NULL;
        while ((result = PQgetResult(conn)) != NULL)
        {
-               if (lastResult)
-                       PQclear(lastResult);
+               PQclear(lastResult);
                lastResult = result;
                if (result->resultStatus == PGRES_COPY_IN ||
                        result->resultStatus == PGRES_COPY_OUT ||