33 *
44 * Copyright (c) 2000-2003, PostgreSQL Global Development Group
55 *
6- * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.47 2004/05/18 20:18:58 momjian Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.48 2004/05/23 22: 20:10 neilc Exp $
77 */
88#include "postgres_fe.h"
99#include "common.h"
@@ -1133,15 +1133,14 @@ void
11331133printQuery (const PGresult * result , const printQueryOpt * opt , FILE * fout )
11341134{
11351135 int nfields ;
1136+ int ncells ;
11361137 const char * * headers ;
11371138 const char * * cells ;
11381139 char * * footers ;
11391140 char * align ;
11401141 int i ;
11411142
1142-
11431143 /* extract headers */
1144-
11451144 nfields = PQnfields (result );
11461145
11471146 headers = calloc (nfields + 1 , sizeof (* headers ));
@@ -1155,15 +1154,15 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
11551154 headers [i ] = mbvalidate (PQfname (result , i ), opt -> topt .encoding );
11561155
11571156 /* set cells */
1158-
1159- cells = calloc (nfields * PQntuples ( result ) + 1 , sizeof (* cells ));
1157+ ncells = PQntuples ( result ) * nfields ;
1158+ cells = calloc (ncells + 1 , sizeof (* cells ));
11601159 if (!cells )
11611160 {
11621161 perror ("calloc" );
11631162 exit (EXIT_FAILURE );
11641163 }
11651164
1166- for (i = 0 ; i < nfields * PQntuples ( result ) ; i ++ )
1165+ for (i = 0 ; i < ncells ; i ++ )
11671166 {
11681167 if (PQgetisnull (result , i / nfields , i % nfields ))
11691168 cells [i ] = opt -> nullPrint ? opt -> nullPrint : "" ;
@@ -1185,6 +1184,11 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
11851184 }
11861185
11871186 footers [0 ] = malloc (100 );
1187+ if (!footers [0 ])
1188+ {
1189+ perror ("malloc" );
1190+ exit (EXIT_FAILURE );
1191+ }
11881192 if (PQntuples (result ) == 1 )
11891193 snprintf (footers [0 ], 100 , gettext ("(1 row)" ));
11901194 else
@@ -1194,7 +1198,6 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
11941198 footers = NULL ;
11951199
11961200 /* set alignment */
1197-
11981201 align = calloc (nfields + 1 , sizeof (* align ));
11991202 if (!align )
12001203 {
@@ -1221,13 +1224,12 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
12211224 }
12221225
12231226 /* call table printer */
1224-
12251227 printTable (opt -> title , headers , cells ,
1226- footers ? (const char * const * ) footers : ( const char * const * ) ( opt -> footers ) ,
1228+ (const char * const * ) footers ,
12271229 align , & opt -> topt , fout );
12281230
1229- free (( void * ) headers );
1230- free (( void * ) cells );
1231+ free (headers );
1232+ free (cells );
12311233 if (footers )
12321234 {
12331235 free (footers [0 ]);
0 commit comments