Remove redundant null pointer checks before pg_free()
authorPeter Eisentraut <peter@eisentraut.org>
Fri, 17 Jun 2022 09:51:38 +0000 (11:51 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Sun, 3 Jul 2022 07:05:34 +0000 (09:05 +0200)
These are especially useless because the whole point of pg_free() was
to do that very check before calling free().

pg_free() could be removed altogether, but I'm keeping it here to keep
the API consistent.

Discussion: https://www.postgresql.org/message-id/flat/dac5d2d0-98f5-94d9-8e69-46da2413593d%40enterprisedb.com

src/bin/pg_basebackup/walmethods.c
src/bin/pg_upgrade/parallel.c
src/bin/pgbench/pgbench.c
src/bin/psql/command.c
src/bin/psql/variables.c

index cc292718da922b6602b4c0937f9df5fdce78abc6..ef4c11277ac8d2387391ec2f08cbb5edd86b4122 100644 (file)
@@ -500,8 +500,7 @@ dir_close(Walfile f, WalCloseMethod method)
 
        pg_free(df->pathname);
        pg_free(df->fullpath);
-       if (df->temp_suffix)
-               pg_free(df->temp_suffix);
+       pg_free(df->temp_suffix);
        pg_free(df);
 
        return r;
index ca40df7b4c7834cf577890c8452f0b2e58ccc916..5a3df84f01938bdd5098446d25a59befa027e8e9 100644 (file)
@@ -130,14 +130,11 @@ parallel_exec_prog(const char *log_file, const char *opt_log_file,
                new_arg = exec_thread_args[parallel_jobs - 1];
 
                /* Can only pass one pointer into the function, so use a struct */
-               if (new_arg->log_file)
-                       pg_free(new_arg->log_file);
+               pg_free(new_arg->log_file);
                new_arg->log_file = pg_strdup(log_file);
-               if (new_arg->opt_log_file)
-                       pg_free(new_arg->opt_log_file);
+               pg_free(new_arg->opt_log_file);
                new_arg->opt_log_file = opt_log_file ? pg_strdup(opt_log_file) : NULL;
-               if (new_arg->cmd)
-                       pg_free(new_arg->cmd);
+               pg_free(new_arg->cmd);
                new_arg->cmd = pg_strdup(cmd);
 
                child = (HANDLE) _beginthreadex(NULL, 0, (void *) win32_exec_prog,
@@ -243,14 +240,11 @@ parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
                /* Can only pass one pointer into the function, so use a struct */
                new_arg->old_db_arr = old_db_arr;
                new_arg->new_db_arr = new_db_arr;
-               if (new_arg->old_pgdata)
-                       pg_free(new_arg->old_pgdata);
+               pg_free(new_arg->old_pgdata);
                new_arg->old_pgdata = pg_strdup(old_pgdata);
-               if (new_arg->new_pgdata)
-                       pg_free(new_arg->new_pgdata);
+               pg_free(new_arg->new_pgdata);
                new_arg->new_pgdata = pg_strdup(new_pgdata);
-               if (new_arg->old_tablespace)
-                       pg_free(new_arg->old_tablespace);
+               pg_free(new_arg->old_tablespace);
                new_arg->old_tablespace = old_tablespace ? pg_strdup(old_tablespace) : NULL;
 
                child = (HANDLE) _beginthreadex(NULL, 0, (void *) win32_transfer_all_new_dbs,
index fbb74bdc4c4ced61e0c4f6fd4a70ff161e0dd17f..89b59a4ff6685d52a254665373fd817199c8672a 100644 (file)
@@ -5475,12 +5475,10 @@ static void
 free_command(Command *command)
 {
        termPQExpBuffer(&command->lines);
-       if (command->first_line)
-               pg_free(command->first_line);
+       pg_free(command->first_line);
        for (int i = 0; i < command->argc; i++)
                pg_free(command->argv[i]);
-       if (command->varprefix)
-               pg_free(command->varprefix);
+       pg_free(command->varprefix);
 
        /*
         * It should also free expr recursively, but this is currently not needed
@@ -6637,8 +6635,7 @@ main(int argc, char **argv)
                                is_init_mode = true;
                                break;
                        case 'I':
-                               if (initialize_steps)
-                                       pg_free(initialize_steps);
+                               pg_free(initialize_steps);
                                initialize_steps = pg_strdup(optarg);
                                checkInitSteps(initialize_steps);
                                initialization_option_set = true;
index b51d28780b160b0589a4124aca6fe1b434bddaf0..653943b87a63234802cf2053d76c9b64c5033885 100644 (file)
@@ -3492,8 +3492,7 @@ do_connect(enum trivalue reuse_previous_specification,
        }                                                       /* end retry loop */
 
        /* Release locally allocated data, whether we succeeded or not */
-       if (password)
-               pg_free(password);
+       pg_free(password);
        if (cinfo)
                PQconninfoFree(cinfo);
 
index 47c58d2be9df21c8f4935ebc78152b8acd98a2c9..5cc4bc0cb785d5be72f5444cd1e9dcc6828c18ed 100644 (file)
@@ -255,8 +255,7 @@ SetVariable(VariableSpace space, const char *name, const char *value)
 
                        if (confirmed)
                        {
-                               if (current->value)
-                                       pg_free(current->value);
+                               pg_free(current->value);
                                current->value = new_value;
 
                                /*
@@ -272,7 +271,7 @@ SetVariable(VariableSpace space, const char *name, const char *value)
                                        free(current);
                                }
                        }
-                       else if (new_value)
+                       else
                                pg_free(new_value); /* current->value is left unchanged */
 
                        return confirmed;