*
* 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 "command.h"
/* Copy the option */
token_len = cp - &options_string[pos];
- return_val = malloc(token_len + 1);
- if (!return_val)
- {
- psql_error("out of memory\n");
- exit(EXIT_FAILURE);
- }
-
+ return_val = xmalloc(token_len + 1);
memcpy(return_val, &options_string[pos], token_len);
return_val[token_len] = '\0';
*
* Replaces \n, \t, and the like.
*
- * The return value is malloc()'ed.
+ * The return value is malloc'ed.
*/
static char *
unescape(const unsigned char *source, size_t len)
length = Min(len, strlen(source)) + 1;
- tmp = destination = malloc(length);
- if (!tmp)
- {
- psql_error("out of memory\n");
- exit(EXIT_FAILURE);
- }
+ tmp = destination = xmalloc(length);
for (p = source; p - source < (int) len && *p; p += PQmblen(p, pset.encoding))
{
if (!editorName)
editorName = DEFAULT_EDITOR;
- sys = malloc(strlen(editorName) + strlen(fname) + 10 + 1);
- if (!sys)
- return false;
+ sys = xmalloc(strlen(editorName) + strlen(fname) + 10 + 1);
sprintf(sys,
#ifndef WIN32
"exec "
if (shellName == NULL)
shellName = DEFAULT_SHELL;
- sys = malloc(strlen(shellName) + 16);
- if (!sys)
- {
- psql_error("out of memory\n");
- if (pset.cur_cmd_interactive)
- return false;
- else
- exit(EXIT_FAILURE);
- }
+ sys = xmalloc(strlen(shellName) + 16);
sprintf(sys,
#ifndef WIN32
"exec "
*
* 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 "common.h"
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;
+}
/*
{
char *newfn;
- newfn = malloc(strlen(home) + strlen(p) + 1);
- if (!newfn)
- {
- psql_error("out of memory\n");
- exit(EXIT_FAILURE);
- }
+ newfn = xmalloc(strlen(home) + strlen(p) + 1);
strcpy(newfn, home);
strcat(newfn, p);
*
* 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
#define COMMON_H
#define psql_assert(p)
#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 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);
*
* 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 "copy.h"
{
char *newvar;
- newvar = (char *) malloc(strlen(*var) + strlen(more) + 1);
- if (!newvar)
- {
- psql_error("out of memory\n");
- exit(EXIT_FAILURE);
- }
+ newvar = xmalloc(strlen(*var) + strlen(more) + 1);
strcpy(newvar, *var);
strcat(newvar, more);
free(*var);
return NULL;
}
- if (!(result = calloc(1, sizeof(struct copy_options))))
- {
- psql_error("out of memory\n");
- exit(EXIT_FAILURE);
- }
+ result = xcalloc(1, sizeof(struct copy_options));
token = strtokx(line, whitespace, ".,()", "\"",
0, false, pset.encoding);
*
* 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 "describe.h"
const char *schemavar, const char *namevar,
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 *
xmalloczero(size_t size)
{
*
* 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 "input.h"
* gets_interactive()
*
* Gets a line of interactive input, using readline of desired.
- * The result is malloced.
+ * The result is malloc'ed.
*/
char *
gets_interactive(const char *prompt)
else
{
free(prev_hist);
- prev_hist = strdup(s);
+ prev_hist = xstrdup(s);
add_history(s);
}
}
home = getenv("HOME");
if (home)
{
- char *psql_history = (char *) malloc(strlen(home) + 1 +
- strlen(PSQLHISTORY) + 1);
-
- if (psql_history)
- {
- sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
- read_history(psql_history);
- free(psql_history);
- }
+ char *psql_history;
+
+ psql_history = xmalloc(strlen(home) + 1 +
+ strlen(PSQLHISTORY) + 1);
+ sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
+ read_history(psql_history);
+ free(psql_history);
}
}
#endif
if (useHistory)
{
char *home;
- char *psql_history;
home = getenv("HOME");
if (home)
{
- psql_history = (char *) malloc(strlen(home) + 1 +
- strlen(PSQLHISTORY) + 1);
- if (psql_history)
- {
- int hist_size;
+ char *psql_history;
+ 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)
- stifle_history(hist_size);
+ if (hist_size >= 0)
+ stifle_history(hist_size);
- sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
- write_history(psql_history);
- free(psql_history);
- }
+ sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
+ write_history(psql_history);
+ free(psql_history);
}
}
#endif
*
* 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 "mainloop.h"
/* It is a variable, perform substitution */
out_length = strlen(value);
- new = malloc(len + out_length - in_length + 1);
- if (!new)
- {
- psql_error("out of memory\n");
- exit(EXIT_FAILURE);
- }
-
+ new = xmalloc(len + out_length - in_length + 1);
sprintf(new, "%.*s%s%s", i, line, value,
&line[i + thislen + in_length]);
*
* 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 "common.h"
if (col_count > 0)
{
- widths = calloc(col_count, sizeof(*widths));
- if (!widths)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
-
- head_w = calloc(col_count, sizeof(*head_w));
- if (!head_w)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ widths = xcalloc(col_count, sizeof(*widths));
+ head_w = xcalloc(col_count, sizeof(*head_w));
}
else
{
if (cell_count > 0)
{
- cell_w = calloc(cell_count, sizeof(*cell_w));
- if (!cell_w)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ cell_w = xcalloc(cell_count, sizeof(*cell_w));
}
else
cell_w = NULL;
col_count++;
if (col_count > 0)
{
- head_w = calloc(col_count, sizeof(*head_w));
- if (!head_w)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ head_w = xcalloc(col_count, sizeof(*head_w));
}
else
head_w = NULL;
if (cell_count > 0)
{
- cell_w = calloc(cell_count, sizeof(*cell_w));
- if (!cell_w)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ cell_w = xcalloc(cell_count, sizeof(*cell_w));
}
else
cell_w = NULL;
fprintf(fout, "%s\n", title);
/* make horizontal border */
- divider = malloc(hwidth + dwidth + 10);
- if (!divider)
- {
- perror("malloc");
- exit(EXIT_FAILURE);
- }
+ divider = xmalloc(hwidth + dwidth + 10);
divider[0] = '\0';
if (opt_border == 2)
strcat(divider, "+-");
{
if (!opt_barebones)
{
- char *record_str = malloc(32);
+ char *record_str = xmalloc(32);
size_t record_str_len;
- if (!record_str)
- {
- perror("malloc");
- exit(EXIT_FAILURE);
- }
-
if (opt_border == 0)
snprintf(record_str, 32, "* Record %d", record++);
else
fprintf(fout, "%.*s%s\n", opt_border, divider, record_str);
else
{
- char *div_copy = strdup(divider);
-
- if (!div_copy)
- {
- perror("malloc");
- exit(EXIT_FAILURE);
- }
+ char *div_copy = xstrdup(divider);
strncpy(div_copy + opt_border, record_str, record_str_len);
fprintf(fout, "%s\n", div_copy);
nfields = PQnfields(result);
- headers = calloc(nfields + 1, sizeof(*headers));
- if (!headers)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ headers = xcalloc(nfields + 1, sizeof(*headers));
for (i = 0; i < nfields; i++)
headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);
/* set cells */
- cells = calloc(nfields * PQntuples(result) + 1, sizeof(*cells));
- if (!cells)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ cells = xcalloc(nfields * PQntuples(result) + 1, sizeof(*cells));
for (i = 0; i < nfields * PQntuples(result); i++)
{
footers = opt->footers;
else if (!opt->topt.expanded && opt->default_footer)
{
- footers = calloc(2, sizeof(*footers));
- if (!footers)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ footers = xcalloc(2, sizeof(*footers));
- footers[0] = malloc(100);
+ footers[0] = xmalloc(100);
if (PQntuples(result) == 1)
snprintf(footers[0], 100, gettext("(1 row)"));
else
/* set alignment */
- align = calloc(nfields + 1, sizeof(*align));
- if (!align)
- {
- perror("calloc");
- exit(EXIT_FAILURE);
- }
+ align = xcalloc(nfields + 1, sizeof(*align));
for (i = 0; i < nfields; i++)
{
*
* 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 "prompt.h"
case '`':
{
FILE *fd = NULL;
- char *file = strdup(p + 1);
+ char *file = xstrdup(p + 1);
int cmdend;
cmdend = strcspn(file, "`");
const char *val;
int nameend;
- name = strdup(p + 1);
+ name = xstrdup(p + 1);
nameend = strcspn(name, ":");
name[nameend] = '\0';
val = GetVariable(pset.vars, name);
*
* 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"
if (strcmp(options.username, "\001") == 0)
username = simple_prompt("User name: ", 100, true);
else
- username = strdup(options.username);
+ username = xstrdup(options.username);
}
if (pset.getPassword)
if (home)
{
- psqlrc = malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
- strlen(PG_VERSION) + 1);
- if (!psqlrc)
- {
- fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
- exit(EXIT_FAILURE);
- }
-
+ psqlrc = xmalloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
+ strlen(PG_VERSION) + 1);
sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
+
if (access(psqlrc, R_OK) == 0)
process_file(psqlrc);
else
*
* 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"
* tokens. 2X the space is a gross overestimate, but it's
* unlikely that this code will be used on huge strings anyway.
*/
- storage = (char *) malloc(2 * strlen(s) + 1);
- if (!storage)
- return NULL; /* really "out of memory" */
+ storage = xmalloc(2 * strlen(s) + 1);
strcpy(storage, s);
string = storage;
}
*
* 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 $
*/
/*----------------------------------------------------------------------
/* Set up suitably-escaped copies of textual inputs */
if (text)
{
- e_text = (char *) malloc(strlen(text) * 2 + 1);
- if (!e_text)
- return NULL;
+ e_text = xmalloc(strlen(text) * 2 + 1);
PQescapeString(e_text, text, strlen(text));
}
else
if (completion_info_charp)
{
- e_info_charp = (char *)
- malloc(strlen(completion_info_charp) * 2 + 1);
- if (!e_info_charp)
- {
- if (e_text)
- free(e_text);
- return NULL;
- }
+ size_t charp_len;
+
+ charp_len = strlen(completion_info_charp);
+ e_info_charp = xmalloc(charp_len * 2 + 1);
PQescapeString(e_info_charp, completion_info_charp,
- strlen(completion_info_charp));
+ charp_len);
}
else
e_info_charp = NULL;
}
/* make a copy */
- s = (char *) malloc(end - start + 2);
- if (!s)
- {
- psql_error("out of memory\n");
- if (!pset.cur_cmd_interactive)
- exit(EXIT_FAILURE);
- else
- return NULL;
- }
+ s = xmalloc(end - start + 2);
strncpy(s, &rl_line_buffer[start], end - start + 1);
s[end - start + 1] = '\0';
(void) quote_pointer; /* not used */
length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2);
- s = malloc(length);
+ s = xmalloc(length);
s[0] = '\'';
strcpy(s + 1, text);
if (match_type == SINGLE_MATCH)
return xstrdup(text);
length = strlen(text);
- s = malloc(length - 2 + 1);
+ s = xmalloc(length - 2 + 1);
strncpy(s, text +1, length - 2);
s[length] = '\0';
*
* 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 "common.h"
{
struct _variable *ptr;
- ptr = calloc(1, sizeof *ptr);
- if (!ptr)
- return NULL;
-
- ptr->name = strdup("@");
- ptr->value = strdup("");
- if (!ptr->name || !ptr->value)
- {
- free(ptr->name);
- free(ptr->value);
- free(ptr);
- return NULL;
- }
+ ptr = xcalloc(1, sizeof *ptr);
+ ptr->name = xstrdup("@");
+ ptr->value = xstrdup("");
return ptr;
}
if (strcmp(current->name, name) == 0)
{
free(current->value);
- current->value = strdup(value);
- return current->value ? true : false;
+ current->value = xstrdup(value);
+ return true;
}
}
- previous->next = calloc(1, sizeof *(previous->next));
- if (!previous->next)
- return false;
- previous->next->name = strdup(name);
- if (!previous->next->name)
- return false;
- previous->next->value = strdup(value);
- return previous->next->value ? true : false;
+ previous->next = xcalloc(1, sizeof *(previous->next));
+ previous->next->name = xstrdup(name);
+ previous->next->value = xstrdup(value);
+ return true;
}
bool