1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

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().
This commit is contained in:
Neil Conway
2004-01-24 19:38:49 +00:00
parent cb3dc829f6
commit 610d33c194
13 changed files with 124 additions and 237 deletions

View 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++)
{