Commit 16cc5372 authored by Tom Lane's avatar Tom Lane

Avoid malloc(0) when printing a table of no columns. On some platforms

this returns NULL, which confuses the code.
parent 643dfb78
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.34 2002/10/24 01:33:50 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.35 2002/11/01 15:12:19 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -212,21 +212,29 @@ print_aligned_text(const char *title, const char *const * headers, ...@@ -212,21 +212,29 @@ print_aligned_text(const char *title, const char *const * headers,
for (ptr = headers; *ptr; ptr++) for (ptr = headers; *ptr; ptr++)
col_count++; col_count++;
widths = calloc(col_count, sizeof(*widths)); if (col_count > 0)
if (!widths)
{ {
perror("calloc"); widths = calloc(col_count, sizeof(*widths));
exit(EXIT_FAILURE); if (!widths)
} {
perror("calloc");
exit(EXIT_FAILURE);
}
head_w = calloc(col_count, sizeof(*head_w)); head_w = calloc(col_count, sizeof(*head_w));
if (!head_w) if (!head_w)
{
perror("calloc");
exit(EXIT_FAILURE);
}
}
else
{ {
perror("calloc"); widths = NULL;
exit(EXIT_FAILURE); head_w = NULL;
} }
/* count rows */ /* count cells (rows * cols) */
for (ptr = cells; *ptr; ptr++) for (ptr = cells; *ptr; ptr++)
cell_count++; cell_count++;
...@@ -240,23 +248,25 @@ print_aligned_text(const char *title, const char *const * headers, ...@@ -240,23 +248,25 @@ print_aligned_text(const char *title, const char *const * headers,
} }
} }
else else
cell_w = 0; cell_w = NULL;
/* calc column widths */ /* calc column widths */
for (i = 0; i < col_count; i++) for (i = 0; i < col_count; i++)
{ {
if ((tmp = pg_wcswidth((unsigned char *) headers[i], strlen(headers[i]))) > widths[i]) tmp = pg_wcswidth((unsigned char *) headers[i], strlen(headers[i]));
if (tmp > widths[i])
widths[i] = tmp; widths[i] = tmp;
head_w[i] = tmp; head_w[i] = tmp;
} }
for (i = 0, ptr = cells; *ptr; ptr++, i++) for (i = 0, ptr = cells; *ptr; ptr++, i++)
{ {
if ((tmp = pg_wcswidth((unsigned char *) *ptr, strlen(*ptr))) > widths[i % col_count]) tmp = pg_wcswidth((unsigned char *) *ptr, strlen(*ptr));
if (tmp > widths[i % col_count])
widths[i % col_count] = tmp; widths[i % col_count] = tmp;
cell_w[i] = tmp; cell_w[i] = tmp;
} }
if (opt_border == 0) if (opt_border == 0)
total_w = col_count - 1; total_w = col_count - 1;
else if (opt_border == 1) else if (opt_border == 1)
...@@ -272,10 +282,11 @@ print_aligned_text(const char *title, const char *const * headers, ...@@ -272,10 +282,11 @@ print_aligned_text(const char *title, const char *const * headers,
{ {
int tlen; int tlen;
if ((unsigned int) (tlen = pg_wcswidth((unsigned char *) title, strlen(title))) >= total_w) tlen = pg_wcswidth((unsigned char *) title, strlen(title));
if (tlen >= (int) total_w)
fprintf(fout, "%s\n", title); fprintf(fout, "%s\n", title);
else else
fprintf(fout, "%-*s%s\n", (int) (total_w - tlen) / 2, "", title); fprintf(fout, "%-*s%s\n", ((int) total_w - tlen) / 2, "", title);
} }
/* print headers */ /* print headers */
...@@ -330,7 +341,7 @@ print_aligned_text(const char *title, const char *const * headers, ...@@ -330,7 +341,7 @@ print_aligned_text(const char *title, const char *const * headers,
} }
/* content */ /* content */
if (opt_align[(i) % col_count] == 'r') if (opt_align[i % col_count] == 'r')
{ {
fprintf(fout, "%*s%s", fprintf(fout, "%*s%s",
widths[i % col_count] - cell_w[i], "", cells[i]); widths[i % col_count] - cell_w[i], "", cells[i]);
......
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