Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16cc537

Browse files
committed
Avoid malloc(0) when printing a table of no columns. On some platforms
this returns NULL, which confuses the code.
1 parent643dfb7 commit16cc537

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

‎src/bin/psql/print.c

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.34 2002/10/24 01:33:50 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.35 2002/11/01 15:12:19 tgl Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"common.h"
@@ -212,21 +212,29 @@ print_aligned_text(const char *title, const char *const * headers,
212212
for (ptr=headers;*ptr;ptr++)
213213
col_count++;
214214

215-
widths=calloc(col_count,sizeof(*widths));
216-
if (!widths)
215+
if (col_count>0)
217216
{
218-
perror("calloc");
219-
exit(EXIT_FAILURE);
220-
}
217+
widths=calloc(col_count,sizeof(*widths));
218+
if (!widths)
219+
{
220+
perror("calloc");
221+
exit(EXIT_FAILURE);
222+
}
221223

222-
head_w=calloc(col_count,sizeof(*head_w));
223-
if (!head_w)
224+
head_w=calloc(col_count,sizeof(*head_w));
225+
if (!head_w)
226+
{
227+
perror("calloc");
228+
exit(EXIT_FAILURE);
229+
}
230+
}
231+
else
224232
{
225-
perror("calloc");
226-
exit(EXIT_FAILURE);
233+
widths=NULL;
234+
head_w=NULL;
227235
}
228236

229-
/* count rows */
237+
/* countcells (rows * cols) */
230238
for (ptr=cells;*ptr;ptr++)
231239
cell_count++;
232240

@@ -240,23 +248,25 @@ print_aligned_text(const char *title, const char *const * headers,
240248
}
241249
}
242250
else
243-
cell_w=0;
244-
251+
cell_w=NULL;
245252

246253
/* calc column widths */
247254
for (i=0;i<col_count;i++)
248255
{
249-
if ((tmp=pg_wcswidth((unsignedchar*)headers[i],strlen(headers[i])))>widths[i])
256+
tmp=pg_wcswidth((unsignedchar*)headers[i],strlen(headers[i]));
257+
if (tmp>widths[i])
250258
widths[i]=tmp;
251259
head_w[i]=tmp;
252260
}
253261

254262
for (i=0,ptr=cells;*ptr;ptr++,i++)
255263
{
256-
if ((tmp=pg_wcswidth((unsignedchar*)*ptr,strlen(*ptr)))>widths[i %col_count])
264+
tmp=pg_wcswidth((unsignedchar*)*ptr,strlen(*ptr));
265+
if (tmp>widths[i %col_count])
257266
widths[i %col_count]=tmp;
258267
cell_w[i]=tmp;
259268
}
269+
260270
if (opt_border==0)
261271
total_w=col_count-1;
262272
elseif (opt_border==1)
@@ -272,10 +282,11 @@ print_aligned_text(const char *title, const char *const * headers,
272282
{
273283
inttlen;
274284

275-
if ((unsignedint) (tlen=pg_wcswidth((unsignedchar*)title,strlen(title))) >=total_w)
285+
tlen=pg_wcswidth((unsignedchar*)title,strlen(title));
286+
if (tlen >= (int)total_w)
276287
fprintf(fout,"%s\n",title);
277288
else
278-
fprintf(fout,"%-*s%s\n", (int)(total_w-tlen) /2,"",title);
289+
fprintf(fout,"%-*s%s\n", ((int)total_w-tlen) /2,"",title);
279290
}
280291

281292
/* print headers */
@@ -330,7 +341,7 @@ print_aligned_text(const char *title, const char *const * headers,
330341
}
331342

332343
/* content */
333-
if (opt_align[(i) %col_count]=='r')
344+
if (opt_align[i %col_count]=='r')
334345
{
335346
fprintf(fout,"%*s%s",
336347
widths[i %col_count]-cell_w[i],"",cells[i]);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp