Commit 610d33c1 authored by Neil Conway's avatar Neil Conway

This patch makes some of the memory manipulation performed by psql a

little more sane. Some parts of the code was using a static function
xmalloc() that did safe memory allocation (where "safe" means "bail
out on OOM"), but most of it was just invoking calloc() or malloc()
directly. Now almost everything invokes xmalloc() or xcalloc().
parent cb3dc829
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.109 2004/01/09 21:12:20 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.110 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "command.h" #include "command.h"
...@@ -1156,13 +1156,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon) ...@@ -1156,13 +1156,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon)
/* Copy the option */ /* Copy the option */
token_len = cp - &options_string[pos]; token_len = cp - &options_string[pos];
return_val = malloc(token_len + 1); return_val = xmalloc(token_len + 1);
if (!return_val)
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
memcpy(return_val, &options_string[pos], token_len); memcpy(return_val, &options_string[pos], token_len);
return_val[token_len] = '\0'; return_val[token_len] = '\0';
...@@ -1235,7 +1229,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon) ...@@ -1235,7 +1229,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon)
* *
* Replaces \n, \t, and the like. * Replaces \n, \t, and the like.
* *
* The return value is malloc()'ed. * The return value is malloc'ed.
*/ */
static char * static char *
unescape(const unsigned char *source, size_t len) unescape(const unsigned char *source, size_t len)
...@@ -1251,12 +1245,7 @@ unescape(const unsigned char *source, size_t len) ...@@ -1251,12 +1245,7 @@ unescape(const unsigned char *source, size_t len)
length = Min(len, strlen(source)) + 1; length = Min(len, strlen(source)) + 1;
tmp = destination = malloc(length); tmp = destination = xmalloc(length);
if (!tmp)
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
for (p = source; p - source < (int) len && *p; p += PQmblen(p, pset.encoding)) for (p = source; p - source < (int) len && *p; p += PQmblen(p, pset.encoding))
{ {
...@@ -1537,9 +1526,7 @@ editFile(const char *fname) ...@@ -1537,9 +1526,7 @@ editFile(const char *fname)
if (!editorName) if (!editorName)
editorName = DEFAULT_EDITOR; editorName = DEFAULT_EDITOR;
sys = malloc(strlen(editorName) + strlen(fname) + 10 + 1); sys = xmalloc(strlen(editorName) + strlen(fname) + 10 + 1);
if (!sys)
return false;
sprintf(sys, sprintf(sys,
#ifndef WIN32 #ifndef WIN32
"exec " "exec "
...@@ -1959,15 +1946,7 @@ do_shell(const char *command) ...@@ -1959,15 +1946,7 @@ do_shell(const char *command)
if (shellName == NULL) if (shellName == NULL)
shellName = DEFAULT_SHELL; shellName = DEFAULT_SHELL;
sys = malloc(strlen(shellName) + 16); sys = xmalloc(strlen(shellName) + 16);
if (!sys)
{
psql_error("out of memory\n");
if (pset.cur_cmd_interactive)
return false;
else
exit(EXIT_FAILURE);
}
sprintf(sys, sprintf(sys,
#ifndef WIN32 #ifndef WIN32
"exec " "exec "
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.80 2004/01/20 23:48:56 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.81 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -89,6 +89,43 @@ xstrdup(const char *string) ...@@ -89,6 +89,43 @@ xstrdup(const char *string)
return tmp; return tmp;
} }
void *
xmalloc(size_t size)
{
void *tmp;
tmp = malloc(size);
if (!tmp)
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
return tmp;
}
void *
xmalloc_zero(size_t size)
{
void *tmp;
tmp = xmalloc(size);
memset(tmp, 0, size);
return tmp;
}
void *
xcalloc(size_t nmemb, size_t size)
{
void *tmp;
tmp = calloc(nmemb, size);
if (!tmp)
{
psql_error("out of memory");
exit(EXIT_FAILURE);
}
return tmp;
}
/* /*
...@@ -854,12 +891,7 @@ expand_tilde(char **filename) ...@@ -854,12 +891,7 @@ expand_tilde(char **filename)
{ {
char *newfn; char *newfn;
newfn = malloc(strlen(home) + strlen(p) + 1); newfn = xmalloc(strlen(home) + strlen(p) + 1);
if (!newfn)
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(newfn, home); strcpy(newfn, home);
strcat(newfn, p); strcat(newfn, p);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.32 2004/01/09 21:12:20 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.33 2004/01/24 19:38:49 neilc Exp $
*/ */
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
...@@ -20,7 +20,15 @@ ...@@ -20,7 +20,15 @@
#define psql_assert(p) #define psql_assert(p)
#endif #endif
/*
* Safer versions of some standard C library functions. If an
* out-of-memory condition occurs, these functions will bail out
* safely; therefore, their return value is guaranteed to be non-NULL.
*/
extern char *xstrdup(const char *string); extern char *xstrdup(const char *string);
extern void *xmalloc(size_t size);
extern void *xmalloc_zero(size_t size);
extern void *xcalloc(size_t nmemb, size_t size);
extern bool setQFout(const char *fname); extern bool setQFout(const char *fname);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.37 2004/01/20 23:48:56 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.38 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "copy.h" #include "copy.h"
...@@ -83,12 +83,7 @@ xstrcat(char **var, const char *more) ...@@ -83,12 +83,7 @@ xstrcat(char **var, const char *more)
{ {
char *newvar; char *newvar;
newvar = (char *) malloc(strlen(*var) + strlen(more) + 1); newvar = xmalloc(strlen(*var) + strlen(more) + 1);
if (!newvar)
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(newvar, *var); strcpy(newvar, *var);
strcat(newvar, more); strcat(newvar, more);
free(*var); free(*var);
...@@ -112,11 +107,7 @@ parse_slash_copy(const char *args) ...@@ -112,11 +107,7 @@ parse_slash_copy(const char *args)
return NULL; return NULL;
} }
if (!(result = calloc(1, sizeof(struct copy_options)))) result = xcalloc(1, sizeof(struct copy_options));
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
token = strtokx(line, whitespace, ".,()", "\"", token = strtokx(line, whitespace, ".,()", "\"",
0, false, pset.encoding); 0, false, pset.encoding);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.92 2004/01/11 19:10:49 dennis Exp $ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.93 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "describe.h" #include "describe.h"
...@@ -39,21 +39,6 @@ static void processNamePattern(PQExpBuffer buf, const char *pattern, ...@@ -39,21 +39,6 @@ static void processNamePattern(PQExpBuffer buf, const char *pattern,
const char *schemavar, const char *namevar, const char *schemavar, const char *namevar,
const char *altnamevar, const char *visibilityrule); const char *altnamevar, const char *visibilityrule);
static void *
xmalloc(size_t size)
{
void *tmp;
tmp = malloc(size);
if (!tmp)
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
return tmp;
}
static void * static void *
xmalloczero(size_t size) xmalloczero(size_t size)
{ {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.32 2003/11/29 19:52:06 pgsql Exp $ * $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.33 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "input.h" #include "input.h"
...@@ -83,7 +83,7 @@ gets_basic(const char prompt[]) ...@@ -83,7 +83,7 @@ gets_basic(const char prompt[])
* gets_interactive() * gets_interactive()
* *
* Gets a line of interactive input, using readline of desired. * Gets a line of interactive input, using readline of desired.
* The result is malloced. * The result is malloc'ed.
*/ */
char * char *
gets_interactive(const char *prompt) gets_interactive(const char *prompt)
...@@ -113,7 +113,7 @@ gets_interactive(const char *prompt) ...@@ -113,7 +113,7 @@ gets_interactive(const char *prompt)
else else
{ {
free(prev_hist); free(prev_hist);
prev_hist = strdup(s); prev_hist = xstrdup(s);
add_history(s); add_history(s);
} }
} }
...@@ -183,17 +183,15 @@ initializeInput(int flags) ...@@ -183,17 +183,15 @@ initializeInput(int flags)
home = getenv("HOME"); home = getenv("HOME");
if (home) if (home)
{ {
char *psql_history = (char *) malloc(strlen(home) + 1 + char *psql_history;
strlen(PSQLHISTORY) + 1);
if (psql_history) psql_history = xmalloc(strlen(home) + 1 +
{ strlen(PSQLHISTORY) + 1);
sprintf(psql_history, "%s/%s", home, PSQLHISTORY); sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
read_history(psql_history); read_history(psql_history);
free(psql_history); free(psql_history);
} }
} }
}
#endif #endif
#ifdef HAVE_ATEXIT #ifdef HAVE_ATEXIT
...@@ -234,17 +232,16 @@ finishInput(int exitstatus, void *arg) ...@@ -234,17 +232,16 @@ finishInput(int exitstatus, void *arg)
if (useHistory) if (useHistory)
{ {
char *home; char *home;
char *psql_history;
home = getenv("HOME"); home = getenv("HOME");
if (home) if (home)
{ {
psql_history = (char *) malloc(strlen(home) + 1 + char *psql_history;
strlen(PSQLHISTORY) + 1);
if (psql_history)
{
int hist_size; int hist_size;
psql_history = xmalloc(strlen(home) + 1 +
strlen(PSQLHISTORY) + 1);
hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true); hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
if (hist_size >= 0) if (hist_size >= 0)
...@@ -255,6 +252,5 @@ finishInput(int exitstatus, void *arg) ...@@ -255,6 +252,5 @@ finishInput(int exitstatus, void *arg)
free(psql_history); free(psql_history);
} }
} }
}
#endif #endif
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.59 2004/01/21 22:05:44 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.60 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "mainloop.h" #include "mainloop.h"
...@@ -332,13 +332,7 @@ MainLoop(FILE *source) ...@@ -332,13 +332,7 @@ MainLoop(FILE *source)
/* It is a variable, perform substitution */ /* It is a variable, perform substitution */
out_length = strlen(value); out_length = strlen(value);
new = malloc(len + out_length - in_length + 1); new = xmalloc(len + out_length - in_length + 1);
if (!new)
{
psql_error("out of memory\n");
exit(EXIT_FAILURE);
}
sprintf(new, "%.*s%s%s", i, line, value, sprintf(new, "%.*s%s%s", i, line, value,
&line[i + thislen + in_length]); &line[i + thislen + in_length]);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.44 2003/11/29 19:52:07 pgsql Exp $ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.45 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -224,19 +224,8 @@ print_aligned_text(const char *title, const char *const * headers, ...@@ -224,19 +224,8 @@ print_aligned_text(const char *title, const char *const * headers,
if (col_count > 0) if (col_count > 0)
{ {
widths = calloc(col_count, sizeof(*widths)); widths = xcalloc(col_count, sizeof(*widths));
if (!widths) head_w = xcalloc(col_count, sizeof(*head_w));
{
perror("calloc");
exit(EXIT_FAILURE);
}
head_w = calloc(col_count, sizeof(*head_w));
if (!head_w)
{
perror("calloc");
exit(EXIT_FAILURE);
}
} }
else else
{ {
...@@ -250,12 +239,7 @@ print_aligned_text(const char *title, const char *const * headers, ...@@ -250,12 +239,7 @@ print_aligned_text(const char *title, const char *const * headers,
if (cell_count > 0) if (cell_count > 0)
{ {
cell_w = calloc(cell_count, sizeof(*cell_w)); cell_w = xcalloc(cell_count, sizeof(*cell_w));
if (!cell_w)
{
perror("calloc");
exit(EXIT_FAILURE);
}
} }
else else
cell_w = NULL; cell_w = NULL;
...@@ -427,12 +411,7 @@ print_aligned_vertical(const char *title, const char *const * headers, ...@@ -427,12 +411,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
col_count++; col_count++;
if (col_count > 0) if (col_count > 0)
{ {
head_w = calloc(col_count, sizeof(*head_w)); head_w = xcalloc(col_count, sizeof(*head_w));
if (!head_w)
{
perror("calloc");
exit(EXIT_FAILURE);
}
} }
else else
head_w = NULL; head_w = NULL;
...@@ -451,12 +430,7 @@ print_aligned_vertical(const char *title, const char *const * headers, ...@@ -451,12 +430,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
if (cell_count > 0) if (cell_count > 0)
{ {
cell_w = calloc(cell_count, sizeof(*cell_w)); cell_w = xcalloc(cell_count, sizeof(*cell_w));
if (!cell_w)
{
perror("calloc");
exit(EXIT_FAILURE);
}
} }
else else
cell_w = NULL; cell_w = NULL;
...@@ -475,12 +449,7 @@ print_aligned_vertical(const char *title, const char *const * headers, ...@@ -475,12 +449,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
fprintf(fout, "%s\n", title); fprintf(fout, "%s\n", title);
/* make horizontal border */ /* make horizontal border */
divider = malloc(hwidth + dwidth + 10); divider = xmalloc(hwidth + dwidth + 10);
if (!divider)
{
perror("malloc");
exit(EXIT_FAILURE);
}
divider[0] = '\0'; divider[0] = '\0';
if (opt_border == 2) if (opt_border == 2)
strcat(divider, "+-"); strcat(divider, "+-");
...@@ -502,15 +471,9 @@ print_aligned_vertical(const char *title, const char *const * headers, ...@@ -502,15 +471,9 @@ print_aligned_vertical(const char *title, const char *const * headers,
{ {
if (!opt_barebones) if (!opt_barebones)
{ {
char *record_str = malloc(32); char *record_str = xmalloc(32);
size_t record_str_len; size_t record_str_len;
if (!record_str)
{
perror("malloc");
exit(EXIT_FAILURE);
}
if (opt_border == 0) if (opt_border == 0)
snprintf(record_str, 32, "* Record %d", record++); snprintf(record_str, 32, "* Record %d", record++);
else else
...@@ -521,13 +484,7 @@ print_aligned_vertical(const char *title, const char *const * headers, ...@@ -521,13 +484,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
fprintf(fout, "%.*s%s\n", opt_border, divider, record_str); fprintf(fout, "%.*s%s\n", opt_border, divider, record_str);
else else
{ {
char *div_copy = strdup(divider); char *div_copy = xstrdup(divider);
if (!div_copy)
{
perror("malloc");
exit(EXIT_FAILURE);
}
strncpy(div_copy + opt_border, record_str, record_str_len); strncpy(div_copy + opt_border, record_str, record_str_len);
fprintf(fout, "%s\n", div_copy); fprintf(fout, "%s\n", div_copy);
...@@ -1141,24 +1098,14 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) ...@@ -1141,24 +1098,14 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
nfields = PQnfields(result); nfields = PQnfields(result);
headers = calloc(nfields + 1, sizeof(*headers)); headers = xcalloc(nfields + 1, sizeof(*headers));
if (!headers)
{
perror("calloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < nfields; i++) for (i = 0; i < nfields; i++)
headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding); headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);
/* set cells */ /* set cells */
cells = calloc(nfields * PQntuples(result) + 1, sizeof(*cells)); cells = xcalloc(nfields * PQntuples(result) + 1, sizeof(*cells));
if (!cells)
{
perror("calloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < nfields * PQntuples(result); i++) for (i = 0; i < nfields * PQntuples(result); i++)
{ {
...@@ -1174,14 +1121,9 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) ...@@ -1174,14 +1121,9 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
footers = opt->footers; footers = opt->footers;
else if (!opt->topt.expanded && opt->default_footer) else if (!opt->topt.expanded && opt->default_footer)
{ {
footers = calloc(2, sizeof(*footers)); footers = xcalloc(2, sizeof(*footers));
if (!footers)
{
perror("calloc");
exit(EXIT_FAILURE);
}
footers[0] = malloc(100); footers[0] = xmalloc(100);
if (PQntuples(result) == 1) if (PQntuples(result) == 1)
snprintf(footers[0], 100, gettext("(1 row)")); snprintf(footers[0], 100, gettext("(1 row)"));
else else
...@@ -1192,12 +1134,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) ...@@ -1192,12 +1134,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
/* set alignment */ /* set alignment */
align = calloc(nfields + 1, sizeof(*align)); align = xcalloc(nfields + 1, sizeof(*align));
if (!align)
{
perror("calloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < nfields; i++) for (i = 0; i < nfields; i++)
{ {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.32 2004/01/20 19:49:34 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.33 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "prompt.h" #include "prompt.h"
...@@ -248,7 +248,7 @@ get_prompt(promptStatus_t status) ...@@ -248,7 +248,7 @@ get_prompt(promptStatus_t status)
case '`': case '`':
{ {
FILE *fd = NULL; FILE *fd = NULL;
char *file = strdup(p + 1); char *file = xstrdup(p + 1);
int cmdend; int cmdend;
cmdend = strcspn(file, "`"); cmdend = strcspn(file, "`");
...@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status) ...@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
const char *val; const char *val;
int nameend; int nameend;
name = strdup(p + 1); name = xstrdup(p + 1);
nameend = strcspn(name, ":"); nameend = strcspn(name, ":");
name[nameend] = '\0'; name[nameend] = '\0';
val = GetVariable(pset.vars, name); val = GetVariable(pset.vars, name);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.81 2003/11/29 19:52:07 pgsql Exp $ * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.82 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -170,7 +170,7 @@ main(int argc, char *argv[]) ...@@ -170,7 +170,7 @@ main(int argc, char *argv[])
if (strcmp(options.username, "\001") == 0) if (strcmp(options.username, "\001") == 0)
username = simple_prompt("User name: ", 100, true); username = simple_prompt("User name: ", 100, true);
else else
username = strdup(options.username); username = xstrdup(options.username);
} }
if (pset.getPassword) if (pset.getPassword)
...@@ -567,15 +567,10 @@ process_psqlrc(void) ...@@ -567,15 +567,10 @@ process_psqlrc(void)
if (home) if (home)
{ {
psqlrc = malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 + psqlrc = xmalloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
strlen(PG_VERSION) + 1); strlen(PG_VERSION) + 1);
if (!psqlrc)
{
fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
exit(EXIT_FAILURE);
}
sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION); sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
if (access(psqlrc, R_OK) == 0) if (access(psqlrc, R_OK) == 0)
process_file(psqlrc); process_file(psqlrc);
else else
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/stringutils.c,v 1.36 2003/12/01 22:14:40 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/stringutils.c,v 1.37 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -77,9 +77,7 @@ strtokx(const char *s, ...@@ -77,9 +77,7 @@ strtokx(const char *s,
* tokens. 2X the space is a gross overestimate, but it's * tokens. 2X the space is a gross overestimate, but it's
* unlikely that this code will be used on huge strings anyway. * unlikely that this code will be used on huge strings anyway.
*/ */
storage = (char *) malloc(2 * strlen(s) + 1); storage = xmalloc(2 * strlen(s) + 1);
if (!storage)
return NULL; /* really "out of memory" */
strcpy(storage, s); strcpy(storage, s);
string = storage; string = storage;
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.98 2004/01/10 02:21:08 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.99 2004/01/24 19:38:49 neilc Exp $
*/ */
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
...@@ -1485,9 +1485,7 @@ _complete_from_query(int is_schema_query, const char *text, int state) ...@@ -1485,9 +1485,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
/* Set up suitably-escaped copies of textual inputs */ /* Set up suitably-escaped copies of textual inputs */
if (text) if (text)
{ {
e_text = (char *) malloc(strlen(text) * 2 + 1); e_text = xmalloc(strlen(text) * 2 + 1);
if (!e_text)
return NULL;
PQescapeString(e_text, text, strlen(text)); PQescapeString(e_text, text, strlen(text));
} }
else else
...@@ -1495,16 +1493,12 @@ _complete_from_query(int is_schema_query, const char *text, int state) ...@@ -1495,16 +1493,12 @@ _complete_from_query(int is_schema_query, const char *text, int state)
if (completion_info_charp) if (completion_info_charp)
{ {
e_info_charp = (char *) size_t charp_len;
malloc(strlen(completion_info_charp) * 2 + 1);
if (!e_info_charp) charp_len = strlen(completion_info_charp);
{ e_info_charp = xmalloc(charp_len * 2 + 1);
if (e_text)
free(e_text);
return NULL;
}
PQescapeString(e_info_charp, completion_info_charp, PQescapeString(e_info_charp, completion_info_charp,
strlen(completion_info_charp)); charp_len);
} }
else else
e_info_charp = NULL; e_info_charp = NULL;
...@@ -1794,15 +1788,7 @@ previous_word(int point, int skip) ...@@ -1794,15 +1788,7 @@ previous_word(int point, int skip)
} }
/* make a copy */ /* make a copy */
s = (char *) malloc(end - start + 2); s = xmalloc(end - start + 2);
if (!s)
{
psql_error("out of memory\n");
if (!pset.cur_cmd_interactive)
exit(EXIT_FAILURE);
else
return NULL;
}
strncpy(s, &rl_line_buffer[start], end - start + 1); strncpy(s, &rl_line_buffer[start], end - start + 1);
s[end - start + 1] = '\0'; s[end - start + 1] = '\0';
...@@ -1828,7 +1814,7 @@ quote_file_name(char *text, int match_type, char *quote_pointer) ...@@ -1828,7 +1814,7 @@ quote_file_name(char *text, int match_type, char *quote_pointer)
(void) quote_pointer; /* not used */ (void) quote_pointer; /* not used */
length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2); length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2);
s = malloc(length); s = xmalloc(length);
s[0] = '\''; s[0] = '\'';
strcpy(s + 1, text); strcpy(s + 1, text);
if (match_type == SINGLE_MATCH) if (match_type == SINGLE_MATCH)
...@@ -1849,7 +1835,7 @@ dequote_file_name(char *text, char quote_char) ...@@ -1849,7 +1835,7 @@ dequote_file_name(char *text, char quote_char)
return xstrdup(text); return xstrdup(text);
length = strlen(text); length = strlen(text);
s = malloc(length - 2 + 1); s = xmalloc(length - 2 + 1);
strncpy(s, text +1, length - 2); strncpy(s, text +1, length - 2);
s[length] = '\0'; s[length] = '\0';
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.15 2003/12/01 22:14:40 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.16 2004/01/24 19:38:49 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -14,19 +14,9 @@ CreateVariableSpace(void) ...@@ -14,19 +14,9 @@ CreateVariableSpace(void)
{ {
struct _variable *ptr; struct _variable *ptr;
ptr = calloc(1, sizeof *ptr); ptr = xcalloc(1, sizeof *ptr);
if (!ptr) ptr->name = xstrdup("@");
return NULL; ptr->value = xstrdup("");
ptr->name = strdup("@");
ptr->value = strdup("");
if (!ptr->name || !ptr->value)
{
free(ptr->name);
free(ptr->value);
free(ptr);
return NULL;
}
return ptr; return ptr;
} }
...@@ -162,19 +152,15 @@ SetVariable(VariableSpace space, const char *name, const char *value) ...@@ -162,19 +152,15 @@ SetVariable(VariableSpace space, const char *name, const char *value)
if (strcmp(current->name, name) == 0) if (strcmp(current->name, name) == 0)
{ {
free(current->value); free(current->value);
current->value = strdup(value); current->value = xstrdup(value);
return current->value ? true : false; return true;
} }
} }
previous->next = calloc(1, sizeof *(previous->next)); previous->next = xcalloc(1, sizeof *(previous->next));
if (!previous->next) previous->next->name = xstrdup(name);
return false; previous->next->value = xstrdup(value);
previous->next->name = strdup(name); return true;
if (!previous->next->name)
return false;
previous->next->value = strdup(value);
return previous->next->value ? true : false;
} }
bool bool
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment