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.)
This commit is contained in:
Tom Lane 2012-10-02 15:35:10 -04:00
parent 779f80b75d
commit a563d94180
28 changed files with 223 additions and 256 deletions

View File

@ -50,8 +50,8 @@ struct options
/* function prototypes */ /* function prototypes */
static void help(const char *progname); static void help(const char *progname);
void get_opts(int, char **, struct options *); void get_opts(int, char **, struct options *);
void *myalloc(size_t size); void *pg_malloc(size_t size);
char *mystrdup(const char *str); char *pg_strdup(const char *str);
void add_one_elt(char *eltname, eary *eary); void add_one_elt(char *eltname, eary *eary);
char *get_comma_elts(eary *eary); char *get_comma_elts(eary *eary);
PGconn *sql_conn(struct options *); PGconn *sql_conn(struct options *);
@ -104,7 +104,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
{ {
/* specify the database */ /* specify the database */
case 'd': case 'd':
my_opts->dbname = mystrdup(optarg); my_opts->dbname = pg_strdup(optarg);
break; break;
/* specify one tablename to show */ /* specify one tablename to show */
@ -129,17 +129,17 @@ get_opts(int argc, char **argv, struct options * my_opts)
/* host to connect to */ /* host to connect to */
case 'H': case 'H':
my_opts->hostname = mystrdup(optarg); my_opts->hostname = pg_strdup(optarg);
break; break;
/* port to connect to on remote host */ /* port to connect to on remote host */
case 'p': case 'p':
my_opts->port = mystrdup(optarg); my_opts->port = pg_strdup(optarg);
break; break;
/* username */ /* username */
case 'U': case 'U':
my_opts->username = mystrdup(optarg); my_opts->username = pg_strdup(optarg);
break; break;
/* display system tables */ /* display system tables */
@ -201,7 +201,7 @@ help(const char *progname)
} }
void * void *
myalloc(size_t size) pg_malloc(size_t size)
{ {
void *ptr = malloc(size); void *ptr = malloc(size);
@ -214,7 +214,7 @@ myalloc(size_t size)
} }
char * char *
mystrdup(const char *str) pg_strdup(const char *str)
{ {
char *result = strdup(str); char *result = strdup(str);
@ -237,7 +237,7 @@ add_one_elt(char *eltname, eary *eary)
if (eary->alloc == 0) if (eary->alloc == 0)
{ {
eary ->alloc = 8; eary ->alloc = 8;
eary ->array = (char **) myalloc(8 * sizeof(char *)); eary ->array = (char **) pg_malloc(8 * sizeof(char *));
} }
else if (eary->num >= eary->alloc) 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++; eary ->num++;
} }
@ -272,7 +272,7 @@ get_comma_elts(eary *eary)
length = 0; length = 0;
if (eary->num == 0) if (eary->num == 0)
return mystrdup(""); return pg_strdup("");
/* /*
* PQescapeString wants 2 * length + 1 bytes of breath space. Add two * 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++) for (i = 0; i < eary->num; i++)
length += strlen(eary->array[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; ptr = ret;
for (i = 0; i < eary->num; i++) for (i = 0; i < eary->num; i++)
@ -401,7 +401,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
nfields = PQnfields(res); nfields = PQnfields(res);
/* for each field, get the needed width */ /* 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++) for (j = 0; j < nfields; j++)
length[j] = strlen(PQfname(res, j)); length[j] = strlen(PQfname(res, j));
@ -424,7 +424,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
l += length[j] + 2; l += length[j] + 2;
} }
fprintf(stdout, "\n"); fprintf(stdout, "\n");
pad = (char *) myalloc(l + 1); pad = (char *) pg_malloc(l + 1);
MemSet(pad, '-', l); MemSet(pad, '-', l);
pad[l] = '\0'; pad[l] = '\0';
fprintf(stdout, "%s\n", pad); fprintf(stdout, "%s\n", pad);
@ -515,8 +515,8 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
comma_filenodes = get_comma_elts(opts->filenodes); comma_filenodes = get_comma_elts(opts->filenodes);
/* 80 extra chars for SQL expression */ /* 80 extra chars for SQL expression */
qualifiers = (char *) myalloc(strlen(comma_oids) + strlen(comma_tables) + qualifiers = (char *) pg_malloc(strlen(comma_oids) + strlen(comma_tables) +
strlen(comma_filenodes) + 80); strlen(comma_filenodes) + 80);
ptr = qualifiers; ptr = qualifiers;
if (opts->oids->num > 0) if (opts->oids->num > 0)
@ -542,7 +542,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
free(comma_filenodes); free(comma_filenodes);
/* now build the query */ /* now build the query */
todo = (char *) myalloc(650 + strlen(qualifiers)); todo = (char *) pg_malloc(650 + strlen(qualifiers));
snprintf(todo, 650 + strlen(qualifiers), snprintf(todo, 650 + strlen(qualifiers),
"SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n" "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
"FROM pg_catalog.pg_class c \n" "FROM pg_catalog.pg_class c \n"
@ -582,11 +582,11 @@ main(int argc, char **argv)
struct options *my_opts; struct options *my_opts;
PGconn *pgconn; 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->oids = (eary *) pg_malloc(sizeof(eary));
my_opts->tables = (eary *) myalloc(sizeof(eary)); my_opts->tables = (eary *) pg_malloc(sizeof(eary));
my_opts->filenodes = (eary *) myalloc(sizeof(eary)); my_opts->filenodes = (eary *) pg_malloc(sizeof(eary));
my_opts->oids->num = my_opts->oids->alloc = 0; my_opts->oids->num = my_opts->oids->alloc = 0;
my_opts->tables->num = my_opts->tables->alloc = 0; my_opts->tables->num = my_opts->tables->alloc = 0;

View File

@ -295,7 +295,7 @@ static void *threadRun(void *arg);
* routines to check mem allocations and fail noisily. * routines to check mem allocations and fail noisily.
*/ */
static void * static void *
xmalloc(size_t size) pg_malloc(size_t size)
{ {
void *result; void *result;
@ -309,7 +309,7 @@ xmalloc(size_t size)
} }
static void * static void *
xrealloc(void *ptr, size_t size) pg_realloc(void *ptr, size_t size)
{ {
void *result; void *result;
@ -323,7 +323,7 @@ xrealloc(void *ptr, size_t size)
} }
static char * static char *
xstrdup(const char *s) pg_strdup(const char *s)
{ {
char *result; char *result;
@ -574,17 +574,17 @@ putVariable(CState *st, const char *context, char *name, char *value)
} }
if (st->variables) if (st->variables)
newvars = (Variable *) xrealloc(st->variables, newvars = (Variable *) pg_realloc(st->variables,
(st->nvariables + 1) * sizeof(Variable)); (st->nvariables + 1) * sizeof(Variable));
else else
newvars = (Variable *) xmalloc(sizeof(Variable)); newvars = (Variable *) pg_malloc(sizeof(Variable));
st->variables = newvars; st->variables = newvars;
var = &newvars[st->nvariables]; var = &newvars[st->nvariables];
var->name = xstrdup(name); var->name = pg_strdup(name);
var->value = xstrdup(value); var->value = pg_strdup(value);
st->nvariables++; st->nvariables++;
@ -596,7 +596,7 @@ putVariable(CState *st, const char *context, char *name, char *value)
char *val; char *val;
/* dup then free, in case value is pointing at this variable */ /* dup then free, in case value is pointing at this variable */
val = xstrdup(value); val = pg_strdup(value);
free(var->value); free(var->value);
var->value = val; var->value = val;
@ -618,7 +618,7 @@ parseVariable(const char *sql, int *eaten)
if (i == 1) if (i == 1)
return NULL; return NULL;
name = xmalloc(i); name = pg_malloc(i);
memcpy(name, &sql[1], i - 1); memcpy(name, &sql[1], i - 1);
name[i - 1] = '\0'; name[i - 1] = '\0';
@ -635,7 +635,7 @@ replaceVariable(char **sql, char *param, int len, char *value)
{ {
size_t offset = param - *sql; 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; param = *sql + offset;
} }
@ -971,7 +971,7 @@ top:
{ {
char *sql; char *sql;
sql = xstrdup(command->argv[0]); sql = pg_strdup(command->argv[0]);
sql = assignVariables(st, sql); sql = assignVariables(st, sql);
if (debug) if (debug)
@ -1496,7 +1496,7 @@ parseQuery(Command *cmd, const char *raw_sql)
char *sql, char *sql,
*p; *p;
sql = xstrdup(raw_sql); sql = pg_strdup(raw_sql);
cmd->argc = 1; cmd->argc = 1;
p = sql; p = sql;
@ -1558,8 +1558,8 @@ process_commands(char *buf)
return NULL; return NULL;
/* Allocate and initialize Command structure */ /* Allocate and initialize Command structure */
my_commands = (Command *) xmalloc(sizeof(Command)); my_commands = (Command *) pg_malloc(sizeof(Command));
my_commands->line = xstrdup(buf); my_commands->line = pg_strdup(buf);
my_commands->command_num = num_commands++; my_commands->command_num = num_commands++;
my_commands->type = 0; /* until set */ my_commands->type = 0; /* until set */
my_commands->argc = 0; my_commands->argc = 0;
@ -1573,7 +1573,7 @@ process_commands(char *buf)
while (tok != NULL) while (tok != NULL)
{ {
my_commands->argv[j++] = xstrdup(tok); my_commands->argv[j++] = pg_strdup(tok);
my_commands->argc++; my_commands->argc++;
tok = strtok(NULL, delim); tok = strtok(NULL, delim);
} }
@ -1675,7 +1675,7 @@ process_commands(char *buf)
switch (querymode) switch (querymode)
{ {
case QUERY_SIMPLE: case QUERY_SIMPLE:
my_commands->argv[0] = xstrdup(p); my_commands->argv[0] = pg_strdup(p);
my_commands->argc++; my_commands->argc++;
break; break;
case QUERY_EXTENDED: case QUERY_EXTENDED:
@ -1709,7 +1709,7 @@ process_file(char *filename)
} }
alloc_num = COMMANDS_ALLOC_NUM; 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) if (strcmp(filename, "-") == 0)
fd = stdin; fd = stdin;
@ -1735,7 +1735,7 @@ process_file(char *filename)
if (lineno >= alloc_num) if (lineno >= alloc_num)
{ {
alloc_num += COMMANDS_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); fclose(fd);
@ -1758,7 +1758,7 @@ process_builtin(char *tb)
int alloc_num; int alloc_num;
alloc_num = COMMANDS_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; lineno = 0;
@ -1789,7 +1789,7 @@ process_builtin(char *tb)
if (lineno >= alloc_num) if (lineno >= alloc_num)
{ {
alloc_num += COMMANDS_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') else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
login = env; login = env;
state = (CState *) xmalloc(sizeof(CState)); state = (CState *) pg_malloc(sizeof(CState));
memset(state, 0, 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) 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) 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)); memset(state + 1, 0, sizeof(CState) * (nclients - 1));
/* copy any -D switch values to all clients */ /* copy any -D switch values to all clients */
@ -2308,7 +2308,7 @@ main(int argc, char **argv)
} }
/* set up thread data structures */ /* set up thread data structures */
threads = (TState *) xmalloc(sizeof(TState) * nthreads); threads = (TState *) pg_malloc(sizeof(TState) * nthreads);
for (i = 0; i < nthreads; i++) for (i = 0; i < nthreads; i++)
{ {
TState *thread = &threads[i]; TState *thread = &threads[i];
@ -2326,9 +2326,9 @@ main(int argc, char **argv)
int t; int t;
thread->exec_elapsed = (instr_time *) thread->exec_elapsed = (instr_time *)
xmalloc(sizeof(instr_time) * num_commands); pg_malloc(sizeof(instr_time) * num_commands);
thread->exec_count = (int *) thread->exec_count = (int *)
xmalloc(sizeof(int) * num_commands); pg_malloc(sizeof(int) * num_commands);
for (t = 0; t < num_commands; t++) for (t = 0; t < num_commands; t++)
{ {
@ -2419,7 +2419,7 @@ threadRun(void *arg)
int remains = nstate; /* number of remaining clients */ int remains = nstate; /* number of remaining clients */
int i; int i;
result = xmalloc(sizeof(TResult)); result = pg_malloc(sizeof(TResult));
INSTR_TIME_SET_ZERO(result->conn_time); INSTR_TIME_SET_ZERO(result->conn_time);
/* open log file if requested */ /* open log file if requested */
@ -2632,7 +2632,7 @@ pthread_create(pthread_t *thread,
fork_pthread *th; fork_pthread *th;
void *ret; void *ret;
th = (fork_pthread *) xmalloc(sizeof(fork_pthread)); th = (fork_pthread *) pg_malloc(sizeof(fork_pthread));
if (pipe(th->pipes) < 0) if (pipe(th->pipes) < 0)
{ {
free(th); free(th);
@ -2680,7 +2680,7 @@ pthread_join(pthread_t th, void **thread_return)
if (thread_return != NULL) if (thread_return != NULL)
{ {
/* assume result is TResult */ /* 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)) if (read(th->pipes[0], *thread_return, sizeof(TResult)) != sizeof(TResult))
{ {
free(*thread_return); free(*thread_return);
@ -2748,7 +2748,7 @@ pthread_create(pthread_t *thread,
int save_errno; int save_errno;
win32_pthread *th; win32_pthread *th;
th = (win32_pthread *) xmalloc(sizeof(win32_pthread)); th = (win32_pthread *) pg_malloc(sizeof(win32_pthread));
th->routine = start_routine; th->routine = start_routine;
th->arg = arg; th->arg = arg;
th->result = NULL; th->result = NULL;

View File

@ -178,7 +178,7 @@ static char bin_path[MAXPGPATH];
static char backend_exec[MAXPGPATH]; static char backend_exec[MAXPGPATH];
static void *pg_malloc(size_t size); 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, static char **replace_token(char **lines,
const char *token, const char *replacement); const char *token, const char *replacement);
@ -304,7 +304,7 @@ pg_malloc(size_t size)
} }
static char * static char *
xstrdup(const char *s) pg_strdup(const char *s)
{ {
char *result; char *result;
@ -453,7 +453,7 @@ readfile(const char *path)
rewind(infile); rewind(infile);
nlines = 0; nlines = 0;
while (fgets(buffer, maxlength + 1, infile) != NULL) while (fgets(buffer, maxlength + 1, infile) != NULL)
result[nlines++] = xstrdup(buffer); result[nlines++] = pg_strdup(buffer);
fclose(infile); fclose(infile);
free(buffer); free(buffer);
@ -796,7 +796,7 @@ get_id(void)
} }
#endif #endif
return xstrdup(pw->pw_name); return pg_strdup(pw->pw_name);
} }
static char * static char *
@ -805,7 +805,7 @@ encodingid_to_string(int enc)
char result[20]; char result[20];
sprintf(result, "%d", enc); 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 '@'. * underscore. Just for paranoia, we also stop at '.' or '@'.
*/ */
if (lc_type == NULL) if (lc_type == NULL)
langname = xstrdup(""); langname = pg_strdup("");
else else
{ {
ptr = langname = xstrdup(lc_type); ptr = langname = pg_strdup(lc_type);
while (*ptr && *ptr != '_' && *ptr != '.' && *ptr != '@') while (*ptr && *ptr != '_' && *ptr != '.' && *ptr != '@')
ptr++; ptr++;
*ptr = '\0'; *ptr = '\0';
@ -1410,10 +1410,10 @@ bootstrap_template1(void)
* there doesn't seem to be any compelling reason to do that. * there doesn't seem to be any compelling reason to do that.
*/ */
snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate); 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); snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
putenv(xstrdup(cmd)); putenv(pg_strdup(cmd));
unsetenv("LC_ALL"); unsetenv("LC_ALL");
@ -1531,7 +1531,7 @@ get_set_pwd(void)
while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n')) while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n'))
pwdbuf[--i] = '\0'; pwdbuf[--i] = '\0';
pwd1 = xstrdup(pwdbuf); pwd1 = pg_strdup(pwdbuf);
} }
printf(_("setting password ... ")); printf(_("setting password ... "));
@ -2065,7 +2065,7 @@ set_info_version(void)
minor = 0, minor = 0,
micro = 0; micro = 0;
char *endptr; char *endptr;
char *vstr = xstrdup(PG_VERSION); char *vstr = pg_strdup(PG_VERSION);
char *ptr; char *ptr;
ptr = vstr + (strlen(vstr) - 1); ptr = vstr + (strlen(vstr) - 1);
@ -2433,7 +2433,7 @@ locale_date_order(const char *locale)
save = setlocale(LC_TIME, NULL); save = setlocale(LC_TIME, NULL);
if (!save) if (!save)
return result; return result;
save = xstrdup(save); save = pg_strdup(save);
setlocale(LC_TIME, locale); 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 */ return false; /* won't happen, we hope */
/* save may be pointing at a modifiable scratch variable, so copy it. */ /* 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. */ /* set the locale with setlocale, to see if it accepts it. */
res = setlocale(category, locale); res = setlocale(category, locale);
/* save canonical name if requested. */ /* save canonical name if requested. */
if (res && canonname) if (res && canonname)
*canonname = xstrdup(res); *canonname = pg_strdup(res);
/* restore old value. */ /* restore old value. */
if (!setlocale(category, save)) if (!setlocale(category, save))
@ -2588,34 +2588,34 @@ setlocales(void)
if (check_locale_name(LC_CTYPE, lc_ctype, &canonname)) if (check_locale_name(LC_CTYPE, lc_ctype, &canonname))
lc_ctype = canonname; lc_ctype = canonname;
else 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)) if (check_locale_name(LC_COLLATE, lc_collate, &canonname))
lc_collate = canonname; lc_collate = canonname;
else 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)) if (check_locale_name(LC_NUMERIC, lc_numeric, &canonname))
lc_numeric = canonname; lc_numeric = canonname;
else 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)) if (check_locale_name(LC_TIME, lc_time, &canonname))
lc_time = canonname; lc_time = canonname;
else 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)) if (check_locale_name(LC_MONETARY, lc_monetary, &canonname))
lc_monetary = canonname; lc_monetary = canonname;
else else
lc_monetary = xstrdup(setlocale(LC_MONETARY, NULL)); lc_monetary = pg_strdup(setlocale(LC_MONETARY, NULL));
#if defined(LC_MESSAGES) && !defined(WIN32) #if defined(LC_MESSAGES) && !defined(WIN32)
if (check_locale_name(LC_MESSAGES, lc_messages, &canonname)) if (check_locale_name(LC_MESSAGES, lc_messages, &canonname))
lc_messages = canonname; lc_messages = canonname;
else else
lc_messages = xstrdup(setlocale(LC_MESSAGES, NULL)); lc_messages = pg_strdup(setlocale(LC_MESSAGES, NULL));
#else #else
/* when LC_MESSAGES is not available, use the LC_CTYPE setting */ /* when LC_MESSAGES is not available, use the LC_CTYPE setting */
if (check_locale_name(LC_CTYPE, lc_messages, &canonname)) if (check_locale_name(LC_CTYPE, lc_messages, &canonname))
lc_messages = canonname; lc_messages = canonname;
else else
lc_messages = xstrdup(setlocale(LC_CTYPE, NULL)); lc_messages = pg_strdup(setlocale(LC_CTYPE, NULL));
#endif #endif
} }
@ -2910,7 +2910,7 @@ main(int argc, char *argv[])
switch (c) switch (c)
{ {
case 'A': case 'A':
authmethodlocal = authmethodhost = xstrdup(optarg); authmethodlocal = authmethodhost = pg_strdup(optarg);
/* /*
* When ident is specified, use peer for local connections. * When ident is specified, use peer for local connections.
@ -2923,22 +2923,22 @@ main(int argc, char *argv[])
authmethodhost = "ident"; authmethodhost = "ident";
break; break;
case 10: case 10:
authmethodlocal = xstrdup(optarg); authmethodlocal = pg_strdup(optarg);
break; break;
case 11: case 11:
authmethodhost = xstrdup(optarg); authmethodhost = pg_strdup(optarg);
break; break;
case 'D': case 'D':
pg_data = xstrdup(optarg); pg_data = pg_strdup(optarg);
break; break;
case 'E': case 'E':
encoding = xstrdup(optarg); encoding = pg_strdup(optarg);
break; break;
case 'W': case 'W':
pwprompt = true; pwprompt = true;
break; break;
case 'U': case 'U':
username = xstrdup(optarg); username = pg_strdup(optarg);
break; break;
case 'd': case 'd':
debug = true; debug = true;
@ -2952,43 +2952,43 @@ main(int argc, char *argv[])
do_sync = false; do_sync = false;
break; break;
case 'L': case 'L':
share_path = xstrdup(optarg); share_path = pg_strdup(optarg);
break; break;
case 1: case 1:
locale = xstrdup(optarg); locale = pg_strdup(optarg);
break; break;
case 2: case 2:
lc_collate = xstrdup(optarg); lc_collate = pg_strdup(optarg);
break; break;
case 3: case 3:
lc_ctype = xstrdup(optarg); lc_ctype = pg_strdup(optarg);
break; break;
case 4: case 4:
lc_monetary = xstrdup(optarg); lc_monetary = pg_strdup(optarg);
break; break;
case 5: case 5:
lc_numeric = xstrdup(optarg); lc_numeric = pg_strdup(optarg);
break; break;
case 6: case 6:
lc_time = xstrdup(optarg); lc_time = pg_strdup(optarg);
break; break;
case 7: case 7:
lc_messages = xstrdup(optarg); lc_messages = pg_strdup(optarg);
break; break;
case 8: case 8:
locale = "C"; locale = "C";
break; break;
case 9: case 9:
pwfilename = xstrdup(optarg); pwfilename = pg_strdup(optarg);
break; break;
case 's': case 's':
show_setting = true; show_setting = true;
break; break;
case 'T': case 'T':
default_text_search_config = xstrdup(optarg); default_text_search_config = pg_strdup(optarg);
break; break;
case 'X': case 'X':
xlog_dir = xstrdup(optarg); xlog_dir = pg_strdup(optarg);
break; break;
default: default:
/* getopt_long already emitted a complaint */ /* getopt_long already emitted a complaint */
@ -3005,7 +3005,7 @@ main(int argc, char *argv[])
*/ */
if (optind < argc && strlen(pg_data) == 0) if (optind < argc && strlen(pg_data) == 0)
{ {
pg_data = xstrdup(argv[optind]); pg_data = pg_strdup(argv[optind]);
optind++; optind++;
} }
@ -3038,7 +3038,7 @@ main(int argc, char *argv[])
if (pgdenv && strlen(pgdenv)) if (pgdenv && strlen(pgdenv))
{ {
/* PGDATA found */ /* PGDATA found */
pg_data = xstrdup(pgdenv); pg_data = pg_strdup(pgdenv);
} }
else else
{ {
@ -3069,7 +3069,7 @@ main(int argc, char *argv[])
ZeroMemory(&pi, sizeof(pi)); ZeroMemory(&pi, sizeof(pi));
cmdline = xstrdup(GetCommandLine()); cmdline = pg_strdup(GetCommandLine());
putenv("PG_RESTRICT_EXEC=1"); putenv("PG_RESTRICT_EXEC=1");

View File

@ -263,7 +263,7 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier)
uint32 hi, uint32 hi,
lo; lo;
param = xmalloc0(sizeof(logstreamer_param)); param = pg_malloc0(sizeof(logstreamer_param));
param->timeline = timeline; param->timeline = timeline;
param->sysidentifier = sysidentifier; param->sysidentifier = sysidentifier;
@ -977,7 +977,7 @@ BaseBackup(void)
progname, PQntuples(res), PQnfields(res), 1, 3); progname, PQntuples(res), PQnfields(res), 1, 3);
disconnect_and_exit(1); disconnect_and_exit(1);
} }
sysidentifier = strdup(PQgetvalue(res, 0, 0)); sysidentifier = pg_strdup(PQgetvalue(res, 0, 0));
timeline = atoi(PQgetvalue(res, 0, 1)); timeline = atoi(PQgetvalue(res, 0, 1));
PQclear(res); PQclear(res);
@ -1286,7 +1286,7 @@ main(int argc, char **argv)
switch (c) switch (c)
{ {
case 'D': case 'D':
basedir = xstrdup(optarg); basedir = pg_strdup(optarg);
break; break;
case 'F': case 'F':
if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0) if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
@ -1338,7 +1338,7 @@ main(int argc, char **argv)
} }
break; break;
case 'l': case 'l':
label = xstrdup(optarg); label = pg_strdup(optarg);
break; break;
case 'z': case 'z':
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
@ -1369,13 +1369,13 @@ main(int argc, char **argv)
} }
break; break;
case 'h': case 'h':
dbhost = xstrdup(optarg); dbhost = pg_strdup(optarg);
break; break;
case 'p': case 'p':
dbport = xstrdup(optarg); dbport = pg_strdup(optarg);
break; break;
case 'U': case 'U':
dbuser = xstrdup(optarg); dbuser = pg_strdup(optarg);
break; break;
case 'w': case 'w':
dbgetpassword = -1; dbgetpassword = -1;

View File

@ -342,10 +342,10 @@ main(int argc, char **argv)
switch (c) switch (c)
{ {
case 'D': case 'D':
basedir = xstrdup(optarg); basedir = pg_strdup(optarg);
break; break;
case 'h': case 'h':
dbhost = xstrdup(optarg); dbhost = pg_strdup(optarg);
break; break;
case 'p': case 'p':
if (atoi(optarg) <= 0) if (atoi(optarg) <= 0)
@ -354,10 +354,10 @@ main(int argc, char **argv)
progname, optarg); progname, optarg);
exit(1); exit(1);
} }
dbport = xstrdup(optarg); dbport = pg_strdup(optarg);
break; break;
case 'U': case 'U':
dbuser = xstrdup(optarg); dbuser = pg_strdup(optarg);
break; break;
case 'w': case 'w':
dbgetpassword = -1; dbgetpassword = -1;

View File

@ -97,7 +97,7 @@ open_walfile(XLogRecPtr startpoint, uint32 timeline, char *basedir,
} }
/* New, empty, file. So pad it to 16Mb with zeroes */ /* 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) for (bytes = 0; bytes < XLogSegSize; bytes += XLOG_BLCKSZ)
{ {
if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ) if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)

View File

@ -32,11 +32,11 @@ static char *dbpassword = NULL;
PGconn *conn = 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. * if something goes wrong. Can never return NULL.
*/ */
char * char *
xstrdup(const char *s) pg_strdup(const char *s)
{ {
char *result; char *result;
@ -50,7 +50,7 @@ xstrdup(const char *s)
} }
void * void *
xmalloc0(int size) pg_malloc0(size_t size)
{ {
void *result; void *result;
@ -89,8 +89,8 @@ GetConnection(void)
if (dbport) if (dbport)
argcount++; argcount++;
keywords = xmalloc0((argcount + 1) * sizeof(*keywords)); keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
values = xmalloc0((argcount + 1) * sizeof(*values)); values = pg_malloc0((argcount + 1) * sizeof(*values));
keywords[0] = "dbname"; keywords[0] = "dbname";
values[0] = "replication"; values[0] = "replication";

View File

@ -16,7 +16,7 @@ extern PGconn *conn;
} }
char *xstrdup(const char *s); extern char *pg_strdup(const char *s);
void *xmalloc0(int size); extern void *pg_malloc0(size_t size);
PGconn *GetConnection(void); extern PGconn *GetConnection(void);

View File

@ -118,7 +118,7 @@ write_stderr(const char *fmt,...)
the supplied arguments. */ the supplied arguments. */
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2))); __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
static void *pg_malloc(size_t size); 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_advice(void);
static void do_help(void); static void do_help(void);
static void set_mode(char *modeopt); static void set_mode(char *modeopt);
@ -244,7 +244,7 @@ pg_malloc(size_t size)
static char * static char *
xstrdup(const char *s) pg_strdup(const char *s)
{ {
char *result; char *result;
@ -351,7 +351,7 @@ readfile(const char *path)
rewind(infile); rewind(infile);
nlines = 0; nlines = 0;
while (fgets(buffer, maxlength + 1, infile) != NULL) while (fgets(buffer, maxlength + 1, infile) != NULL)
result[nlines++] = xstrdup(buffer); result[nlines++] = pg_strdup(buffer);
fclose(infile); fclose(infile);
free(buffer); free(buffer);
@ -1931,7 +1931,7 @@ adjust_data_dir(void)
if (exec_path == NULL) if (exec_path == NULL)
my_exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR); my_exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR);
else 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, snprintf(cmd, MAXPGPATH, SYSTEMQUOTE "\"%s\" %s%s -C data_directory" SYSTEMQUOTE,
my_exec_path, pgdata_opt ? pgdata_opt : "", post_opts ? my_exec_path, pgdata_opt ? pgdata_opt : "", post_opts ?
@ -1951,7 +1951,7 @@ adjust_data_dir(void)
*strchr(filename, '\n') = '\0'; *strchr(filename, '\n') = '\0';
free(pg_data); free(pg_data);
pg_data = xstrdup(filename); pg_data = pg_strdup(filename);
canonicalize_path(pg_data); canonicalize_path(pg_data);
} }
@ -2042,7 +2042,7 @@ main(int argc, char **argv)
char *pgdata_D; char *pgdata_D;
char *env_var = pg_malloc(strlen(optarg) + 8); char *env_var = pg_malloc(strlen(optarg) + 8);
pgdata_D = xstrdup(optarg); pgdata_D = pg_strdup(optarg);
canonicalize_path(pgdata_D); canonicalize_path(pgdata_D);
snprintf(env_var, strlen(optarg) + 8, "PGDATA=%s", snprintf(env_var, strlen(optarg) + 8, "PGDATA=%s",
pgdata_D); pgdata_D);
@ -2060,22 +2060,22 @@ main(int argc, char **argv)
break; break;
} }
case 'l': case 'l':
log_file = xstrdup(optarg); log_file = pg_strdup(optarg);
break; break;
case 'm': case 'm':
set_mode(optarg); set_mode(optarg);
break; break;
case 'N': case 'N':
register_servicename = xstrdup(optarg); register_servicename = pg_strdup(optarg);
break; break;
case 'o': case 'o':
post_opts = xstrdup(optarg); post_opts = pg_strdup(optarg);
break; break;
case 'p': case 'p':
exec_path = xstrdup(optarg); exec_path = pg_strdup(optarg);
break; break;
case 'P': case 'P':
register_password = xstrdup(optarg); register_password = pg_strdup(optarg);
break; break;
case 's': case 's':
silent_mode = true; silent_mode = true;
@ -2094,16 +2094,11 @@ main(int argc, char **argv)
break; break;
case 'U': case 'U':
if (strchr(optarg, '\\')) if (strchr(optarg, '\\'))
register_username = xstrdup(optarg); register_username = pg_strdup(optarg);
else else
/* Prepend .\ for local accounts */ /* Prepend .\ for local accounts */
{ {
register_username = malloc(strlen(optarg) + 3); register_username = pg_malloc(strlen(optarg) + 3);
if (!register_username)
{
write_stderr(_("%s: out of memory\n"), progname);
exit(1);
}
strcpy(register_username, ".\\"); strcpy(register_username, ".\\");
strcat(register_username, optarg); strcat(register_username, optarg);
} }
@ -2192,9 +2187,9 @@ main(int argc, char **argv)
pg_config = getenv("PGDATA"); pg_config = getenv("PGDATA");
if (pg_config) if (pg_config)
{ {
pg_config = xstrdup(pg_config); pg_config = pg_strdup(pg_config);
canonicalize_path(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 */ /* -D might point at config-only directory; if so find the real PGDATA */

View File

@ -138,7 +138,7 @@ AllocateCompressor(int compression, WriteFunc writeF)
exit_horribly(modulename, "not built with zlib support\n"); exit_horribly(modulename, "not built with zlib support\n");
#endif #endif
cs = (CompressorState *) pg_calloc(1, sizeof(CompressorState)); cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
cs->writeF = writeF; cs->writeF = writeF;
cs->comprAlg = alg; cs->comprAlg = alg;

View File

@ -49,13 +49,12 @@ pg_malloc(size_t size)
} }
void * void *
pg_calloc(size_t nmemb, size_t size) pg_malloc0(size_t size)
{ {
void *tmp; void *tmp;
tmp = calloc(nmemb, size); tmp = pg_malloc(size);
if (!tmp) MemSet(tmp, 0, size);
exit_horribly(NULL, "out of memory\n");
return tmp; return tmp;
} }

View File

@ -16,7 +16,7 @@
extern char *pg_strdup(const char *string); extern char *pg_strdup(const char *string);
extern void *pg_malloc(size_t size); 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); extern void *pg_realloc(void *ptr, size_t size);
#endif /* DUMPMEM_H */ #endif /* DUMPMEM_H */

View File

@ -748,7 +748,7 @@ NewRestoreOptions(void)
{ {
RestoreOptions *opts; RestoreOptions *opts;
opts = (RestoreOptions *) pg_calloc(1, sizeof(RestoreOptions)); opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
/* set any fields that shouldn't default to zeroes */ /* set any fields that shouldn't default to zeroes */
opts->format = archUnknown; opts->format = archUnknown;
@ -848,7 +848,7 @@ ArchiveEntry(Archive *AHX,
ArchiveHandle *AH = (ArchiveHandle *) AHX; ArchiveHandle *AH = (ArchiveHandle *) AHX;
TocEntry *newToc; TocEntry *newToc;
newToc = (TocEntry *) pg_calloc(1, sizeof(TocEntry)); newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry));
AH->tocCount++; AH->tocCount++;
if (dumpId > AH->maxDumpId) if (dumpId > AH->maxDumpId)
@ -1594,8 +1594,8 @@ buildTocEntryArrays(ArchiveHandle *AH)
DumpId maxDumpId = AH->maxDumpId; DumpId maxDumpId = AH->maxDumpId;
TocEntry *te; TocEntry *te;
AH->tocsByDumpId = (TocEntry **) pg_calloc(maxDumpId + 1, sizeof(TocEntry *)); AH->tocsByDumpId = (TocEntry **) pg_malloc0((maxDumpId + 1) * sizeof(TocEntry *));
AH->tableDataId = (DumpId *) pg_calloc(maxDumpId + 1, sizeof(DumpId)); AH->tableDataId = (DumpId *) pg_malloc0((maxDumpId + 1) * sizeof(DumpId));
for (te = AH->toc->next; te != AH->toc; te = te->next) for (te = AH->toc->next; te != AH->toc; te = te->next)
{ {
@ -1845,7 +1845,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
free(AH->lookahead); free(AH->lookahead);
AH->lookaheadSize = 512; AH->lookaheadSize = 512;
AH->lookahead = pg_calloc(1, 512); AH->lookahead = pg_malloc0(512);
AH->lookaheadLen = 0; AH->lookaheadLen = 0;
AH->lookaheadPos = 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); write_msg(modulename, "allocating AH for %s, format %d\n", FileSpec, fmt);
#endif #endif
AH = (ArchiveHandle *) pg_calloc(1, sizeof(ArchiveHandle)); AH = (ArchiveHandle *) pg_malloc0(sizeof(ArchiveHandle));
/* AH->debugLevel = 100; */ /* AH->debugLevel = 100; */
@ -2065,7 +2065,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
AH->currTablespace = NULL; /* ditto */ AH->currTablespace = NULL; /* ditto */
AH->currWithOids = -1; /* force SET */ 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->next = AH->toc;
AH->toc->prev = AH->toc; AH->toc->prev = AH->toc;
@ -2246,7 +2246,7 @@ ReadToc(ArchiveHandle *AH)
for (i = 0; i < AH->tocCount; i++) for (i = 0; i < AH->tocCount; i++)
{ {
te = (TocEntry *) pg_calloc(1, sizeof(TocEntry)); te = (TocEntry *) pg_malloc0(sizeof(TocEntry));
te->dumpId = ReadInt(AH); te->dumpId = ReadInt(AH);
if (te->dumpId > AH->maxDumpId) if (te->dumpId > AH->maxDumpId)
@ -3485,9 +3485,9 @@ restore_toc_entries_parallel(ArchiveHandle *AH)
ahlog(AH, 2, "entering restore_toc_entries_parallel\n"); 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 = (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; pstate->numWorkers = ropt->number_of_jobs;
for (i = 0; i < pstate->numWorkers; i++) for (i = 0; i < pstate->numWorkers; i++)
unsetProcessIdentifier(&(pstate->pse[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 */ /* first time around only, make space for handles to listen on */
if (handles == NULL) 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 */ /* set up list of handles to listen to */
for (snum = 0, tnum = 0; snum < n_slots; snum++) for (snum = 0, tnum = 0; snum < n_slots; snum++)

View File

@ -129,7 +129,7 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
AH->DeClonePtr = _DeClone; AH->DeClonePtr = _DeClone;
/* Set up a private area. */ /* Set up a private area. */
ctx = (lclContext *) pg_calloc(1, sizeof(lclContext)); ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
AH->formatData = (void *) ctx; AH->formatData = (void *) ctx;
/* Initialize LO buffering */ /* Initialize LO buffering */
@ -198,7 +198,7 @@ _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
{ {
lclTocEntry *ctx; lclTocEntry *ctx;
ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
if (te->dataDumper) if (te->dataDumper)
ctx->dataState = K_OFFSET_POS_NOT_SET; ctx->dataState = K_OFFSET_POS_NOT_SET;
else else
@ -239,7 +239,7 @@ _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
if (ctx == NULL) if (ctx == NULL)
{ {
ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
te->formatData = (void *) ctx; te->formatData = (void *) ctx;
} }

View File

@ -126,7 +126,7 @@ InitArchiveFmt_Directory(ArchiveHandle *AH)
AH->DeClonePtr = NULL; AH->DeClonePtr = NULL;
/* Set up our private context */ /* Set up our private context */
ctx = (lclContext *) pg_calloc(1, sizeof(lclContext)); ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
AH->formatData = (void *) ctx; AH->formatData = (void *) ctx;
ctx->dataFH = NULL; ctx->dataFH = NULL;
@ -194,7 +194,7 @@ _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
lclTocEntry *tctx; lclTocEntry *tctx;
char fn[MAXPGPATH]; char fn[MAXPGPATH];
tctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
if (te->dataDumper) if (te->dataDumper)
{ {
snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId); snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
@ -243,7 +243,7 @@ _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
if (tctx == NULL) if (tctx == NULL)
{ {
tctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
te->formatData = (void *) tctx; te->formatData = (void *) tctx;
} }

View File

@ -159,7 +159,7 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
/* /*
* Set up some special context used in compressing data. * 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; AH->formatData = (void *) ctx;
ctx->filePos = 0; ctx->filePos = 0;
ctx->isSpecialScript = 0; ctx->isSpecialScript = 0;
@ -266,7 +266,7 @@ _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
lclTocEntry *ctx; lclTocEntry *ctx;
char fn[K_STD_BUF_SIZE]; char fn[K_STD_BUF_SIZE];
ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
if (te->dataDumper != NULL) if (te->dataDumper != NULL)
{ {
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
@ -305,7 +305,7 @@ _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
if (ctx == NULL) if (ctx == NULL)
{ {
ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry)); ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
te->formatData = (void *) ctx; te->formatData = (void *) ctx;
} }
@ -378,7 +378,7 @@ tarOpen(ArchiveHandle *AH, const char *filename, char mode)
} }
else else
{ {
tm = pg_calloc(1, sizeof(TAR_MEMBER)); tm = pg_malloc0(sizeof(TAR_MEMBER));
#ifndef WIN32 #ifndef WIN32
tm->tmpFH = tmpfile(); tm->tmpFH = tmpfile();
@ -1128,7 +1128,7 @@ static TAR_MEMBER *
_tarPositionTo(ArchiveHandle *AH, const char *filename) _tarPositionTo(ArchiveHandle *AH, const char *filename)
{ {
lclContext *ctx = (lclContext *) AH->formatData; 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 c;
char header[512]; char header[512];
size_t i, size_t i,

View File

@ -3776,7 +3776,7 @@ getFuncs(Archive *fout, int *numFuncs)
*numFuncs = ntups; *numFuncs = ntups;
finfo = (FuncInfo *) pg_calloc(ntups, sizeof(FuncInfo)); finfo = (FuncInfo *) pg_malloc0(ntups * sizeof(FuncInfo));
i_tableoid = PQfnumber(res, "tableoid"); i_tableoid = PQfnumber(res, "tableoid");
i_oid = PQfnumber(res, "oid"); 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 * only one, because we don't yet know which tables might be inheritance
* ancestors of the target table. * 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_reltableoid = PQfnumber(res, "tableoid");
i_reloid = PQfnumber(res, "oid"); i_reloid = PQfnumber(res, "oid");

View File

@ -552,7 +552,7 @@ findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs)
bool fixedloop; bool fixedloop;
int i; 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 *)); workspace = (DumpableObject **) pg_malloc(totObjs * sizeof(DumpableObject *));
fixedloop = false; fixedloop = false;

View File

@ -1472,7 +1472,7 @@ prompt_for_password(const char *username)
{ {
char *prompt_text; char *prompt_text;
prompt_text = malloc(strlen(username) + 100); prompt_text = pg_malloc(strlen(username) + 100);
snprintf(prompt_text, strlen(username) + 100, snprintf(prompt_text, strlen(username) + 100,
_("Password for user %s: "), username); _("Password for user %s: "), username);
result = simple_prompt(prompt_text, 100, false); 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) else if (o_conn && user && strcmp(PQuser(o_conn), user) == 0)
{ {
password = strdup(PQpass(o_conn)); password = pg_strdup(PQpass(o_conn));
} }
while (true) while (true)

View File

@ -70,26 +70,12 @@ pg_malloc(size_t size)
} }
void * void *
pg_malloc_zero(size_t size) pg_malloc0(size_t size)
{ {
void *tmp; void *tmp;
tmp = pg_malloc(size); tmp = pg_malloc(size);
memset(tmp, 0, 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);
}
return tmp; return tmp;
} }

View File

@ -28,8 +28,7 @@
*/ */
extern char *pg_strdup(const char *string); extern char *pg_strdup(const char *string);
extern void *pg_malloc(size_t size); extern void *pg_malloc(size_t size);
extern void *pg_malloc_zero(size_t size); extern void *pg_malloc0(size_t size);
extern void *pg_calloc(size_t nmemb, size_t size);
extern bool setQFout(const char *fname); extern bool setQFout(const char *fname);

View File

@ -97,7 +97,7 @@ parse_slash_copy(const char *args)
return NULL; 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 */ result->before_tofrom = pg_strdup(""); /* initialize for appending */

View File

@ -1236,12 +1236,12 @@ describeOneTableDetails(const char *schemaname,
tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0; tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0; tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
tableinfo.reloptions = (pset.sversion >= 80200) ? tableinfo.reloptions = (pset.sversion >= 80200) ?
strdup(PQgetvalue(res, 0, 6)) : NULL; pg_strdup(PQgetvalue(res, 0, 6)) : NULL;
tableinfo.tablespace = (pset.sversion >= 80000) ? tableinfo.tablespace = (pset.sversion >= 80000) ?
atooid(PQgetvalue(res, 0, 7)) : 0; atooid(PQgetvalue(res, 0, 7)) : 0;
tableinfo.reloftype = (pset.sversion >= 90000 && tableinfo.reloftype = (pset.sversion >= 90000 &&
strcmp(PQgetvalue(res, 0, 8), "") != 0) ? strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
strdup(PQgetvalue(res, 0, 8)) : NULL; pg_strdup(PQgetvalue(res, 0, 8)) : NULL;
tableinfo.relpersistence = (pset.sversion >= 90100) ? tableinfo.relpersistence = (pset.sversion >= 90100) ?
*(PQgetvalue(res, 0, 9)) : 0; *(PQgetvalue(res, 0, 9)) : 0;
PQclear(res); PQclear(res);
@ -1382,7 +1382,7 @@ describeOneTableDetails(const char *schemaname,
{ {
show_modifiers = true; show_modifiers = true;
headers[cols++] = gettext_noop("Modifiers"); headers[cols++] = gettext_noop("Modifiers");
modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers)); modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers));
} }
if (tableinfo.relkind == 'S') if (tableinfo.relkind == 'S')
@ -2417,7 +2417,7 @@ describeRoles(const char *pattern, bool verbose)
return false; return false;
nrows = PQntuples(res); 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); printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);

View File

@ -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 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 static int
integer_digits(const char *my_str) integer_digits(const char *my_str)
{ {
@ -210,8 +182,7 @@ format_numeric_locale(const char *my_str)
leading_digits; leading_digits;
int groupdigits = atoi(grouping); int groupdigits = atoi(grouping);
int new_str_start = 0; int new_str_start = 0;
char *new_str = pg_local_malloc( char *new_str = pg_malloc(strlen_with_numeric_locale(my_str) + 1);
strlen_with_numeric_locale(my_str) + 1);
leading_digits = (int_len % groupdigits != 0) ? leading_digits = (int_len % groupdigits != 0) ?
int_len % groupdigits : groupdigits; int_len % groupdigits : groupdigits;
@ -571,18 +542,18 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
if (cont->ncolumns > 0) if (cont->ncolumns > 0)
{ {
col_count = cont->ncolumns; col_count = cont->ncolumns;
width_header = pg_local_calloc(col_count, sizeof(*width_header)); width_header = pg_malloc0(col_count * sizeof(*width_header));
width_average = pg_local_calloc(col_count, sizeof(*width_average)); width_average = pg_malloc0(col_count * sizeof(*width_average));
max_width = pg_local_calloc(col_count, sizeof(*max_width)); max_width = pg_malloc0(col_count * sizeof(*max_width));
width_wrap = pg_local_calloc(col_count, sizeof(*width_wrap)); width_wrap = pg_malloc0(col_count * sizeof(*width_wrap));
max_nl_lines = pg_local_calloc(col_count, sizeof(*max_nl_lines)); max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines));
curr_nl_line = pg_local_calloc(col_count, sizeof(*curr_nl_line)); curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line));
col_lineptrs = pg_local_calloc(col_count, sizeof(*col_lineptrs)); col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs));
max_bytes = pg_local_calloc(col_count, sizeof(*max_bytes)); max_bytes = pg_malloc0(col_count * sizeof(*max_bytes));
format_buf = pg_local_calloc(col_count, sizeof(*format_buf)); format_buf = pg_malloc0(col_count * sizeof(*format_buf));
header_done = pg_local_calloc(col_count, sizeof(*header_done)); header_done = pg_malloc0(col_count * sizeof(*header_done));
bytes_output = pg_local_calloc(col_count, sizeof(*bytes_output)); bytes_output = pg_malloc0(col_count * sizeof(*bytes_output));
wrap = pg_local_calloc(col_count, sizeof(*wrap)); wrap = pg_malloc0(col_count * sizeof(*wrap));
} }
else else
{ {
@ -678,10 +649,10 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
for (i = 0; i < col_count; i++) for (i = 0; i < col_count; i++)
{ {
/* Add entry for ptr == NULL array termination */ /* Add entry for ptr == NULL array termination */
col_lineptrs[i] = pg_local_calloc(max_nl_lines[i] + 1, col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) *
sizeof(**col_lineptrs)); 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]; 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 * We now have all the information we need to setup the formatting
* structures * structures
*/ */
dlineptr = pg_local_malloc((sizeof(*dlineptr)) * (dheight + 1)); dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1));
hlineptr = pg_local_malloc((sizeof(*hlineptr)) * (hheight + 1)); hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1));
dlineptr->ptr = pg_local_malloc(dformatsize); dlineptr->ptr = pg_malloc(dformatsize);
hlineptr->ptr = pg_local_malloc(hformatsize); hlineptr->ptr = pg_malloc(hformatsize);
if (cont->opt->start_table) if (cont->opt->start_table)
{ {
@ -2132,17 +2103,14 @@ printTableInit(printTableContent *const content, const printTableOpt *opt,
content->ncolumns = ncolumns; content->ncolumns = ncolumns;
content->nrows = nrows; content->nrows = nrows;
content->headers = pg_local_calloc(ncolumns + 1, content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
sizeof(*content->headers));
content->cells = pg_local_calloc(ncolumns * nrows + 1, content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells));
sizeof(*content->cells));
content->cellmustfree = NULL; content->cellmustfree = NULL;
content->footers = NULL; content->footers = NULL;
content->aligns = pg_local_calloc(ncolumns + 1, content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align));
sizeof(*content->align));
content->header = content->headers; content->header = content->headers;
content->cell = content->cells; content->cell = content->cells;
@ -2230,8 +2198,8 @@ printTableAddCell(printTableContent *const content, char *cell,
if (mustfree) if (mustfree)
{ {
if (content->cellmustfree == NULL) if (content->cellmustfree == NULL)
content->cellmustfree = pg_local_calloc( content->cellmustfree =
content->ncolumns * content->nrows + 1, sizeof(bool)); pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
content->cellmustfree[content->cellsadded] = true; content->cellmustfree[content->cellsadded] = true;
} }
@ -2256,7 +2224,7 @@ printTableAddFooter(printTableContent *const content, const char *footer)
{ {
printTableFooter *f; printTableFooter *f;
f = pg_local_calloc(1, sizeof(*f)); f = pg_malloc0(sizeof(*f));
f->data = pg_strdup(footer); f->data = pg_strdup(footer);
if (content->footers == NULL) if (content->footers == NULL)

View File

@ -1150,7 +1150,7 @@ psql_scan_create(void)
{ {
PsqlScanState state; PsqlScanState state;
state = (PsqlScanStateData *) pg_malloc_zero(sizeof(PsqlScanStateData)); state = (PsqlScanStateData *) pg_malloc0(sizeof(PsqlScanStateData));
psql_scan_reset(state); psql_scan_reset(state);

View File

@ -188,8 +188,8 @@ main(int argc, char *argv[])
password_prompt = pg_strdup(_("Password: ")); password_prompt = pg_strdup(_("Password: "));
else else
{ {
password_prompt = malloc(strlen(_("Password for user %s: ")) - 2 + password_prompt = pg_malloc(strlen(_("Password for user %s: ")) - 2 +
strlen(options.username) + 1); strlen(options.username) + 1);
sprintf(password_prompt, _("Password for user %s: "), sprintf(password_prompt, _("Password for user %s: "),
options.username); options.username);
} }

View File

@ -109,14 +109,8 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
do do
{ {
#define PARAMS_ARRAY_SIZE 7 #define PARAMS_ARRAY_SIZE 7
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords)); const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values)); const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
if (!keywords || !values)
{
fprintf(stderr, _("%s: out of memory\n"), progname);
exit(1);
}
keywords[0] = "host"; keywords[0] = "host";
values[0] = pghost; values[0] = pghost;
@ -305,6 +299,30 @@ pg_strdup(const char *string)
return tmp; 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. * Check yes/no answer in a localized way. 1=yes, 0=no, -1=neither.
*/ */

View File

@ -51,5 +51,7 @@ extern bool yesno_prompt(const char *question);
extern void setup_cancel_handler(void); extern void setup_cancel_handler(void);
extern char *pg_strdup(const char *string); extern char *pg_strdup(const char *string);
extern void *pg_malloc(size_t size);
extern void *pg_malloc0(size_t size);
#endif /* COMMON_H */ #endif /* COMMON_H */