Index: src/bin/psql/command.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/command.c,v
retrieving revision 1.109
diff -c -r1.109 command.c
*** src/bin/psql/command.c	9 Jan 2004 21:12:20 -0000	1.109
--- src/bin/psql/command.c	23 Jan 2004 19:43:45 -0000
***************
*** 1156,1168 ****
  				/* 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);
! 				}
! 
  				memcpy(return_val, &options_string[pos], token_len);
  				return_val[token_len] = '\0';
  
--- 1156,1162 ----
  				/* Copy the option */
  				token_len = cp - &options_string[pos];
  
! 				return_val = xmalloc(token_len + 1);
  				memcpy(return_val, &options_string[pos], token_len);
  				return_val[token_len] = '\0';
  
***************
*** 1235,1241 ****
   *
   * Replaces \n, \t, and the like.
   *
!  * The return value is malloc()'ed.
   */
  static char *
  unescape(const unsigned char *source, size_t len)
--- 1229,1235 ----
   *
   * Replaces \n, \t, and the like.
   *
!  * The return value is malloc'ed.
   */
  static char *
  unescape(const unsigned char *source, size_t len)
***************
*** 1251,1262 ****
  
  	length = Min(len, strlen(source)) + 1;
  
! 	tmp = destination = malloc(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))
  	{
--- 1245,1251 ----
  
  	length = Min(len, strlen(source)) + 1;
  
! 	tmp = destination = xmalloc(length);
  
  	for (p = source; p - source < (int) len && *p; p += PQmblen(p, pset.encoding))
  	{
***************
*** 1537,1545 ****
  	if (!editorName)
  		editorName = DEFAULT_EDITOR;
  
! 	sys = malloc(strlen(editorName) + strlen(fname) + 10 + 1);
! 	if (!sys)
! 		return false;
  	sprintf(sys,
  #ifndef WIN32
  			"exec "
--- 1526,1532 ----
  	if (!editorName)
  		editorName = DEFAULT_EDITOR;
  
! 	sys = xmalloc(strlen(editorName) + strlen(fname) + 10 + 1);
  	sprintf(sys,
  #ifndef WIN32
  			"exec "
***************
*** 1959,1973 ****
  		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);
! 		}
  		sprintf(sys,
  #ifndef WIN32
  				"exec "
--- 1946,1952 ----
  		if (shellName == NULL)
  			shellName = DEFAULT_SHELL;
  
! 		sys = xmalloc(strlen(shellName) + 16);
  		sprintf(sys,
  #ifndef WIN32
  				"exec "
Index: src/bin/psql/common.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/common.c,v
retrieving revision 1.80
diff -c -r1.80 common.c
*** src/bin/psql/common.c	20 Jan 2004 23:48:56 -0000	1.80
--- src/bin/psql/common.c	23 Jan 2004 19:11:28 -0000
***************
*** 89,94 ****
--- 89,131 ----
  	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,865 ****
  		{
  			char	   *newfn;
  
! 			newfn = malloc(strlen(home) + strlen(p) + 1);
! 			if (!newfn)
! 			{
! 				psql_error("out of memory\n");
! 				exit(EXIT_FAILURE);
! 			}
  			strcpy(newfn, home);
  			strcat(newfn, p);
  
--- 891,897 ----
  		{
  			char	   *newfn;
  
! 			newfn = xmalloc(strlen(home) + strlen(p) + 1);
  			strcpy(newfn, home);
  			strcat(newfn, p);
  
Index: src/bin/psql/common.h
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/common.h,v
retrieving revision 1.32
diff -c -r1.32 common.h
*** src/bin/psql/common.h	9 Jan 2004 21:12:20 -0000	1.32
--- src/bin/psql/common.h	23 Jan 2004 19:10:02 -0000
***************
*** 20,26 ****
--- 20,34 ----
  #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: src/bin/psql/copy.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/copy.c,v
retrieving revision 1.37
diff -c -r1.37 copy.c
*** src/bin/psql/copy.c	20 Jan 2004 23:48:56 -0000	1.37
--- src/bin/psql/copy.c	23 Jan 2004 19:12:27 -0000
***************
*** 83,94 ****
  {
  	char	   *newvar;
  
! 	newvar = (char *) malloc(strlen(*var) + strlen(more) + 1);
! 	if (!newvar)
! 	{
! 		psql_error("out of memory\n");
! 		exit(EXIT_FAILURE);
! 	}
  	strcpy(newvar, *var);
  	strcat(newvar, more);
  	free(*var);
--- 83,89 ----
  {
  	char	   *newvar;
  
! 	newvar = xmalloc(strlen(*var) + strlen(more) + 1);
  	strcpy(newvar, *var);
  	strcat(newvar, more);
  	free(*var);
***************
*** 112,122 ****
  		return NULL;
  	}
  
! 	if (!(result = calloc(1, sizeof(struct copy_options))))
! 	{
! 		psql_error("out of memory\n");
! 		exit(EXIT_FAILURE);
! 	}
  
  	token = strtokx(line, whitespace, ".,()", "\"",
  					0, false, pset.encoding);
--- 107,113 ----
  		return NULL;
  	}
  
! 	result = xcalloc(1, sizeof(struct copy_options));
  
  	token = strtokx(line, whitespace, ".,()", "\"",
  					0, false, pset.encoding);
Index: src/bin/psql/describe.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/describe.c,v
retrieving revision 1.92
diff -c -r1.92 describe.c
*** src/bin/psql/describe.c	11 Jan 2004 19:10:49 -0000	1.92
--- src/bin/psql/describe.c	23 Jan 2004 19:06:33 -0000
***************
*** 39,59 ****
  				   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)
  {
--- 39,44 ----
Index: src/bin/psql/input.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/input.c,v
retrieving revision 1.32
diff -c -r1.32 input.c
*** src/bin/psql/input.c	29 Nov 2003 19:52:06 -0000	1.32
--- src/bin/psql/input.c	23 Jan 2004 19:03:31 -0000
***************
*** 83,89 ****
   * gets_interactive()
   *
   * Gets a line of interactive input, using readline of desired.
!  * The result is malloced.
   */
  char *
  gets_interactive(const char *prompt)
--- 83,89 ----
   * gets_interactive()
   *
   * Gets a line of interactive input, using readline of desired.
!  * The result is malloc'ed.
   */
  char *
  gets_interactive(const char *prompt)
***************
*** 113,119 ****
  		else
  		{
  			free(prev_hist);
! 			prev_hist = strdup(s);
  			add_history(s);
  		}
  	}
--- 113,119 ----
  		else
  		{
  			free(prev_hist);
! 			prev_hist = xstrdup(s);
  			add_history(s);
  		}
  	}
***************
*** 183,197 ****
  		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);
! 			}
  		}
  	}
  #endif
--- 183,195 ----
  		home = getenv("HOME");
  		if (home)
  		{
! 			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,259 ****
  	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;
! 
! 				hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
! 
! 				if (hist_size >= 0)
! 					stifle_history(hist_size);
! 
! 				sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
! 				write_history(psql_history);
! 				free(psql_history);
! 			}
  		}
  	}
  #endif
--- 232,255 ----
  	if (useHistory)
  	{
  		char	   *home;
  
  		home = getenv("HOME");
  		if (home)
  		{
! 			char	*psql_history;
! 			int		 hist_size;
! 
! 			psql_history = xmalloc(strlen(home) + 1 +
! 								   strlen(PSQLHISTORY) + 1);
! 
! 			hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
! 
! 			if (hist_size >= 0)
! 				stifle_history(hist_size);
! 
! 			sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
! 			write_history(psql_history);
! 			free(psql_history);
  		}
  	}
  #endif
Index: src/bin/psql/mainloop.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/mainloop.c,v
retrieving revision 1.59
diff -c -r1.59 mainloop.c
*** src/bin/psql/mainloop.c	21 Jan 2004 22:05:44 -0000	1.59
--- src/bin/psql/mainloop.c	23 Jan 2004 18:59:47 -0000
***************
*** 332,344 ****
  					/* 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);
! 					}
! 
  					sprintf(new, "%.*s%s%s", i, line, value,
  							&line[i + thislen + in_length]);
  
--- 332,338 ----
  					/* It is a variable, perform substitution */
  					out_length = strlen(value);
  
! 					new = xmalloc(len + out_length - in_length + 1);
  					sprintf(new, "%.*s%s%s", i, line, value,
  							&line[i + thislen + in_length]);
  
Index: src/bin/psql/print.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/print.c,v
retrieving revision 1.44
diff -c -r1.44 print.c
*** src/bin/psql/print.c	29 Nov 2003 19:52:07 -0000	1.44
--- src/bin/psql/print.c	23 Jan 2004 19:13:53 -0000
***************
*** 224,242 ****
  
  	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);
! 		}
  	}
  	else
  	{
--- 224,231 ----
  
  	if (col_count > 0)
  	{
! 		widths = xcalloc(col_count, sizeof(*widths));
! 		head_w = xcalloc(col_count, sizeof(*head_w));
  	}
  	else
  	{
***************
*** 250,261 ****
  
  	if (cell_count > 0)
  	{
! 		cell_w = calloc(cell_count, sizeof(*cell_w));
! 		if (!cell_w)
! 		{
! 			perror("calloc");
! 			exit(EXIT_FAILURE);
! 		}
  	}
  	else
  		cell_w = NULL;
--- 239,245 ----
  
  	if (cell_count > 0)
  	{
! 		cell_w = xcalloc(cell_count, sizeof(*cell_w));
  	}
  	else
  		cell_w = NULL;
***************
*** 427,438 ****
  		col_count++;
  	if (col_count > 0)
  	{
! 		head_w = calloc(col_count, sizeof(*head_w));
! 		if (!head_w)
! 		{
! 			perror("calloc");
! 			exit(EXIT_FAILURE);
! 		}
  	}
  	else
  		head_w = NULL;
--- 411,417 ----
  		col_count++;
  	if (col_count > 0)
  	{
! 		head_w = xcalloc(col_count, sizeof(*head_w));
  	}
  	else
  		head_w = NULL;
***************
*** 451,462 ****
  
  	if (cell_count > 0)
  	{
! 		cell_w = calloc(cell_count, sizeof(*cell_w));
! 		if (!cell_w)
! 		{
! 			perror("calloc");
! 			exit(EXIT_FAILURE);
! 		}
  	}
  	else
  		cell_w = NULL;
--- 430,436 ----
  
  	if (cell_count > 0)
  	{
! 		cell_w = xcalloc(cell_count, sizeof(*cell_w));
  	}
  	else
  		cell_w = NULL;
***************
*** 475,486 ****
  		fprintf(fout, "%s\n", title);
  
  	/* make horizontal border */
! 	divider = malloc(hwidth + dwidth + 10);
! 	if (!divider)
! 	{
! 		perror("malloc");
! 		exit(EXIT_FAILURE);
! 	}
  	divider[0] = '\0';
  	if (opt_border == 2)
  		strcat(divider, "+-");
--- 449,455 ----
  		fprintf(fout, "%s\n", title);
  
  	/* make horizontal border */
! 	divider = xmalloc(hwidth + dwidth + 10);
  	divider[0] = '\0';
  	if (opt_border == 2)
  		strcat(divider, "+-");
***************
*** 502,516 ****
  		{
  			if (!opt_barebones)
  			{
! 				char	   *record_str = malloc(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
--- 471,479 ----
  		{
  			if (!opt_barebones)
  			{
! 				char	   *record_str = xmalloc(32);
  				size_t		record_str_len;
  
  				if (opt_border == 0)
  					snprintf(record_str, 32, "* Record %d", record++);
  				else
***************
*** 521,533 ****
  					fprintf(fout, "%.*s%s\n", opt_border, divider, record_str);
  				else
  				{
! 					char	   *div_copy = strdup(divider);
! 
! 					if (!div_copy)
! 					{
! 						perror("malloc");
! 						exit(EXIT_FAILURE);
! 					}
  
  					strncpy(div_copy + opt_border, record_str, record_str_len);
  					fprintf(fout, "%s\n", div_copy);
--- 484,490 ----
  					fprintf(fout, "%.*s%s\n", opt_border, divider, record_str);
  				else
  				{
! 					char	   *div_copy = xstrdup(divider);
  
  					strncpy(div_copy + opt_border, record_str, record_str_len);
  					fprintf(fout, "%s\n", div_copy);
***************
*** 1141,1164 ****
  
  	nfields = PQnfields(result);
  
! 	headers = calloc(nfields + 1, sizeof(*headers));
! 	if (!headers)
! 	{
! 		perror("calloc");
! 		exit(EXIT_FAILURE);
! 	}
  
  	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);
! 	}
  
  	for (i = 0; i < nfields * PQntuples(result); i++)
  	{
--- 1098,1111 ----
  
  	nfields = PQnfields(result);
  
! 	headers = xcalloc(nfields + 1, sizeof(*headers));
  
  	for (i = 0; i < nfields; i++)
  		headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);
  
  	/* set cells */
  
! 	cells = xcalloc(nfields * PQntuples(result) + 1, sizeof(*cells));
  
  	for (i = 0; i < nfields * PQntuples(result); i++)
  	{
***************
*** 1174,1187 ****
  		footers = opt->footers;
  	else if (!opt->topt.expanded && opt->default_footer)
  	{
! 		footers = calloc(2, sizeof(*footers));
! 		if (!footers)
! 		{
! 			perror("calloc");
! 			exit(EXIT_FAILURE);
! 		}
  
! 		footers[0] = malloc(100);
  		if (PQntuples(result) == 1)
  			snprintf(footers[0], 100, gettext("(1 row)"));
  		else
--- 1121,1129 ----
  		footers = opt->footers;
  	else if (!opt->topt.expanded && opt->default_footer)
  	{
! 		footers = xcalloc(2, sizeof(*footers));
  
! 		footers[0] = xmalloc(100);
  		if (PQntuples(result) == 1)
  			snprintf(footers[0], 100, gettext("(1 row)"));
  		else
***************
*** 1192,1203 ****
  
  	/* set alignment */
  
! 	align = calloc(nfields + 1, sizeof(*align));
! 	if (!align)
! 	{
! 		perror("calloc");
! 		exit(EXIT_FAILURE);
! 	}
  
  	for (i = 0; i < nfields; i++)
  	{
--- 1134,1140 ----
  
  	/* set alignment */
  
! 	align = xcalloc(nfields + 1, sizeof(*align));
  
  	for (i = 0; i < nfields; i++)
  	{
Index: src/bin/psql/prompt.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/prompt.c,v
retrieving revision 1.32
diff -c -r1.32 prompt.c
*** src/bin/psql/prompt.c	20 Jan 2004 19:49:34 -0000	1.32
--- src/bin/psql/prompt.c	23 Jan 2004 19:03:57 -0000
***************
*** 248,254 ****
  				case '`':
  					{
  						FILE	   *fd = NULL;
! 						char	   *file = strdup(p + 1);
  						int			cmdend;
  
  						cmdend = strcspn(file, "`");
--- 248,254 ----
  				case '`':
  					{
  						FILE	   *fd = NULL;
! 						char	   *file = xstrdup(p + 1);
  						int			cmdend;
  
  						cmdend = strcspn(file, "`");
***************
*** 274,280 ****
  						const char *val;
  						int			nameend;
  
! 						name = strdup(p + 1);
  						nameend = strcspn(name, ":");
  						name[nameend] = '\0';
  						val = GetVariable(pset.vars, name);
--- 274,280 ----
  						const char *val;
  						int			nameend;
  
! 						name = xstrdup(p + 1);
  						nameend = strcspn(name, ":");
  						name[nameend] = '\0';
  						val = GetVariable(pset.vars, name);
Index: src/bin/psql/startup.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/startup.c,v
retrieving revision 1.81
diff -c -r1.81 startup.c
*** src/bin/psql/startup.c	29 Nov 2003 19:52:07 -0000	1.81
--- src/bin/psql/startup.c	23 Jan 2004 19:01:26 -0000
***************
*** 170,176 ****
  		if (strcmp(options.username, "\001") == 0)
  			username = simple_prompt("User name: ", 100, true);
  		else
! 			username = strdup(options.username);
  	}
  
  	if (pset.getPassword)
--- 170,176 ----
  		if (strcmp(options.username, "\001") == 0)
  			username = simple_prompt("User name: ", 100, true);
  		else
! 			username = xstrdup(options.username);
  	}
  
  	if (pset.getPassword)
***************
*** 567,581 ****
  
  	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);
! 		}
! 
  		sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
  		if (access(psqlrc, R_OK) == 0)
  			process_file(psqlrc);
  		else
--- 567,576 ----
  
  	if (home)
  	{
! 		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: src/bin/psql/stringutils.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/stringutils.c,v
retrieving revision 1.36
diff -c -r1.36 stringutils.c
*** src/bin/psql/stringutils.c	1 Dec 2003 22:14:40 -0000	1.36
--- src/bin/psql/stringutils.c	23 Jan 2004 19:43:00 -0000
***************
*** 77,85 ****
  		 * 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" */
  		strcpy(storage, s);
  		string = storage;
  	}
--- 77,83 ----
  		 * tokens.	2X the space is a gross overestimate, but it's
  		 * unlikely that this code will be used on huge strings anyway.
  		 */
! 		storage = xmalloc(2 * strlen(s) + 1);
  		strcpy(storage, s);
  		string = storage;
  	}
Index: src/bin/psql/tab-complete.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/tab-complete.c,v
retrieving revision 1.98
diff -c -r1.98 tab-complete.c
*** src/bin/psql/tab-complete.c	10 Jan 2004 02:21:08 -0000	1.98
--- src/bin/psql/tab-complete.c	23 Jan 2004 19:49:48 -0000
***************
*** 1485,1493 ****
  		/* Set up suitably-escaped copies of textual inputs */
  		if (text)
  		{
! 			e_text = (char *) malloc(strlen(text) * 2 + 1);
! 			if (!e_text)
! 				return NULL;
  			PQescapeString(e_text, text, strlen(text));
  		}
  		else
--- 1485,1491 ----
  		/* Set up suitably-escaped copies of textual inputs */
  		if (text)
  		{
! 			e_text = xmalloc(strlen(text) * 2 + 1);
  			PQescapeString(e_text, text, strlen(text));
  		}
  		else
***************
*** 1495,1510 ****
  
  		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;
! 			}
  			PQescapeString(e_info_charp, completion_info_charp,
! 						   strlen(completion_info_charp));
  		}
  		else
  			e_info_charp = NULL;
--- 1493,1504 ----
  
  		if (completion_info_charp)
  		{
! 			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,
! 						   charp_len);
  		}
  		else
  			e_info_charp = NULL;
***************
*** 1794,1808 ****
  	}
  
  	/* 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;
! 	}
  
  	strncpy(s, &rl_line_buffer[start], end - start + 1);
  	s[end - start + 1] = '\0';
--- 1788,1794 ----
  	}
  
  	/* make a copy */
! 	s = xmalloc(end - start + 2);
  
  	strncpy(s, &rl_line_buffer[start], end - start + 1);
  	s[end - start + 1] = '\0';
***************
*** 1828,1834 ****
  	(void) quote_pointer;		/* not used */
  
  	length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2);
! 	s = malloc(length);
  	s[0] = '\'';
  	strcpy(s + 1, text);
  	if (match_type == SINGLE_MATCH)
--- 1814,1820 ----
  	(void) quote_pointer;		/* not used */
  
  	length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2);
! 	s = xmalloc(length);
  	s[0] = '\'';
  	strcpy(s + 1, text);
  	if (match_type == SINGLE_MATCH)
***************
*** 1849,1855 ****
  		return xstrdup(text);
  
  	length = strlen(text);
! 	s = malloc(length - 2 + 1);
  	strncpy(s, text +1, length - 2);
  	s[length] = '\0';
  
--- 1835,1841 ----
  		return xstrdup(text);
  
  	length = strlen(text);
! 	s = xmalloc(length - 2 + 1);
  	strncpy(s, text +1, length - 2);
  	s[length] = '\0';
  
Index: src/bin/psql/variables.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/bin/psql/variables.c,v
retrieving revision 1.15
diff -c -r1.15 variables.c
*** src/bin/psql/variables.c	1 Dec 2003 22:14:40 -0000	1.15
--- src/bin/psql/variables.c	23 Jan 2004 19:17:00 -0000
***************
*** 14,32 ****
  {
  	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;
! 	}
  
  	return ptr;
  }
--- 14,22 ----
  {
  	struct _variable *ptr;
  
! 	ptr = xcalloc(1, sizeof *ptr);
! 	ptr->name = xstrdup("@");
! 	ptr->value = xstrdup("");
  
  	return ptr;
  }
***************
*** 162,180 ****
  		if (strcmp(current->name, name) == 0)
  		{
  			free(current->value);
! 			current->value = strdup(value);
! 			return current->value ? true : false;
  		}
  	}
  
! 	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;
  }
  
  bool
--- 152,166 ----
  		if (strcmp(current->name, name) == 0)
  		{
  			free(current->value);
! 			current->value = xstrdup(value);
! 			return true;
  		}
  	}
  
! 	previous->next = xcalloc(1, sizeof *(previous->next));
! 	previous->next->name = xstrdup(name);
! 	previous->next->value = xstrdup(value);
! 	return true;
  }
  
  bool
