OSDN Git Service

This patch makes some of the memory manipulation performed by psql a
authorNeil Conway <neilc@samurai.com>
Sat, 24 Jan 2004 19:38:49 +0000 (19:38 +0000)
committerNeil Conway <neilc@samurai.com>
Sat, 24 Jan 2004 19:38:49 +0000 (19:38 +0000)
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().

13 files changed:
src/bin/psql/command.c
src/bin/psql/common.c
src/bin/psql/common.h
src/bin/psql/copy.c
src/bin/psql/describe.c
src/bin/psql/input.c
src/bin/psql/mainloop.c
src/bin/psql/print.c
src/bin/psql/prompt.c
src/bin/psql/startup.c
src/bin/psql/stringutils.c
src/bin/psql/tab-complete.c
src/bin/psql/variables.c

index e129478..2f31d3e 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -1156,13 +1156,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon)
                                /* 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';
 
@@ -1235,7 +1229,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon)
  *
  * 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)
@@ -1251,12 +1245,7 @@ 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))
        {
@@ -1537,9 +1526,7 @@ editFile(const char *fname)
        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 "
@@ -1959,15 +1946,7 @@ do_shell(const char *command)
                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 "
index ed3649d..d27dc10 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -89,6 +89,43 @@ xstrdup(const char *string)
        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)
                {
                        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);
 
index db8037b..05e2996 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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);
 
index e852dd5..534efe0 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -83,12 +83,7 @@ xstrcat(char **var, const char *more)
 {
        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);
@@ -112,11 +107,7 @@ parse_slash_copy(const char *args)
                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);
index 7e168ef..83aa3f1 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -39,21 +39,6 @@ static void processNamePattern(PQExpBuffer buf, const char *pattern,
                                   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)
 {
index 90c2c92..6a2afa3 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -83,7 +83,7 @@ gets_basic(const char prompt[])
  * 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)
@@ -113,7 +113,7 @@ gets_interactive(const char *prompt)
                else
                {
                        free(prev_hist);
-                       prev_hist = strdup(s);
+                       prev_hist = xstrdup(s);
                        add_history(s);
                }
        }
@@ -183,15 +183,13 @@ initializeInput(int flags)
                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
@@ -234,26 +232,24 @@ finishInput(int exitstatus, void *arg)
        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
index 9a64483..d59261f 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -332,13 +332,7 @@ MainLoop(FILE *source)
                                        /* 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]);
 
index 890b2ef..6f8b3ed 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -224,19 +224,8 @@ print_aligned_text(const char *title, const char *const * headers,
 
        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
        {
@@ -250,12 +239,7 @@ print_aligned_text(const char *title, const char *const * headers,
 
        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;
@@ -427,12 +411,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
                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;
@@ -451,12 +430,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
 
        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;
@@ -475,12 +449,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
                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, "+-");
@@ -502,15 +471,9 @@ print_aligned_vertical(const char *title, const char *const * headers,
                {
                        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
@@ -521,13 +484,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
                                        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);
@@ -1141,24 +1098,14 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
 
        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++)
        {
@@ -1174,14 +1121,9 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
                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
@@ -1192,12 +1134,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
 
        /* 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++)
        {
index 60e82cc..c26168b 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -248,7 +248,7 @@ get_prompt(promptStatus_t status)
                                case '`':
                                        {
                                                FILE       *fd = NULL;
-                                               char       *file = strdup(p + 1);
+                                               char       *file = xstrdup(p + 1);
                                                int                     cmdend;
 
                                                cmdend = strcspn(file, "`");
@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
                                                const char *val;
                                                int                     nameend;
 
-                                               name = strdup(p + 1);
+                                               name = xstrdup(p + 1);
                                                nameend = strcspn(name, ":");
                                                name[nameend] = '\0';
                                                val = GetVariable(pset.vars, name);
index a0640b1..b21beeb 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
 
@@ -170,7 +170,7 @@ main(int argc, char *argv[])
                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)
@@ -567,15 +567,10 @@ process_psqlrc(void)
 
        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
index c90abb2..eda7837 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
 
@@ -77,9 +77,7 @@ strtokx(const char *s,
                 * 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;
        }
index 58ef84f..40650d3 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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)
                /* 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
@@ -1495,16 +1493,12 @@ _complete_from_query(int is_schema_query, const char *text, int state)
 
                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;
@@ -1794,15 +1788,7 @@ previous_word(int point, int skip)
        }
 
        /* 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';
@@ -1828,7 +1814,7 @@ quote_file_name(char *text, int match_type, char *quote_pointer)
        (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)
@@ -1849,7 +1835,7 @@ dequote_file_name(char *text, char quote_char)
                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';
 
index 21dc8ae..31c5510 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"
@@ -14,19 +14,9 @@ CreateVariableSpace(void)
 {
        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;
 }
@@ -162,19 +152,15 @@ SetVariable(VariableSpace space, const char *name, const char *value)
                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