diff options
| author | Peter Eisentraut | 2022-06-16 19:50:56 +0000 |
|---|---|---|
| committer | Peter Eisentraut | 2022-07-03 09:47:15 +0000 |
| commit | 02c408e21a6e78ff246ea7a1beb4669634fa9c4c (patch) | |
| tree | 7e4cf03f3de7e32af266b739e12c6600d871ed45 /src/bin | |
| parent | 098c703d308fa88dc9e3f9f623ca023ce4717794 (diff) | |
Remove redundant null pointer checks before free()
Per applicable standards, free() with a null pointer is a no-op.
Systems that don't observe that are ancient and no longer relevant.
Some PostgreSQL code already required this behavior, so this change
does not introduce any new requirements, just makes the code more
consistent.
Discussion: https://www.postgresql.org/message-id/flat/dac5d2d0-98f5-94d9-8e69-46da2413593d%40enterprisedb.com
Diffstat (limited to 'src/bin')
| -rw-r--r-- | src/bin/pg_basebackup/pg_basebackup.c | 3 | ||||
| -rw-r--r-- | src/bin/pg_basebackup/streamutil.c | 3 | ||||
| -rw-r--r-- | src/bin/pg_dump/dumputils.c | 21 | ||||
| -rw-r--r-- | src/bin/pg_dump/pg_backup_archiver.c | 60 | ||||
| -rw-r--r-- | src/bin/pg_dump/pg_backup_custom.c | 6 | ||||
| -rw-r--r-- | src/bin/pg_dump/pg_backup_db.c | 3 | ||||
| -rw-r--r-- | src/bin/pg_dump/pg_backup_tar.c | 3 | ||||
| -rw-r--r-- | src/bin/pg_dump/pg_dump.c | 30 | ||||
| -rw-r--r-- | src/bin/pg_dump/pg_dumpall.c | 6 | ||||
| -rw-r--r-- | src/bin/pgbench/pgbench.c | 6 | ||||
| -rw-r--r-- | src/bin/psql/command.c | 66 | ||||
| -rw-r--r-- | src/bin/psql/copy.c | 3 | ||||
| -rw-r--r-- | src/bin/psql/describe.c | 6 | ||||
| -rw-r--r-- | src/bin/psql/input.c | 3 | ||||
| -rw-r--r-- | src/bin/psql/tab-complete.c | 15 |
15 files changed, 78 insertions, 156 deletions
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 4adb170d464..b66eac707a4 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -775,8 +775,7 @@ progress_update_filename(const char *filename) /* We needn't maintain this variable if not doing verbose reports. */ if (showprogress && verbose) { - if (progress_filename) - free(progress_filename); + free(progress_filename); if (filename) progress_filename = pg_strdup(filename); else diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 299b9b76213..8e820c0c71c 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -154,8 +154,7 @@ GetConnection(void) /* Get a new password if appropriate */ if (need_password) { - if (password) - free(password); + free(password); password = simple_prompt("Password: ", false); need_password = false; } diff --git a/src/bin/pg_dump/dumputils.c b/src/bin/pg_dump/dumputils.c index 3e68dfc78f9..6e501a54138 100644 --- a/src/bin/pg_dump/dumputils.c +++ b/src/bin/pg_dump/dumputils.c @@ -98,18 +98,15 @@ buildACLCommands(const char *name, const char *subname, const char *nspname, /* Parse the acls array */ if (!parsePGArray(acls, &aclitems, &naclitems)) { - if (aclitems) - free(aclitems); + free(aclitems); return false; } /* Parse the baseacls too */ if (!parsePGArray(baseacls, &baseitems, &nbaseitems)) { - if (aclitems) - free(aclitems); - if (baseitems) - free(baseitems); + free(aclitems); + free(baseitems); return false; } @@ -298,14 +295,10 @@ buildACLCommands(const char *name, const char *subname, const char *nspname, destroyPQExpBuffer(firstsql); destroyPQExpBuffer(secondsql); - if (aclitems) - free(aclitems); - if (baseitems) - free(baseitems); - if (grantitems) - free(grantitems); - if (revokeitems) - free(revokeitems); + free(aclitems); + free(baseitems); + free(grantitems); + free(revokeitems); return ok; } diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 77fe51a3a53..233198afc0c 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -640,8 +640,7 @@ RestoreArchive(Archive *AHX) * If we treated users as pg_dump'able objects then we'd need to reset * currUser here too. */ - if (AH->currSchema) - free(AH->currSchema); + free(AH->currSchema); AH->currSchema = NULL; } @@ -2067,8 +2066,7 @@ _discoverArchiveFormat(ArchiveHandle *AH) pg_log_debug("attempting to ascertain archive format"); - if (AH->lookahead) - free(AH->lookahead); + free(AH->lookahead); AH->readHeader = 0; AH->lookaheadSize = 512; @@ -3178,21 +3176,17 @@ _reconnectToDB(ArchiveHandle *AH, const char *dbname) * NOTE: currUser keeps track of what the imaginary session user in our * script is. It's now effectively reset to the original userID. */ - if (AH->currUser) - free(AH->currUser); + free(AH->currUser); AH->currUser = NULL; /* don't assume we still know the output schema, tablespace, etc either */ - if (AH->currSchema) - free(AH->currSchema); + free(AH->currSchema); AH->currSchema = NULL; - if (AH->currTableAm) - free(AH->currTableAm); + free(AH->currTableAm); AH->currTableAm = NULL; - if (AH->currTablespace) - free(AH->currTablespace); + free(AH->currTablespace); AH->currTablespace = NULL; /* re-establish fixed state */ @@ -3219,8 +3213,7 @@ _becomeUser(ArchiveHandle *AH, const char *user) * NOTE: currUser keeps track of what the imaginary session user in our * script is */ - if (AH->currUser) - free(AH->currUser); + free(AH->currUser); AH->currUser = pg_strdup(user); } @@ -3285,8 +3278,7 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName) else ahprintf(AH, "%s;\n\n", qry->data); - if (AH->currSchema) - free(AH->currSchema); + free(AH->currSchema); AH->currSchema = pg_strdup(schemaName); destroyPQExpBuffer(qry); @@ -3347,8 +3339,7 @@ _selectTablespace(ArchiveHandle *AH, const char *tablespace) else ahprintf(AH, "%s;\n\n", qry->data); - if (AH->currTablespace) - free(AH->currTablespace); + free(AH->currTablespace); AH->currTablespace = pg_strdup(want); destroyPQExpBuffer(qry); @@ -3399,8 +3390,7 @@ _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam) destroyPQExpBuffer(cmd); - if (AH->currTableAm) - free(AH->currTableAm); + free(AH->currTableAm); AH->currTableAm = pg_strdup(want); } @@ -3659,8 +3649,7 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData) */ if (_tocEntryIsACL(te)) { - if (AH->currUser) - free(AH->currUser); + free(AH->currUser); AH->currUser = NULL; } } @@ -3991,17 +3980,13 @@ restore_toc_entries_prefork(ArchiveHandle *AH, TocEntry *pending_list) DisconnectDatabase(&AH->public); /* blow away any transient state from the old connection */ - if (AH->currUser) - free(AH->currUser); + free(AH->currUser); AH->currUser = NULL; - if (AH->currSchema) - free(AH->currSchema); + free(AH->currSchema); AH->currSchema = NULL; - if (AH->currTablespace) - free(AH->currTablespace); + free(AH->currTablespace); AH->currTablespace = NULL; - if (AH->currTableAm) - free(AH->currTableAm); + free(AH->currTableAm); AH->currTableAm = NULL; } @@ -4842,16 +4827,11 @@ DeCloneArchive(ArchiveHandle *AH) destroyPQExpBuffer(AH->sqlparse.curCmd); /* Clear any connection-local state */ - if (AH->currUser) - free(AH->currUser); - if (AH->currSchema) - free(AH->currSchema); - if (AH->currTablespace) - free(AH->currTablespace); - if (AH->currTableAm) - free(AH->currTableAm); - if (AH->savedPassword) - free(AH->savedPassword); + free(AH->currUser); + free(AH->currSchema); + free(AH->currTablespace); + free(AH->currTableAm); + free(AH->savedPassword); free(AH); } diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index 3443eef6b0e..1023fea01b2 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -632,8 +632,7 @@ _skipData(ArchiveHandle *AH) { if (blkLen > buflen) { - if (buf) - free(buf); + free(buf); buf = (char *) pg_malloc(blkLen); buflen = blkLen; } @@ -649,8 +648,7 @@ _skipData(ArchiveHandle *AH) blkLen = ReadInt(AH); } - if (buf) - free(buf); + free(buf); } /* diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c index 89cdbf80e0e..28baa68fd4e 100644 --- a/src/bin/pg_dump/pg_backup_db.c +++ b/src/bin/pg_dump/pg_backup_db.c @@ -204,8 +204,7 @@ ConnectDatabase(Archive *AHX, */ if (PQconnectionUsedPassword(AH->connection)) { - if (AH->savedPassword) - free(AH->savedPassword); + free(AH->savedPassword); AH->savedPassword = pg_strdup(PQpass(AH->connection)); } diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c index 39d71badb7b..bfc49b66d2c 100644 --- a/src/bin/pg_dump/pg_backup_tar.c +++ b/src/bin/pg_dump/pg_backup_tar.c @@ -412,8 +412,7 @@ tarClose(ArchiveHandle *AH, TAR_MEMBER *th) * handle, and we don't use temp files. */ - if (th->targetFile) - free(th->targetFile); + free(th->targetFile); th->nFH = NULL; } diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 7cc9c72e492..c871cb727d1 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -3339,8 +3339,7 @@ dumpSearchPath(Archive *AH) /* Also save it in AH->searchpath, in case we're doing plain text dump */ AH->searchpath = pg_strdup(qry->data); - if (schemanames) - free(schemanames); + free(schemanames); PQclear(res); destroyPQExpBuffer(qry); destroyPQExpBuffer(path); @@ -4614,8 +4613,7 @@ dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo) subinfo->dobj.catId, 0, subinfo->dobj.dumpId); destroyPQExpBuffer(publications); - if (pubnames) - free(pubnames); + free(pubnames); destroyPQExpBuffer(delq); destroyPQExpBuffer(query); @@ -11908,12 +11906,10 @@ dumpFunc(Archive *fout, const FuncInfo *finfo) destroyPQExpBuffer(delqry); destroyPQExpBuffer(asPart); free(funcsig); - if (funcfullsig) - free(funcfullsig); + free(funcfullsig); free(funcsig_tag); free(qual_funcsig); - if (configitems) - free(configitems); + free(configitems); } @@ -13658,8 +13654,7 @@ dumpAgg(Archive *fout, const AggInfo *agginfo) agginfo->aggfn.rolname, &agginfo->aggfn.dacl); free(aggsig); - if (aggfullsig) - free(aggfullsig); + free(aggfullsig); free(aggsig_tag); PQclear(res); @@ -15713,12 +15708,9 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) tbinfo->attfdwoptions[j]); } /* end loop over columns */ - if (partkeydef) - free(partkeydef); - if (ftoptions) - free(ftoptions); - if (srvname) - free(srvname); + free(partkeydef); + free(ftoptions); + free(srvname); } /* @@ -16105,10 +16097,8 @@ dumpIndex(Archive *fout, const IndxInfo *indxinfo) .createStmt = q->data, .dropStmt = delq->data)); - if (indstatcolsarray) - free(indstatcolsarray); - if (indstatvalsarray) - free(indstatvalsarray); + free(indstatcolsarray); + free(indstatvalsarray); } /* Dump Index Comments */ diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index ae41a652d79..da5cf85272c 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -1500,10 +1500,8 @@ connectDatabase(const char *dbname, const char *connection_string, char *err_msg = NULL; int i = 0; - if (keywords) - free(keywords); - if (values) - free(values); + free(keywords); + free(values); if (conn_opts) PQconninfoFree(conn_opts); diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 89b59a4ff66..bcaea8f5ea2 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -1852,8 +1852,7 @@ putVariable(Variables *variables, const char *context, char *name, /* dup then free, in case value is pointing at this variable */ val = pg_strdup(value); - if (var->svalue) - free(var->svalue); + free(var->svalue); var->svalue = val; var->value.type = PGBT_NO_VALUE; @@ -1872,8 +1871,7 @@ putVariableValue(Variables *variables, const char *context, char *name, if (!var) return false; - if (var->svalue) - free(var->svalue); + free(var->svalue); var->svalue = NULL; var->value = *value; diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 653943b87a6..f3c5196c901 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -595,8 +595,7 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd) success = false; } - if (opt) - free(opt); + free(opt); } else ignore_slash_options(scan_state); @@ -769,8 +768,7 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) break; } - if (pattern2) - free(pattern2); + free(pattern2); } break; case 'a': @@ -881,8 +879,7 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) OT_NORMAL, NULL, true); success = listDbRoleSettings(pattern, pattern2); - if (pattern2) - free(pattern2); + free(pattern2); } else status = PSQL_CMD_UNKNOWN; @@ -963,8 +960,7 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) status = PSQL_CMD_UNKNOWN; } - if (pattern) - free(pattern); + free(pattern); } else ignore_slash_options(scan_state); @@ -1092,10 +1088,8 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch, else status = PSQL_CMD_ERROR; } - if (fname) - free(fname); - if (ln) - free(ln); + free(fname); + free(ln); } } else @@ -1204,8 +1198,7 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch, status = PSQL_CMD_NEWEDIT; } - if (obj_desc) - free(obj_desc); + free(obj_desc); } else ignore_slash_whole_line(scan_state); @@ -1920,8 +1913,7 @@ exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd) success = listAllDbs(pattern, show_verbose); - if (pattern) - free(pattern); + free(pattern); } else ignore_slash_options(scan_state); @@ -2136,10 +2128,8 @@ exec_command_password(PsqlScanState scan_state, bool active_branch) } free(user); - if (pw1) - free(pw1); - if (pw2) - free(pw2); + free(pw1); + free(pw2); termPQExpBuffer(&buf); } else @@ -2214,10 +2204,8 @@ exec_command_prompt(PsqlScanState scan_state, bool active_branch, (result && !SetVariable(pset.vars, opt, result))) success = false; - if (result) - free(result); - if (prompt_text) - free(prompt_text); + free(result); + free(prompt_text); free(opt); } } @@ -2522,8 +2510,7 @@ exec_command_sf_sv(PsqlScanState scan_state, bool active_branch, ClosePager(output); } - if (obj_desc) - free(obj_desc); + free(obj_desc); destroyPQExpBuffer(buf); } else @@ -2802,8 +2789,7 @@ exec_command_z(PsqlScanState scan_state, bool active_branch) OT_NORMAL, NULL, true); success = permissionsList(pattern); - if (pattern) - free(pattern); + free(pattern); } else ignore_slash_options(scan_state); @@ -2853,8 +2839,7 @@ exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch) else slashUsage(pset.popt.topt.pager); - if (opt0) - free(opt0); + free(opt0); } else ignore_slash_options(scan_state); @@ -2994,8 +2979,7 @@ ignore_slash_filepipe(PsqlScanState scan_state) char *arg = psql_scan_slash_option(scan_state, OT_FILEPIPE, NULL, false); - if (arg) - free(arg); + free(arg); } /* @@ -3011,8 +2995,7 @@ ignore_slash_whole_line(PsqlScanState scan_state) char *arg = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, false); - if (arg) - free(arg); + free(arg); } /* @@ -4779,16 +4762,11 @@ restorePsetInfo(printQueryOpt *popt, printQueryOpt *save) /* Free all the old data we're about to overwrite the pointers to. */ /* topt.line_style points to const data that need not be duplicated */ - if (popt->topt.fieldSep.separator) - free(popt->topt.fieldSep.separator); - if (popt->topt.recordSep.separator) - free(popt->topt.recordSep.separator); - if (popt->topt.tableAttr) - free(popt->topt.tableAttr); - if (popt->nullPrint) - free(popt->nullPrint); - if (popt->title) - free(popt->title); + free(popt->topt.fieldSep.separator); + free(popt->topt.recordSep.separator); + free(popt->topt.tableAttr); + free(popt->nullPrint); + free(popt->title); /* * footers and translate_columns are never set in psql's print settings, diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 424a429e1e2..c181682a132 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -389,8 +389,7 @@ do_copy(const char *args) pg_log_error("%s: %s", options->file, reason ? reason : ""); - if (reason) - free(reason); + free(reason); } success = false; } diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index d1ae6991714..2df95bc1654 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1786,8 +1786,7 @@ describeOneTableDetails(const char *schemaname, printQuery(res, &myopt, pset.queryFout, false, pset.logfile); - if (footers[0]) - free(footers[0]); + free(footers[0]); retval = true; goto error_return; /* not an error, just return early */ @@ -3491,8 +3490,7 @@ error_return: termPQExpBuffer(&title); termPQExpBuffer(&tmpbuf); - if (view_def) - free(view_def); + free(view_def); if (res) PQclear(res); diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 416185d659f..6cc7ddda71e 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -158,8 +158,7 @@ pg_send_history(PQExpBuffer history_buf) else { /* Save each previous line for ignoredups processing */ - if (prev_hist) - free(prev_hist); + free(prev_hist); prev_hist = pg_strdup(s); /* And send it to readline */ add_history(s); diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index bd44a1d55da..463cac9fb07 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -4754,11 +4754,9 @@ psql_completion(const char *text, int start, int end) free(previous_words); free(words_buffer); free(text_copy); - if (completion_ref_object) - free(completion_ref_object); + free(completion_ref_object); completion_ref_object = NULL; - if (completion_ref_schema) - free(completion_ref_schema); + free(completion_ref_schema); completion_ref_schema = NULL; /* Return our Grand List O' Matches */ @@ -5160,12 +5158,9 @@ _complete_from_query(const char *simple_query, /* Clean up */ termPQExpBuffer(&query_buffer); free(e_object_like); - if (e_schemaname) - free(e_schemaname); - if (e_ref_object) - free(e_ref_object); - if (e_ref_schema) - free(e_ref_schema); + free(e_schemaname); + free(e_ref_object); + free(e_ref_schema); } /* Return the next result, if any, but not if the query failed */ |
