From a563d941803535dbd27d4191fe7729497b7fdf31 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 2 Oct 2012 15:35:10 -0400 Subject: [PATCH] Standardize naming of malloc/realloc/strdup wrapper functions. We had a number of variants on the theme of "malloc or die", with the majority named like "pg_malloc", but by no means all. Standardize on the names pg_malloc, pg_malloc0, pg_realloc, pg_strdup. Get rid of pg_calloc entirely in favor of using pg_malloc0. This is an essentially cosmetic change, so no back-patch. (I did find a couple of places where psql and pg_dump were using plain malloc or strdup instead of the pg_ versions, but they don't look significant enough to bother back-patching.) --- contrib/oid2name/oid2name.c | 42 ++++++------- contrib/pgbench/pgbench.c | 58 +++++++++--------- src/bin/initdb/initdb.c | 82 ++++++++++++------------- src/bin/pg_basebackup/pg_basebackup.c | 14 ++--- src/bin/pg_basebackup/pg_receivexlog.c | 8 +-- src/bin/pg_basebackup/receivelog.c | 2 +- src/bin/pg_basebackup/streamutil.c | 10 +-- src/bin/pg_basebackup/streamutil.h | 6 +- src/bin/pg_ctl/pg_ctl.c | 35 +++++------ src/bin/pg_dump/compress_io.c | 2 +- src/bin/pg_dump/dumpmem.c | 7 +-- src/bin/pg_dump/dumpmem.h | 2 +- src/bin/pg_dump/pg_backup_archiver.c | 22 +++---- src/bin/pg_dump/pg_backup_custom.c | 6 +- src/bin/pg_dump/pg_backup_directory.c | 6 +- src/bin/pg_dump/pg_backup_tar.c | 10 +-- src/bin/pg_dump/pg_dump.c | 4 +- src/bin/pg_dump/pg_dump_sort.c | 2 +- src/bin/psql/command.c | 4 +- src/bin/psql/common.c | 18 +----- src/bin/psql/common.h | 3 +- src/bin/psql/copy.c | 2 +- src/bin/psql/describe.c | 8 +-- src/bin/psql/print.c | 84 ++++++++------------------ src/bin/psql/psqlscan.l | 2 +- src/bin/psql/startup.c | 4 +- src/bin/scripts/common.c | 34 ++++++++--- src/bin/scripts/common.h | 2 + 28 files changed, 223 insertions(+), 256 deletions(-) diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c index 754eac5f8a..17d4d80fc9 100644 --- a/contrib/oid2name/oid2name.c +++ b/contrib/oid2name/oid2name.c @@ -50,8 +50,8 @@ struct options /* function prototypes */ static void help(const char *progname); void get_opts(int, char **, struct options *); -void *myalloc(size_t size); -char *mystrdup(const char *str); +void *pg_malloc(size_t size); +char *pg_strdup(const char *str); void add_one_elt(char *eltname, eary *eary); char *get_comma_elts(eary *eary); PGconn *sql_conn(struct options *); @@ -104,7 +104,7 @@ get_opts(int argc, char **argv, struct options * my_opts) { /* specify the database */ case 'd': - my_opts->dbname = mystrdup(optarg); + my_opts->dbname = pg_strdup(optarg); break; /* specify one tablename to show */ @@ -129,17 +129,17 @@ get_opts(int argc, char **argv, struct options * my_opts) /* host to connect to */ case 'H': - my_opts->hostname = mystrdup(optarg); + my_opts->hostname = pg_strdup(optarg); break; /* port to connect to on remote host */ case 'p': - my_opts->port = mystrdup(optarg); + my_opts->port = pg_strdup(optarg); break; /* username */ case 'U': - my_opts->username = mystrdup(optarg); + my_opts->username = pg_strdup(optarg); break; /* display system tables */ @@ -201,7 +201,7 @@ help(const char *progname) } void * -myalloc(size_t size) +pg_malloc(size_t size) { void *ptr = malloc(size); @@ -214,7 +214,7 @@ myalloc(size_t size) } char * -mystrdup(const char *str) +pg_strdup(const char *str) { char *result = strdup(str); @@ -237,7 +237,7 @@ add_one_elt(char *eltname, eary *eary) if (eary->alloc == 0) { eary ->alloc = 8; - eary ->array = (char **) myalloc(8 * sizeof(char *)); + eary ->array = (char **) pg_malloc(8 * sizeof(char *)); } else if (eary->num >= eary->alloc) { @@ -252,7 +252,7 @@ add_one_elt(char *eltname, eary *eary) } } - eary ->array[eary->num] = mystrdup(eltname); + eary ->array[eary->num] = pg_strdup(eltname); eary ->num++; } @@ -272,7 +272,7 @@ get_comma_elts(eary *eary) length = 0; if (eary->num == 0) - return mystrdup(""); + return pg_strdup(""); /* * PQescapeString wants 2 * length + 1 bytes of breath space. Add two @@ -281,7 +281,7 @@ get_comma_elts(eary *eary) for (i = 0; i < eary->num; i++) length += strlen(eary->array[i]); - ret = (char *) myalloc(length * 2 + 4 * eary->num); + ret = (char *) pg_malloc(length * 2 + 4 * eary->num); ptr = ret; for (i = 0; i < eary->num; i++) @@ -401,7 +401,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet) nfields = PQnfields(res); /* for each field, get the needed width */ - length = (int *) myalloc(sizeof(int) * nfields); + length = (int *) pg_malloc(sizeof(int) * nfields); for (j = 0; j < nfields; j++) length[j] = strlen(PQfname(res, j)); @@ -424,7 +424,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet) l += length[j] + 2; } fprintf(stdout, "\n"); - pad = (char *) myalloc(l + 1); + pad = (char *) pg_malloc(l + 1); MemSet(pad, '-', l); pad[l] = '\0'; fprintf(stdout, "%s\n", pad); @@ -515,8 +515,8 @@ sql_exec_searchtables(PGconn *conn, struct options * opts) comma_filenodes = get_comma_elts(opts->filenodes); /* 80 extra chars for SQL expression */ - qualifiers = (char *) myalloc(strlen(comma_oids) + strlen(comma_tables) + - strlen(comma_filenodes) + 80); + qualifiers = (char *) pg_malloc(strlen(comma_oids) + strlen(comma_tables) + + strlen(comma_filenodes) + 80); ptr = qualifiers; if (opts->oids->num > 0) @@ -542,7 +542,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts) free(comma_filenodes); /* now build the query */ - todo = (char *) myalloc(650 + strlen(qualifiers)); + todo = (char *) pg_malloc(650 + strlen(qualifiers)); snprintf(todo, 650 + strlen(qualifiers), "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n" "FROM pg_catalog.pg_class c \n" @@ -582,11 +582,11 @@ main(int argc, char **argv) struct options *my_opts; PGconn *pgconn; - my_opts = (struct options *) myalloc(sizeof(struct options)); + my_opts = (struct options *) pg_malloc(sizeof(struct options)); - my_opts->oids = (eary *) myalloc(sizeof(eary)); - my_opts->tables = (eary *) myalloc(sizeof(eary)); - my_opts->filenodes = (eary *) myalloc(sizeof(eary)); + my_opts->oids = (eary *) pg_malloc(sizeof(eary)); + my_opts->tables = (eary *) pg_malloc(sizeof(eary)); + my_opts->filenodes = (eary *) pg_malloc(sizeof(eary)); my_opts->oids->num = my_opts->oids->alloc = 0; my_opts->tables->num = my_opts->tables->alloc = 0; diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c index f5ac3b1c6d..509472262b 100644 --- a/contrib/pgbench/pgbench.c +++ b/contrib/pgbench/pgbench.c @@ -295,7 +295,7 @@ static void *threadRun(void *arg); * routines to check mem allocations and fail noisily. */ static void * -xmalloc(size_t size) +pg_malloc(size_t size) { void *result; @@ -309,7 +309,7 @@ xmalloc(size_t size) } static void * -xrealloc(void *ptr, size_t size) +pg_realloc(void *ptr, size_t size) { void *result; @@ -323,7 +323,7 @@ xrealloc(void *ptr, size_t size) } static char * -xstrdup(const char *s) +pg_strdup(const char *s) { char *result; @@ -574,17 +574,17 @@ putVariable(CState *st, const char *context, char *name, char *value) } if (st->variables) - newvars = (Variable *) xrealloc(st->variables, + newvars = (Variable *) pg_realloc(st->variables, (st->nvariables + 1) * sizeof(Variable)); else - newvars = (Variable *) xmalloc(sizeof(Variable)); + newvars = (Variable *) pg_malloc(sizeof(Variable)); st->variables = newvars; var = &newvars[st->nvariables]; - var->name = xstrdup(name); - var->value = xstrdup(value); + var->name = pg_strdup(name); + var->value = pg_strdup(value); st->nvariables++; @@ -596,7 +596,7 @@ putVariable(CState *st, const char *context, char *name, char *value) char *val; /* dup then free, in case value is pointing at this variable */ - val = xstrdup(value); + val = pg_strdup(value); free(var->value); var->value = val; @@ -618,7 +618,7 @@ parseVariable(const char *sql, int *eaten) if (i == 1) return NULL; - name = xmalloc(i); + name = pg_malloc(i); memcpy(name, &sql[1], i - 1); name[i - 1] = '\0'; @@ -635,7 +635,7 @@ replaceVariable(char **sql, char *param, int len, char *value) { size_t offset = param - *sql; - *sql = xrealloc(*sql, strlen(*sql) - len + valueln + 1); + *sql = pg_realloc(*sql, strlen(*sql) - len + valueln + 1); param = *sql + offset; } @@ -971,7 +971,7 @@ top: { char *sql; - sql = xstrdup(command->argv[0]); + sql = pg_strdup(command->argv[0]); sql = assignVariables(st, sql); if (debug) @@ -1496,7 +1496,7 @@ parseQuery(Command *cmd, const char *raw_sql) char *sql, *p; - sql = xstrdup(raw_sql); + sql = pg_strdup(raw_sql); cmd->argc = 1; p = sql; @@ -1558,8 +1558,8 @@ process_commands(char *buf) return NULL; /* Allocate and initialize Command structure */ - my_commands = (Command *) xmalloc(sizeof(Command)); - my_commands->line = xstrdup(buf); + my_commands = (Command *) pg_malloc(sizeof(Command)); + my_commands->line = pg_strdup(buf); my_commands->command_num = num_commands++; my_commands->type = 0; /* until set */ my_commands->argc = 0; @@ -1573,7 +1573,7 @@ process_commands(char *buf) while (tok != NULL) { - my_commands->argv[j++] = xstrdup(tok); + my_commands->argv[j++] = pg_strdup(tok); my_commands->argc++; tok = strtok(NULL, delim); } @@ -1675,7 +1675,7 @@ process_commands(char *buf) switch (querymode) { case QUERY_SIMPLE: - my_commands->argv[0] = xstrdup(p); + my_commands->argv[0] = pg_strdup(p); my_commands->argc++; break; case QUERY_EXTENDED: @@ -1709,7 +1709,7 @@ process_file(char *filename) } alloc_num = COMMANDS_ALLOC_NUM; - my_commands = (Command **) xmalloc(sizeof(Command *) * alloc_num); + my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num); if (strcmp(filename, "-") == 0) fd = stdin; @@ -1735,7 +1735,7 @@ process_file(char *filename) if (lineno >= alloc_num) { alloc_num += COMMANDS_ALLOC_NUM; - my_commands = xrealloc(my_commands, sizeof(Command *) * alloc_num); + my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num); } } fclose(fd); @@ -1758,7 +1758,7 @@ process_builtin(char *tb) int alloc_num; alloc_num = COMMANDS_ALLOC_NUM; - my_commands = (Command **) xmalloc(sizeof(Command *) * alloc_num); + my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num); lineno = 0; @@ -1789,7 +1789,7 @@ process_builtin(char *tb) if (lineno >= alloc_num) { alloc_num += COMMANDS_ALLOC_NUM; - my_commands = xrealloc(my_commands, sizeof(Command *) * alloc_num); + my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num); } } @@ -1961,7 +1961,7 @@ main(int argc, char **argv) else if ((env = getenv("PGUSER")) != NULL && *env != '\0') login = env; - state = (CState *) xmalloc(sizeof(CState)); + state = (CState *) pg_malloc(sizeof(CState)); memset(state, 0, sizeof(CState)); while ((c = getopt_long(argc, argv, "ih:nvp:dSNc:j:Crs:t:T:U:lf:D:F:M:", long_options, &optindex)) != -1) @@ -2184,7 +2184,7 @@ main(int argc, char **argv) if (nclients > 1) { - state = (CState *) xrealloc(state, sizeof(CState) * nclients); + state = (CState *) pg_realloc(state, sizeof(CState) * nclients); memset(state + 1, 0, sizeof(CState) * (nclients - 1)); /* copy any -D switch values to all clients */ @@ -2308,7 +2308,7 @@ main(int argc, char **argv) } /* set up thread data structures */ - threads = (TState *) xmalloc(sizeof(TState) * nthreads); + threads = (TState *) pg_malloc(sizeof(TState) * nthreads); for (i = 0; i < nthreads; i++) { TState *thread = &threads[i]; @@ -2326,9 +2326,9 @@ main(int argc, char **argv) int t; thread->exec_elapsed = (instr_time *) - xmalloc(sizeof(instr_time) * num_commands); + pg_malloc(sizeof(instr_time) * num_commands); thread->exec_count = (int *) - xmalloc(sizeof(int) * num_commands); + pg_malloc(sizeof(int) * num_commands); for (t = 0; t < num_commands; t++) { @@ -2419,7 +2419,7 @@ threadRun(void *arg) int remains = nstate; /* number of remaining clients */ int i; - result = xmalloc(sizeof(TResult)); + result = pg_malloc(sizeof(TResult)); INSTR_TIME_SET_ZERO(result->conn_time); /* open log file if requested */ @@ -2632,7 +2632,7 @@ pthread_create(pthread_t *thread, fork_pthread *th; void *ret; - th = (fork_pthread *) xmalloc(sizeof(fork_pthread)); + th = (fork_pthread *) pg_malloc(sizeof(fork_pthread)); if (pipe(th->pipes) < 0) { free(th); @@ -2680,7 +2680,7 @@ pthread_join(pthread_t th, void **thread_return) if (thread_return != NULL) { /* assume result is TResult */ - *thread_return = xmalloc(sizeof(TResult)); + *thread_return = pg_malloc(sizeof(TResult)); if (read(th->pipes[0], *thread_return, sizeof(TResult)) != sizeof(TResult)) { free(*thread_return); @@ -2748,7 +2748,7 @@ pthread_create(pthread_t *thread, int save_errno; win32_pthread *th; - th = (win32_pthread *) xmalloc(sizeof(win32_pthread)); + th = (win32_pthread *) pg_malloc(sizeof(win32_pthread)); th->routine = start_routine; th->arg = arg; th->result = NULL; diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 824c7fa7e4..11ff554d79 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -178,7 +178,7 @@ static char bin_path[MAXPGPATH]; static char backend_exec[MAXPGPATH]; static void *pg_malloc(size_t size); -static char *xstrdup(const char *s); +static char *pg_strdup(const char *s); static char **replace_token(char **lines, const char *token, const char *replacement); @@ -304,7 +304,7 @@ pg_malloc(size_t size) } static char * -xstrdup(const char *s) +pg_strdup(const char *s) { char *result; @@ -453,7 +453,7 @@ readfile(const char *path) rewind(infile); nlines = 0; while (fgets(buffer, maxlength + 1, infile) != NULL) - result[nlines++] = xstrdup(buffer); + result[nlines++] = pg_strdup(buffer); fclose(infile); free(buffer); @@ -796,7 +796,7 @@ get_id(void) } #endif - return xstrdup(pw->pw_name); + return pg_strdup(pw->pw_name); } static char * @@ -805,7 +805,7 @@ encodingid_to_string(int enc) char result[20]; sprintf(result, "%d", enc); - return xstrdup(result); + return pg_strdup(result); } /* @@ -888,10 +888,10 @@ find_matching_ts_config(const char *lc_type) * underscore. Just for paranoia, we also stop at '.' or '@'. */ if (lc_type == NULL) - langname = xstrdup(""); + langname = pg_strdup(""); else { - ptr = langname = xstrdup(lc_type); + ptr = langname = pg_strdup(lc_type); while (*ptr && *ptr != '_' && *ptr != '.' && *ptr != '@') ptr++; *ptr = '\0'; @@ -1410,10 +1410,10 @@ bootstrap_template1(void) * there doesn't seem to be any compelling reason to do that. */ snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate); - putenv(xstrdup(cmd)); + putenv(pg_strdup(cmd)); snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype); - putenv(xstrdup(cmd)); + putenv(pg_strdup(cmd)); unsetenv("LC_ALL"); @@ -1531,7 +1531,7 @@ get_set_pwd(void) while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n')) pwdbuf[--i] = '\0'; - pwd1 = xstrdup(pwdbuf); + pwd1 = pg_strdup(pwdbuf); } printf(_("setting password ... ")); @@ -2065,7 +2065,7 @@ set_info_version(void) minor = 0, micro = 0; char *endptr; - char *vstr = xstrdup(PG_VERSION); + char *vstr = pg_strdup(PG_VERSION); char *ptr; ptr = vstr + (strlen(vstr) - 1); @@ -2433,7 +2433,7 @@ locale_date_order(const char *locale) save = setlocale(LC_TIME, NULL); if (!save) return result; - save = xstrdup(save); + save = pg_strdup(save); setlocale(LC_TIME, locale); @@ -2493,14 +2493,14 @@ check_locale_name(int category, const char *locale, char **canonname) return false; /* won't happen, we hope */ /* save may be pointing at a modifiable scratch variable, so copy it. */ - save = xstrdup(save); + save = pg_strdup(save); /* set the locale with setlocale, to see if it accepts it. */ res = setlocale(category, locale); /* save canonical name if requested. */ if (res && canonname) - *canonname = xstrdup(res); + *canonname = pg_strdup(res); /* restore old value. */ if (!setlocale(category, save)) @@ -2588,34 +2588,34 @@ setlocales(void) if (check_locale_name(LC_CTYPE, lc_ctype, &canonname)) lc_ctype = canonname; else - lc_ctype = xstrdup(setlocale(LC_CTYPE, NULL)); + lc_ctype = pg_strdup(setlocale(LC_CTYPE, NULL)); if (check_locale_name(LC_COLLATE, lc_collate, &canonname)) lc_collate = canonname; else - lc_collate = xstrdup(setlocale(LC_COLLATE, NULL)); + lc_collate = pg_strdup(setlocale(LC_COLLATE, NULL)); if (check_locale_name(LC_NUMERIC, lc_numeric, &canonname)) lc_numeric = canonname; else - lc_numeric = xstrdup(setlocale(LC_NUMERIC, NULL)); + lc_numeric = pg_strdup(setlocale(LC_NUMERIC, NULL)); if (check_locale_name(LC_TIME, lc_time, &canonname)) lc_time = canonname; else - lc_time = xstrdup(setlocale(LC_TIME, NULL)); + lc_time = pg_strdup(setlocale(LC_TIME, NULL)); if (check_locale_name(LC_MONETARY, lc_monetary, &canonname)) lc_monetary = canonname; else - lc_monetary = xstrdup(setlocale(LC_MONETARY, NULL)); + lc_monetary = pg_strdup(setlocale(LC_MONETARY, NULL)); #if defined(LC_MESSAGES) && !defined(WIN32) if (check_locale_name(LC_MESSAGES, lc_messages, &canonname)) lc_messages = canonname; else - lc_messages = xstrdup(setlocale(LC_MESSAGES, NULL)); + lc_messages = pg_strdup(setlocale(LC_MESSAGES, NULL)); #else /* when LC_MESSAGES is not available, use the LC_CTYPE setting */ if (check_locale_name(LC_CTYPE, lc_messages, &canonname)) lc_messages = canonname; else - lc_messages = xstrdup(setlocale(LC_CTYPE, NULL)); + lc_messages = pg_strdup(setlocale(LC_CTYPE, NULL)); #endif } @@ -2910,7 +2910,7 @@ main(int argc, char *argv[]) switch (c) { case 'A': - authmethodlocal = authmethodhost = xstrdup(optarg); + authmethodlocal = authmethodhost = pg_strdup(optarg); /* * When ident is specified, use peer for local connections. @@ -2923,22 +2923,22 @@ main(int argc, char *argv[]) authmethodhost = "ident"; break; case 10: - authmethodlocal = xstrdup(optarg); + authmethodlocal = pg_strdup(optarg); break; case 11: - authmethodhost = xstrdup(optarg); + authmethodhost = pg_strdup(optarg); break; case 'D': - pg_data = xstrdup(optarg); + pg_data = pg_strdup(optarg); break; case 'E': - encoding = xstrdup(optarg); + encoding = pg_strdup(optarg); break; case 'W': pwprompt = true; break; case 'U': - username = xstrdup(optarg); + username = pg_strdup(optarg); break; case 'd': debug = true; @@ -2952,43 +2952,43 @@ main(int argc, char *argv[]) do_sync = false; break; case 'L': - share_path = xstrdup(optarg); + share_path = pg_strdup(optarg); break; case 1: - locale = xstrdup(optarg); + locale = pg_strdup(optarg); break; case 2: - lc_collate = xstrdup(optarg); + lc_collate = pg_strdup(optarg); break; case 3: - lc_ctype = xstrdup(optarg); + lc_ctype = pg_strdup(optarg); break; case 4: - lc_monetary = xstrdup(optarg); + lc_monetary = pg_strdup(optarg); break; case 5: - lc_numeric = xstrdup(optarg); + lc_numeric = pg_strdup(optarg); break; case 6: - lc_time = xstrdup(optarg); + lc_time = pg_strdup(optarg); break; case 7: - lc_messages = xstrdup(optarg); + lc_messages = pg_strdup(optarg); break; case 8: locale = "C"; break; case 9: - pwfilename = xstrdup(optarg); + pwfilename = pg_strdup(optarg); break; case 's': show_setting = true; break; case 'T': - default_text_search_config = xstrdup(optarg); + default_text_search_config = pg_strdup(optarg); break; case 'X': - xlog_dir = xstrdup(optarg); + xlog_dir = pg_strdup(optarg); break; default: /* getopt_long already emitted a complaint */ @@ -3005,7 +3005,7 @@ main(int argc, char *argv[]) */ if (optind < argc && strlen(pg_data) == 0) { - pg_data = xstrdup(argv[optind]); + pg_data = pg_strdup(argv[optind]); optind++; } @@ -3038,7 +3038,7 @@ main(int argc, char *argv[]) if (pgdenv && strlen(pgdenv)) { /* PGDATA found */ - pg_data = xstrdup(pgdenv); + pg_data = pg_strdup(pgdenv); } else { @@ -3069,7 +3069,7 @@ main(int argc, char *argv[]) ZeroMemory(&pi, sizeof(pi)); - cmdline = xstrdup(GetCommandLine()); + cmdline = pg_strdup(GetCommandLine()); putenv("PG_RESTRICT_EXEC=1"); diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index ad994f4fa2..4f22116c3e 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -263,7 +263,7 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) uint32 hi, lo; - param = xmalloc0(sizeof(logstreamer_param)); + param = pg_malloc0(sizeof(logstreamer_param)); param->timeline = timeline; param->sysidentifier = sysidentifier; @@ -977,7 +977,7 @@ BaseBackup(void) progname, PQntuples(res), PQnfields(res), 1, 3); disconnect_and_exit(1); } - sysidentifier = strdup(PQgetvalue(res, 0, 0)); + sysidentifier = pg_strdup(PQgetvalue(res, 0, 0)); timeline = atoi(PQgetvalue(res, 0, 1)); PQclear(res); @@ -1286,7 +1286,7 @@ main(int argc, char **argv) switch (c) { case 'D': - basedir = xstrdup(optarg); + basedir = pg_strdup(optarg); break; case 'F': if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0) @@ -1338,7 +1338,7 @@ main(int argc, char **argv) } break; case 'l': - label = xstrdup(optarg); + label = pg_strdup(optarg); break; case 'z': #ifdef HAVE_LIBZ @@ -1369,13 +1369,13 @@ main(int argc, char **argv) } break; case 'h': - dbhost = xstrdup(optarg); + dbhost = pg_strdup(optarg); break; case 'p': - dbport = xstrdup(optarg); + dbport = pg_strdup(optarg); break; case 'U': - dbuser = xstrdup(optarg); + dbuser = pg_strdup(optarg); break; case 'w': dbgetpassword = -1; diff --git a/src/bin/pg_basebackup/pg_receivexlog.c b/src/bin/pg_basebackup/pg_receivexlog.c index 55c7bc692f..843fc69294 100644 --- a/src/bin/pg_basebackup/pg_receivexlog.c +++ b/src/bin/pg_basebackup/pg_receivexlog.c @@ -342,10 +342,10 @@ main(int argc, char **argv) switch (c) { case 'D': - basedir = xstrdup(optarg); + basedir = pg_strdup(optarg); break; case 'h': - dbhost = xstrdup(optarg); + dbhost = pg_strdup(optarg); break; case 'p': if (atoi(optarg) <= 0) @@ -354,10 +354,10 @@ main(int argc, char **argv) progname, optarg); exit(1); } - dbport = xstrdup(optarg); + dbport = pg_strdup(optarg); break; case 'U': - dbuser = xstrdup(optarg); + dbuser = pg_strdup(optarg); break; case 'w': dbgetpassword = -1; diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c index 1e09012bfc..404ff91715 100644 --- a/src/bin/pg_basebackup/receivelog.c +++ b/src/bin/pg_basebackup/receivelog.c @@ -97,7 +97,7 @@ open_walfile(XLogRecPtr startpoint, uint32 timeline, char *basedir, } /* New, empty, file. So pad it to 16Mb with zeroes */ - zerobuf = xmalloc0(XLOG_BLCKSZ); + zerobuf = pg_malloc0(XLOG_BLCKSZ); for (bytes = 0; bytes < XLogSegSize; bytes += XLOG_BLCKSZ) { if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ) diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index c32c5acb2b..b6cc2a303e 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -32,11 +32,11 @@ static char *dbpassword = NULL; PGconn *conn = NULL; /* - * strdup() and malloc() replacements that prints an error and exits + * strdup() and malloc() replacements that print an error and exit * if something goes wrong. Can never return NULL. */ char * -xstrdup(const char *s) +pg_strdup(const char *s) { char *result; @@ -50,7 +50,7 @@ xstrdup(const char *s) } void * -xmalloc0(int size) +pg_malloc0(size_t size) { void *result; @@ -89,8 +89,8 @@ GetConnection(void) if (dbport) argcount++; - keywords = xmalloc0((argcount + 1) * sizeof(*keywords)); - values = xmalloc0((argcount + 1) * sizeof(*values)); + keywords = pg_malloc0((argcount + 1) * sizeof(*keywords)); + values = pg_malloc0((argcount + 1) * sizeof(*values)); keywords[0] = "dbname"; values[0] = "replication"; diff --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h index baba5eb04f..fdf3641861 100644 --- a/src/bin/pg_basebackup/streamutil.h +++ b/src/bin/pg_basebackup/streamutil.h @@ -16,7 +16,7 @@ extern PGconn *conn; } -char *xstrdup(const char *s); -void *xmalloc0(int size); +extern char *pg_strdup(const char *s); +extern void *pg_malloc0(size_t size); -PGconn *GetConnection(void); +extern PGconn *GetConnection(void); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 81ba39ec40..6d74a154ab 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -118,7 +118,7 @@ write_stderr(const char *fmt,...) the supplied arguments. */ __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2))); static void *pg_malloc(size_t size); -static char *xstrdup(const char *s); +static char *pg_strdup(const char *s); static void do_advice(void); static void do_help(void); static void set_mode(char *modeopt); @@ -244,7 +244,7 @@ pg_malloc(size_t size) static char * -xstrdup(const char *s) +pg_strdup(const char *s) { char *result; @@ -351,7 +351,7 @@ readfile(const char *path) rewind(infile); nlines = 0; while (fgets(buffer, maxlength + 1, infile) != NULL) - result[nlines++] = xstrdup(buffer); + result[nlines++] = pg_strdup(buffer); fclose(infile); free(buffer); @@ -1931,7 +1931,7 @@ adjust_data_dir(void) if (exec_path == NULL) my_exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR); else - my_exec_path = xstrdup(exec_path); + my_exec_path = pg_strdup(exec_path); snprintf(cmd, MAXPGPATH, SYSTEMQUOTE "\"%s\" %s%s -C data_directory" SYSTEMQUOTE, my_exec_path, pgdata_opt ? pgdata_opt : "", post_opts ? @@ -1951,7 +1951,7 @@ adjust_data_dir(void) *strchr(filename, '\n') = '\0'; free(pg_data); - pg_data = xstrdup(filename); + pg_data = pg_strdup(filename); canonicalize_path(pg_data); } @@ -2042,7 +2042,7 @@ main(int argc, char **argv) char *pgdata_D; char *env_var = pg_malloc(strlen(optarg) + 8); - pgdata_D = xstrdup(optarg); + pgdata_D = pg_strdup(optarg); canonicalize_path(pgdata_D); snprintf(env_var, strlen(optarg) + 8, "PGDATA=%s", pgdata_D); @@ -2060,22 +2060,22 @@ main(int argc, char **argv) break; } case 'l': - log_file = xstrdup(optarg); + log_file = pg_strdup(optarg); break; case 'm': set_mode(optarg); break; case 'N': - register_servicename = xstrdup(optarg); + register_servicename = pg_strdup(optarg); break; case 'o': - post_opts = xstrdup(optarg); + post_opts = pg_strdup(optarg); break; case 'p': - exec_path = xstrdup(optarg); + exec_path = pg_strdup(optarg); break; case 'P': - register_password = xstrdup(optarg); + register_password = pg_strdup(optarg); break; case 's': silent_mode = true; @@ -2094,16 +2094,11 @@ main(int argc, char **argv) break; case 'U': if (strchr(optarg, '\\')) - register_username = xstrdup(optarg); + register_username = pg_strdup(optarg); else /* Prepend .\ for local accounts */ { - register_username = malloc(strlen(optarg) + 3); - if (!register_username) - { - write_stderr(_("%s: out of memory\n"), progname); - exit(1); - } + register_username = pg_malloc(strlen(optarg) + 3); strcpy(register_username, ".\\"); strcat(register_username, optarg); } @@ -2192,9 +2187,9 @@ main(int argc, char **argv) pg_config = getenv("PGDATA"); if (pg_config) { - pg_config = xstrdup(pg_config); + pg_config = pg_strdup(pg_config); canonicalize_path(pg_config); - pg_data = xstrdup(pg_config); + pg_data = pg_strdup(pg_config); } /* -D might point at config-only directory; if so find the real PGDATA */ diff --git a/src/bin/pg_dump/compress_io.c b/src/bin/pg_dump/compress_io.c index dea058899b..024722fe2d 100644 --- a/src/bin/pg_dump/compress_io.c +++ b/src/bin/pg_dump/compress_io.c @@ -138,7 +138,7 @@ AllocateCompressor(int compression, WriteFunc writeF) exit_horribly(modulename, "not built with zlib support\n"); #endif - cs = (CompressorState *) pg_calloc(1, sizeof(CompressorState)); + cs = (CompressorState *) pg_malloc0(sizeof(CompressorState)); cs->writeF = writeF; cs->comprAlg = alg; diff --git a/src/bin/pg_dump/dumpmem.c b/src/bin/pg_dump/dumpmem.c index 3ef10307bc..22637e26e4 100644 --- a/src/bin/pg_dump/dumpmem.c +++ b/src/bin/pg_dump/dumpmem.c @@ -49,13 +49,12 @@ pg_malloc(size_t size) } void * -pg_calloc(size_t nmemb, size_t size) +pg_malloc0(size_t size) { void *tmp; - tmp = calloc(nmemb, size); - if (!tmp) - exit_horribly(NULL, "out of memory\n"); + tmp = pg_malloc(size); + MemSet(tmp, 0, size); return tmp; } diff --git a/src/bin/pg_dump/dumpmem.h b/src/bin/pg_dump/dumpmem.h index ea73bd62cd..ab31de8be0 100644 --- a/src/bin/pg_dump/dumpmem.h +++ b/src/bin/pg_dump/dumpmem.h @@ -16,7 +16,7 @@ extern char *pg_strdup(const char *string); extern void *pg_malloc(size_t size); -extern void *pg_calloc(size_t nmemb, size_t size); +extern void *pg_malloc0(size_t size); extern void *pg_realloc(void *ptr, size_t size); #endif /* DUMPMEM_H */ diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 722b3e924c..7f47a7c51a 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -748,7 +748,7 @@ NewRestoreOptions(void) { RestoreOptions *opts; - opts = (RestoreOptions *) pg_calloc(1, sizeof(RestoreOptions)); + opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions)); /* set any fields that shouldn't default to zeroes */ opts->format = archUnknown; @@ -848,7 +848,7 @@ ArchiveEntry(Archive *AHX, ArchiveHandle *AH = (ArchiveHandle *) AHX; TocEntry *newToc; - newToc = (TocEntry *) pg_calloc(1, sizeof(TocEntry)); + newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry)); AH->tocCount++; if (dumpId > AH->maxDumpId) @@ -1594,8 +1594,8 @@ buildTocEntryArrays(ArchiveHandle *AH) DumpId maxDumpId = AH->maxDumpId; TocEntry *te; - AH->tocsByDumpId = (TocEntry **) pg_calloc(maxDumpId + 1, sizeof(TocEntry *)); - AH->tableDataId = (DumpId *) pg_calloc(maxDumpId + 1, sizeof(DumpId)); + AH->tocsByDumpId = (TocEntry **) pg_malloc0((maxDumpId + 1) * sizeof(TocEntry *)); + AH->tableDataId = (DumpId *) pg_malloc0((maxDumpId + 1) * sizeof(DumpId)); for (te = AH->toc->next; te != AH->toc; te = te->next) { @@ -1845,7 +1845,7 @@ _discoverArchiveFormat(ArchiveHandle *AH) free(AH->lookahead); AH->lookaheadSize = 512; - AH->lookahead = pg_calloc(1, 512); + AH->lookahead = pg_malloc0(512); AH->lookaheadLen = 0; AH->lookaheadPos = 0; @@ -2021,7 +2021,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt, write_msg(modulename, "allocating AH for %s, format %d\n", FileSpec, fmt); #endif - AH = (ArchiveHandle *) pg_calloc(1, sizeof(ArchiveHandle)); + AH = (ArchiveHandle *) pg_malloc0(sizeof(ArchiveHandle)); /* AH->debugLevel = 100; */ @@ -2065,7 +2065,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt, AH->currTablespace = NULL; /* ditto */ AH->currWithOids = -1; /* force SET */ - AH->toc = (TocEntry *) pg_calloc(1, sizeof(TocEntry)); + AH->toc = (TocEntry *) pg_malloc0(sizeof(TocEntry)); AH->toc->next = AH->toc; AH->toc->prev = AH->toc; @@ -2246,7 +2246,7 @@ ReadToc(ArchiveHandle *AH) for (i = 0; i < AH->tocCount; i++) { - te = (TocEntry *) pg_calloc(1, sizeof(TocEntry)); + te = (TocEntry *) pg_malloc0(sizeof(TocEntry)); te->dumpId = ReadInt(AH); if (te->dumpId > AH->maxDumpId) @@ -3485,9 +3485,9 @@ restore_toc_entries_parallel(ArchiveHandle *AH) ahlog(AH, 2, "entering restore_toc_entries_parallel\n"); - slots = (ParallelSlot *) pg_calloc(n_slots, sizeof(ParallelSlot)); + slots = (ParallelSlot *) pg_malloc0(n_slots * sizeof(ParallelSlot)); pstate = (ParallelState *) pg_malloc(sizeof(ParallelState)); - pstate->pse = (ParallelStateEntry *) pg_calloc(n_slots, sizeof(ParallelStateEntry)); + pstate->pse = (ParallelStateEntry *) pg_malloc0(n_slots * sizeof(ParallelStateEntry)); pstate->numWorkers = ropt->number_of_jobs; for (i = 0; i < pstate->numWorkers; i++) unsetProcessIdentifier(&(pstate->pse[i])); @@ -3776,7 +3776,7 @@ reap_child(ParallelSlot *slots, int n_slots, int *work_status) /* first time around only, make space for handles to listen on */ if (handles == NULL) - handles = (HANDLE *) pg_calloc(sizeof(HANDLE), n_slots); + handles = (HANDLE *) pg_malloc0(n_slots * sizeof(HANDLE)); /* set up list of handles to listen to */ for (snum = 0, tnum = 0; snum < n_slots; snum++) diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index f7dc5be8e5..c9adc6f82f 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -129,7 +129,7 @@ InitArchiveFmt_Custom(ArchiveHandle *AH) AH->DeClonePtr = _DeClone; /* Set up a private area. */ - ctx = (lclContext *) pg_calloc(1, sizeof(lclContext)); + ctx = (lclContext *) pg_malloc0(sizeof(lclContext)); AH->formatData = (void *) ctx; /* Initialize LO buffering */ @@ -198,7 +198,7 @@ _ArchiveEntry(ArchiveHandle *AH, TocEntry *te) { lclTocEntry *ctx; - ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); + ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry)); if (te->dataDumper) ctx->dataState = K_OFFSET_POS_NOT_SET; else @@ -239,7 +239,7 @@ _ReadExtraToc(ArchiveHandle *AH, TocEntry *te) if (ctx == NULL) { - ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); + ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry)); te->formatData = (void *) ctx; } diff --git a/src/bin/pg_dump/pg_backup_directory.c b/src/bin/pg_dump/pg_backup_directory.c index a2be6ac000..574fdaa28a 100644 --- a/src/bin/pg_dump/pg_backup_directory.c +++ b/src/bin/pg_dump/pg_backup_directory.c @@ -126,7 +126,7 @@ InitArchiveFmt_Directory(ArchiveHandle *AH) AH->DeClonePtr = NULL; /* Set up our private context */ - ctx = (lclContext *) pg_calloc(1, sizeof(lclContext)); + ctx = (lclContext *) pg_malloc0(sizeof(lclContext)); AH->formatData = (void *) ctx; ctx->dataFH = NULL; @@ -194,7 +194,7 @@ _ArchiveEntry(ArchiveHandle *AH, TocEntry *te) lclTocEntry *tctx; char fn[MAXPGPATH]; - tctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); + tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry)); if (te->dataDumper) { snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId); @@ -243,7 +243,7 @@ _ReadExtraToc(ArchiveHandle *AH, TocEntry *te) if (tctx == NULL) { - tctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); + tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry)); te->formatData = (void *) tctx; } diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c index 9254de9576..ee277b1f8c 100644 --- a/src/bin/pg_dump/pg_backup_tar.c +++ b/src/bin/pg_dump/pg_backup_tar.c @@ -159,7 +159,7 @@ InitArchiveFmt_Tar(ArchiveHandle *AH) /* * Set up some special context used in compressing data. */ - ctx = (lclContext *) pg_calloc(1, sizeof(lclContext)); + ctx = (lclContext *) pg_malloc0(sizeof(lclContext)); AH->formatData = (void *) ctx; ctx->filePos = 0; ctx->isSpecialScript = 0; @@ -266,7 +266,7 @@ _ArchiveEntry(ArchiveHandle *AH, TocEntry *te) lclTocEntry *ctx; char fn[K_STD_BUF_SIZE]; - ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); + ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry)); if (te->dataDumper != NULL) { #ifdef HAVE_LIBZ @@ -305,7 +305,7 @@ _ReadExtraToc(ArchiveHandle *AH, TocEntry *te) if (ctx == NULL) { - ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); + ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry)); te->formatData = (void *) ctx; } @@ -378,7 +378,7 @@ tarOpen(ArchiveHandle *AH, const char *filename, char mode) } else { - tm = pg_calloc(1, sizeof(TAR_MEMBER)); + tm = pg_malloc0(sizeof(TAR_MEMBER)); #ifndef WIN32 tm->tmpFH = tmpfile(); @@ -1128,7 +1128,7 @@ static TAR_MEMBER * _tarPositionTo(ArchiveHandle *AH, const char *filename) { lclContext *ctx = (lclContext *) AH->formatData; - TAR_MEMBER *th = pg_calloc(1, sizeof(TAR_MEMBER)); + TAR_MEMBER *th = pg_malloc0(sizeof(TAR_MEMBER)); char c; char header[512]; size_t i, diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 4bed690105..9920d96724 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -3776,7 +3776,7 @@ getFuncs(Archive *fout, int *numFuncs) *numFuncs = ntups; - finfo = (FuncInfo *) pg_calloc(ntups, sizeof(FuncInfo)); + finfo = (FuncInfo *) pg_malloc0(ntups * sizeof(FuncInfo)); i_tableoid = PQfnumber(res, "tableoid"); i_oid = PQfnumber(res, "oid"); @@ -4205,7 +4205,7 @@ getTables(Archive *fout, int *numTables) * only one, because we don't yet know which tables might be inheritance * ancestors of the target table. */ - tblinfo = (TableInfo *) pg_calloc(ntups, sizeof(TableInfo)); + tblinfo = (TableInfo *) pg_malloc0(ntups * sizeof(TableInfo)); i_reltableoid = PQfnumber(res, "tableoid"); i_reloid = PQfnumber(res, "oid"); diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index ddbe8305b1..f0dc14592a 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -552,7 +552,7 @@ findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs) bool fixedloop; int i; - processed = (bool *) pg_calloc(getMaxDumpId() + 1, sizeof(bool)); + processed = (bool *) pg_malloc0((getMaxDumpId() + 1) * sizeof(bool)); workspace = (DumpableObject **) pg_malloc(totObjs * sizeof(DumpableObject *)); fixedloop = false; diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index b90d2f12b9..8ccd00d75f 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1472,7 +1472,7 @@ prompt_for_password(const char *username) { char *prompt_text; - prompt_text = malloc(strlen(username) + 100); + prompt_text = pg_malloc(strlen(username) + 100); snprintf(prompt_text, strlen(username) + 100, _("Password for user %s: "), username); result = simple_prompt(prompt_text, 100, false); @@ -1549,7 +1549,7 @@ do_connect(char *dbname, char *user, char *host, char *port) } else if (o_conn && user && strcmp(PQuser(o_conn), user) == 0) { - password = strdup(PQpass(o_conn)); + password = pg_strdup(PQpass(o_conn)); } while (true) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f5bd0f6d42..1cb30088c4 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -70,26 +70,12 @@ pg_malloc(size_t size) } void * -pg_malloc_zero(size_t size) +pg_malloc0(size_t size) { void *tmp; tmp = pg_malloc(size); - memset(tmp, 0, size); - return tmp; -} - -void * -pg_calloc(size_t nmemb, size_t size) -{ - void *tmp; - - tmp = calloc(nmemb, size); - if (!tmp) - { - psql_error("out of memory\n"); - exit(EXIT_FAILURE); - } + MemSet(tmp, 0, size); return tmp; } diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 8037cbc0a0..f54baab841 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -28,8 +28,7 @@ */ extern char *pg_strdup(const char *string); extern void *pg_malloc(size_t size); -extern void *pg_malloc_zero(size_t size); -extern void *pg_calloc(size_t nmemb, size_t size); +extern void *pg_malloc0(size_t size); extern bool setQFout(const char *fname); diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 22fcc5975e..6cc1f4643c 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -97,7 +97,7 @@ parse_slash_copy(const char *args) return NULL; } - result = pg_calloc(1, sizeof(struct copy_options)); + result = pg_malloc0(sizeof(struct copy_options)); result->before_tofrom = pg_strdup(""); /* initialize for appending */ diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 35295a8772..15d02eeeff 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1236,12 +1236,12 @@ describeOneTableDetails(const char *schemaname, tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0; tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0; tableinfo.reloptions = (pset.sversion >= 80200) ? - strdup(PQgetvalue(res, 0, 6)) : NULL; + pg_strdup(PQgetvalue(res, 0, 6)) : NULL; tableinfo.tablespace = (pset.sversion >= 80000) ? atooid(PQgetvalue(res, 0, 7)) : 0; tableinfo.reloftype = (pset.sversion >= 90000 && strcmp(PQgetvalue(res, 0, 8), "") != 0) ? - strdup(PQgetvalue(res, 0, 8)) : NULL; + pg_strdup(PQgetvalue(res, 0, 8)) : NULL; tableinfo.relpersistence = (pset.sversion >= 90100) ? *(PQgetvalue(res, 0, 9)) : 0; PQclear(res); @@ -1382,7 +1382,7 @@ describeOneTableDetails(const char *schemaname, { show_modifiers = true; headers[cols++] = gettext_noop("Modifiers"); - modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers)); + modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers)); } if (tableinfo.relkind == 'S') @@ -2417,7 +2417,7 @@ describeRoles(const char *pattern, bool verbose) return false; nrows = PQntuples(res); - attr = pg_malloc_zero((nrows + 1) * sizeof(*attr)); + attr = pg_malloc0((nrows + 1) * sizeof(*attr)); printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows); diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c index 8fa5e37128..8421245088 100644 --- a/src/bin/psql/print.c +++ b/src/bin/psql/print.c @@ -131,34 +131,6 @@ static void IsPagerNeeded(const printTableContent *cont, const int extra_lines, static void print_aligned_vertical(const printTableContent *cont, FILE *fout); -static void * -pg_local_malloc(size_t size) -{ - void *tmp; - - tmp = malloc(size); - if (!tmp) - { - fprintf(stderr, _("out of memory\n")); - exit(EXIT_FAILURE); - } - return tmp; -} - -static void * -pg_local_calloc(int count, size_t size) -{ - void *tmp; - - tmp = calloc(count, size); - if (!tmp) - { - fprintf(stderr, _("out of memory\n")); - exit(EXIT_FAILURE); - } - return tmp; -} - static int integer_digits(const char *my_str) { @@ -210,8 +182,7 @@ format_numeric_locale(const char *my_str) leading_digits; int groupdigits = atoi(grouping); int new_str_start = 0; - char *new_str = pg_local_malloc( - strlen_with_numeric_locale(my_str) + 1); + char *new_str = pg_malloc(strlen_with_numeric_locale(my_str) + 1); leading_digits = (int_len % groupdigits != 0) ? int_len % groupdigits : groupdigits; @@ -571,18 +542,18 @@ print_aligned_text(const printTableContent *cont, FILE *fout) if (cont->ncolumns > 0) { col_count = cont->ncolumns; - width_header = pg_local_calloc(col_count, sizeof(*width_header)); - width_average = pg_local_calloc(col_count, sizeof(*width_average)); - max_width = pg_local_calloc(col_count, sizeof(*max_width)); - width_wrap = pg_local_calloc(col_count, sizeof(*width_wrap)); - max_nl_lines = pg_local_calloc(col_count, sizeof(*max_nl_lines)); - curr_nl_line = pg_local_calloc(col_count, sizeof(*curr_nl_line)); - col_lineptrs = pg_local_calloc(col_count, sizeof(*col_lineptrs)); - max_bytes = pg_local_calloc(col_count, sizeof(*max_bytes)); - format_buf = pg_local_calloc(col_count, sizeof(*format_buf)); - header_done = pg_local_calloc(col_count, sizeof(*header_done)); - bytes_output = pg_local_calloc(col_count, sizeof(*bytes_output)); - wrap = pg_local_calloc(col_count, sizeof(*wrap)); + width_header = pg_malloc0(col_count * sizeof(*width_header)); + width_average = pg_malloc0(col_count * sizeof(*width_average)); + max_width = pg_malloc0(col_count * sizeof(*max_width)); + width_wrap = pg_malloc0(col_count * sizeof(*width_wrap)); + max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines)); + curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line)); + col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs)); + max_bytes = pg_malloc0(col_count * sizeof(*max_bytes)); + format_buf = pg_malloc0(col_count * sizeof(*format_buf)); + header_done = pg_malloc0(col_count * sizeof(*header_done)); + bytes_output = pg_malloc0(col_count * sizeof(*bytes_output)); + wrap = pg_malloc0(col_count * sizeof(*wrap)); } else { @@ -678,10 +649,10 @@ print_aligned_text(const printTableContent *cont, FILE *fout) for (i = 0; i < col_count; i++) { /* Add entry for ptr == NULL array termination */ - col_lineptrs[i] = pg_local_calloc(max_nl_lines[i] + 1, - sizeof(**col_lineptrs)); + col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) * + sizeof(**col_lineptrs)); - format_buf[i] = pg_local_malloc(max_bytes[i] + 1); + format_buf[i] = pg_malloc(max_bytes[i] + 1); col_lineptrs[i]->ptr = format_buf[i]; } @@ -1247,11 +1218,11 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout) * We now have all the information we need to setup the formatting * structures */ - dlineptr = pg_local_malloc((sizeof(*dlineptr)) * (dheight + 1)); - hlineptr = pg_local_malloc((sizeof(*hlineptr)) * (hheight + 1)); + dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1)); + hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1)); - dlineptr->ptr = pg_local_malloc(dformatsize); - hlineptr->ptr = pg_local_malloc(hformatsize); + dlineptr->ptr = pg_malloc(dformatsize); + hlineptr->ptr = pg_malloc(hformatsize); if (cont->opt->start_table) { @@ -2132,17 +2103,14 @@ printTableInit(printTableContent *const content, const printTableOpt *opt, content->ncolumns = ncolumns; content->nrows = nrows; - content->headers = pg_local_calloc(ncolumns + 1, - sizeof(*content->headers)); + content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers)); - content->cells = pg_local_calloc(ncolumns * nrows + 1, - sizeof(*content->cells)); + content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells)); content->cellmustfree = NULL; content->footers = NULL; - content->aligns = pg_local_calloc(ncolumns + 1, - sizeof(*content->align)); + content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align)); content->header = content->headers; content->cell = content->cells; @@ -2230,8 +2198,8 @@ printTableAddCell(printTableContent *const content, char *cell, if (mustfree) { if (content->cellmustfree == NULL) - content->cellmustfree = pg_local_calloc( - content->ncolumns * content->nrows + 1, sizeof(bool)); + content->cellmustfree = + pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool)); content->cellmustfree[content->cellsadded] = true; } @@ -2256,7 +2224,7 @@ printTableAddFooter(printTableContent *const content, const char *footer) { printTableFooter *f; - f = pg_local_calloc(1, sizeof(*f)); + f = pg_malloc0(sizeof(*f)); f->data = pg_strdup(footer); if (content->footers == NULL) diff --git a/src/bin/psql/psqlscan.l b/src/bin/psql/psqlscan.l index 1208c8f475..d32a12c63c 100644 --- a/src/bin/psql/psqlscan.l +++ b/src/bin/psql/psqlscan.l @@ -1150,7 +1150,7 @@ psql_scan_create(void) { PsqlScanState state; - state = (PsqlScanStateData *) pg_malloc_zero(sizeof(PsqlScanStateData)); + state = (PsqlScanStateData *) pg_malloc0(sizeof(PsqlScanStateData)); psql_scan_reset(state); diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 8ba8f70421..3fb12c9452 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -188,8 +188,8 @@ main(int argc, char *argv[]) password_prompt = pg_strdup(_("Password: ")); else { - password_prompt = malloc(strlen(_("Password for user %s: ")) - 2 + - strlen(options.username) + 1); + password_prompt = pg_malloc(strlen(_("Password for user %s: ")) - 2 + + strlen(options.username) + 1); sprintf(password_prompt, _("Password for user %s: "), options.username); } diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index 0ae708b21e..4a7d02f777 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -109,14 +109,8 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport, do { #define PARAMS_ARRAY_SIZE 7 - const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords)); - const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values)); - - if (!keywords || !values) - { - fprintf(stderr, _("%s: out of memory\n"), progname); - exit(1); - } + const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords)); + const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values)); keywords[0] = "host"; values[0] = pghost; @@ -305,6 +299,30 @@ pg_strdup(const char *string) return tmp; } +void * +pg_malloc(size_t size) +{ + void *tmp; + + tmp = malloc(size); + if (!tmp) + { + fprintf(stderr, _("out of memory\n")); + exit(EXIT_FAILURE); + } + return tmp; +} + +void * +pg_malloc0(size_t size) +{ + void *tmp; + + tmp = pg_malloc(size); + MemSet(tmp, 0, size); + return tmp; +} + /* * Check yes/no answer in a localized way. 1=yes, 0=no, -1=neither. */ diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h index 854bc2f03a..244409cc5b 100644 --- a/src/bin/scripts/common.h +++ b/src/bin/scripts/common.h @@ -51,5 +51,7 @@ extern bool yesno_prompt(const char *question); extern void setup_cancel_handler(void); extern char *pg_strdup(const char *string); +extern void *pg_malloc(size_t size); +extern void *pg_malloc0(size_t size); #endif /* COMMON_H */