Skip to content

Commit cff9c57

Browse files
committed
Fix malloc length for new numeric separator patch.
Centralize malloc into function.
1 parent c3f1b0f commit cff9c57

File tree

1 file changed

+26
-62
lines changed

1 file changed

+26
-62
lines changed

src/bin/psql/print.c

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.63 2005/07/10 15:53:42 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.64 2005/07/14 06:46:17 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -29,6 +29,20 @@
2929

3030
#include "mbprint.h"
3131

32+
static void *
33+
pg_local_malloc(size_t size)
34+
{
35+
void *tmp;
36+
37+
tmp = malloc(size);
38+
if (!tmp)
39+
{
40+
psql_error("out of memory\n");
41+
exit(EXIT_FAILURE);
42+
}
43+
return tmp;
44+
}
45+
3246
static int
3347
num_numericseps(const char *my_str)
3448
{
@@ -46,6 +60,7 @@ num_numericseps(const char *my_str)
4660
else
4761
return int_len / 3 - 1; /* no leading separator */
4862
}
63+
4964
static int
5065
len_with_numericsep(const char *my_str)
5166
{
@@ -77,12 +92,7 @@ format_numericsep(char *my_str, char *numericsep)
7792
if (digits_before_sep == 0)
7893
new_len--; /* no leading separator */
7994

80-
new_str = malloc(new_len);
81-
if (!new_str)
82-
{
83-
fprintf(stderr, _("out of memory\n"));
84-
exit(EXIT_FAILURE);
85-
}
95+
new_str = pg_local_malloc(new_len + 1);
8696

8797
for (i=0, j=0; ; i++, j++)
8898
{
@@ -167,13 +177,8 @@ print_unaligned_text(const char *title, const char *const *headers,
167177
if ((opt_align[i % col_count] == 'r') && strlen(*ptr) > 0 &&
168178
opt_numericsep != NULL && strlen(opt_numericsep) > 0)
169179
{
170-
char *my_cell = malloc(len_with_numericsep(*ptr));
180+
char *my_cell = pg_local_malloc(len_with_numericsep(*ptr) + 1);
171181

172-
if (!my_cell)
173-
{
174-
fprintf(stderr, _("out of memory\n"));
175-
exit(EXIT_FAILURE);
176-
}
177182
strcpy(my_cell, *ptr);
178183
format_numericsep(my_cell, opt_numericsep);
179184
fputs(my_cell, fout);
@@ -249,13 +254,8 @@ print_unaligned_vertical(const char *title, const char *const *headers,
249254
if ((opt_align[i % col_count] == 'r') && strlen(*ptr) != 0 &&
250255
opt_numericsep != NULL && strlen(opt_numericsep) > 0)
251256
{
252-
char *my_cell = malloc(len_with_numericsep(*ptr));
257+
char *my_cell = pg_local_malloc(len_with_numericsep(*ptr) + 1);
253258

254-
if (!my_cell)
255-
{
256-
fprintf(stderr, _("out of memory\n"));
257-
exit(EXIT_FAILURE);
258-
}
259259
strcpy(my_cell, *ptr);
260260
format_numericsep(my_cell, opt_numericsep);
261261
fputs(my_cell, fout);
@@ -482,13 +482,8 @@ print_aligned_text(const char *title, const char *const *headers,
482482
{
483483
if (strlen(*ptr) > 0 && opt_numericsep != NULL && strlen(opt_numericsep) > 0)
484484
{
485-
char *my_cell = malloc(cell_w[i]);
485+
char *my_cell = pg_local_malloc(cell_w[i] + 1);
486486

487-
if (!my_cell)
488-
{
489-
fprintf(stderr, _("out of memory\n"));
490-
exit(EXIT_FAILURE);
491-
}
492487
strcpy(my_cell, *ptr);
493488
format_numericsep(my_cell, opt_numericsep);
494489
fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", my_cell);
@@ -634,12 +629,7 @@ print_aligned_vertical(const char *title, const char *const *headers,
634629
fprintf(fout, "%s\n", title);
635630

636631
/* make horizontal border */
637-
divider = malloc(hwidth + dwidth + 10);
638-
if (!divider)
639-
{
640-
fprintf(stderr, _("out of memory\n"));
641-
exit(EXIT_FAILURE);
642-
}
632+
divider = pg_local_malloc(hwidth + dwidth + 10);
643633
divider[0] = '\0';
644634
if (opt_border == 2)
645635
strcat(divider, "+-");
@@ -661,15 +651,9 @@ print_aligned_vertical(const char *title, const char *const *headers,
661651
{
662652
if (!opt_barebones)
663653
{
664-
char *record_str = malloc(32);
654+
char *record_str = pg_local_malloc(32);
665655
size_t record_str_len;
666656

667-
if (!record_str)
668-
{
669-
fprintf(stderr, _("out of memory\n"));
670-
exit(EXIT_FAILURE);
671-
}
672-
673657
if (opt_border == 0)
674658
snprintf(record_str, 32, "* Record %d", record++);
675659
else
@@ -709,13 +693,8 @@ print_aligned_vertical(const char *title, const char *const *headers,
709693
fputs(" ", fout);
710694

711695
{
712-
char *my_cell = malloc(cell_w[i]);
696+
char *my_cell = pg_local_malloc(cell_w[i] + 1);
713697

714-
if (!my_cell)
715-
{
716-
fprintf(stderr, _("out of memory\n"));
717-
exit(EXIT_FAILURE);
718-
}
719698
strcpy(my_cell, *ptr);
720699
if ((opt_align[i % col_count] == 'r') && strlen(*ptr) != 0 &&
721700
opt_numericsep != NULL && strlen(opt_numericsep) > 0)
@@ -855,13 +834,8 @@ print_html_text(const char *title, const char *const *headers,
855834
else if ((opt_align[i % col_count] == 'r') && strlen(*ptr) != 0 &&
856835
opt_numericsep != NULL && strlen(opt_numericsep) > 0)
857836
{
858-
char *my_cell = malloc(len_with_numericsep(*ptr));
837+
char *my_cell = pg_local_malloc(len_with_numericsep(*ptr) + 1);
859838

860-
if (!my_cell)
861-
{
862-
fprintf(stderr, _("out of memory\n"));
863-
exit(EXIT_FAILURE);
864-
}
865839
strcpy(my_cell, *ptr);
866840
format_numericsep(my_cell, opt_numericsep);
867841
html_escaped_print(my_cell, fout);
@@ -946,13 +920,8 @@ print_html_vertical(const char *title, const char *const *headers,
946920
else if ((opt_align[i % col_count] == 'r') && strlen(*ptr) != 0 &&
947921
opt_numericsep != NULL && strlen(opt_numericsep) > 0)
948922
{
949-
char *my_cell = malloc(len_with_numericsep(*ptr));
923+
char *my_cell = pg_local_malloc(len_with_numericsep(*ptr) + 1);
950924

951-
if (!my_cell)
952-
{
953-
fprintf(stderr, _("out of memory\n"));
954-
exit(EXIT_FAILURE);
955-
}
956925
strcpy(my_cell, *ptr);
957926
format_numericsep(my_cell, opt_numericsep);
958927
html_escaped_print(my_cell, fout);
@@ -1646,12 +1615,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
16461615
exit(EXIT_FAILURE);
16471616
}
16481617

1649-
footers[0] = malloc(100);
1650-
if (!footers[0])
1651-
{
1652-
fprintf(stderr, _("out of memory\n"));
1653-
exit(EXIT_FAILURE);
1654-
}
1618+
footers[0] = pg_local_malloc(100);
16551619
if (PQntuples(result) == 1)
16561620
snprintf(footers[0], 100, _("(1 row)"));
16571621
else

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy