Skip to content

Commit 8ddd22f

Browse files
committed
Fix incorrect psql \x memory allocation for numericlocale. Redesign API
to be less error-prone.
1 parent 451cd7d commit 8ddd22f

File tree

1 file changed

+41
-51
lines changed

1 file changed

+41
-51
lines changed

src/bin/psql/print.c

Lines changed: 41 additions & 51 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.75 2005/09/26 18:09:57 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.76 2005/09/27 16:30:25 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -86,22 +86,26 @@ strlen_with_numeric_locale(const char *my_str)
8686
return strlen(my_str) + additional_numeric_locale_len(my_str);
8787
}
8888

89-
static void
90-
format_numeric_locale(char *my_str)
89+
static char *
90+
format_numeric_locale(const char *my_str)
9191
{
9292
int i, j, int_len = integer_digits(my_str), leading_digits;
9393
int groupdigits = atoi(grouping);
94-
char *new_str;
95-
96-
if (my_str[0] == '-')
97-
my_str++;
98-
99-
new_str = pg_local_malloc(strlen_with_numeric_locale(my_str) + 1);
94+
int new_str_start = 0;
95+
char *new_str = new_str = pg_local_malloc(
96+
strlen_with_numeric_locale(my_str) + 1);
10097

10198
leading_digits = (int_len % groupdigits != 0) ?
10299
int_len % groupdigits : groupdigits;
103100

104-
for (i=0, j=0; ; i++, j++)
101+
if (my_str[0] == '-') /* skip over sign, affects grouping calculations */
102+
{
103+
new_str[0] = my_str[0];
104+
my_str++;
105+
new_str_start = 1;
106+
}
107+
108+
for (i=0, j=new_str_start; ; i++, j++)
105109
{
106110
/* Hit decimal point? */
107111
if (my_str[i] == '.')
@@ -130,8 +134,7 @@ format_numeric_locale(char *my_str)
130134
new_str[j] = my_str[i];
131135
}
132136

133-
strcpy(my_str, new_str);
134-
free(new_str);
137+
return new_str;
135138
}
136139

137140
/*************************/
@@ -185,10 +188,8 @@ print_unaligned_text(const char *title, const char *const *headers,
185188
}
186189
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
187190
{
188-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
189-
190-
strcpy(my_cell, *ptr);
191-
format_numeric_locale(my_cell);
191+
char *my_cell = format_numeric_locale(*ptr);
192+
192193
fputs(my_cell, fout);
193194
free(my_cell);
194195
}
@@ -261,10 +262,8 @@ print_unaligned_vertical(const char *title, const char *const *headers,
261262
fputs(opt_fieldsep, fout);
262263
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
263264
{
264-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
265-
266-
strcpy(my_cell, *ptr);
267-
format_numeric_locale(my_cell);
265+
char *my_cell = format_numeric_locale(*ptr);
266+
268267
fputs(my_cell, fout);
269268
free(my_cell);
270269
}
@@ -488,13 +487,11 @@ print_aligned_text(const char *title, const char *const *headers,
488487
{
489488
if (opt_numeric_locale)
490489
{
491-
char *my_cell = pg_local_malloc(cell_w[i] + 1);
490+
char *my_cell = format_numeric_locale(*ptr);
492491

493-
strcpy(my_cell, *ptr);
494-
format_numeric_locale(my_cell);
495492
fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", my_cell);
496493
free(my_cell);
497-
}
494+
}
498495
else
499496
fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", *ptr);
500497
}
@@ -697,18 +694,23 @@ print_aligned_vertical(const char *title, const char *const *headers,
697694
else
698695
fputs(" ", fout);
699696

697+
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
700698
{
701-
char *my_cell = pg_local_malloc(cell_w[i] + 1);
702-
703-
strcpy(my_cell, *ptr);
704-
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
705-
format_numeric_locale(my_cell);
699+
char *my_cell = format_numeric_locale(*ptr);
700+
706701
if (opt_border < 2)
707702
fprintf(fout, "%s\n", my_cell);
708703
else
709704
fprintf(fout, "%-s%*s |\n", my_cell, dwidth - cell_w[i], "");
710705
free(my_cell);
711706
}
707+
else
708+
{
709+
if (opt_border < 2)
710+
fprintf(fout, "%s\n", *ptr);
711+
else
712+
fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], "");
713+
}
712714
}
713715

714716
if (opt_border == 2)
@@ -837,10 +839,8 @@ print_html_text(const char *title, const char *const *headers,
837839
fputs("&nbsp; ", fout);
838840
else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
839841
{
840-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
842+
char *my_cell = format_numeric_locale(*ptr);
841843

842-
strcpy(my_cell, *ptr);
843-
format_numeric_locale(my_cell);
844844
html_escaped_print(my_cell, fout);
845845
free(my_cell);
846846
}
@@ -922,10 +922,8 @@ print_html_vertical(const char *title, const char *const *headers,
922922
fputs("&nbsp; ", fout);
923923
else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
924924
{
925-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
926-
927-
strcpy(my_cell, *ptr);
928-
format_numeric_locale(my_cell);
925+
char *my_cell = format_numeric_locale(*ptr);
926+
929927
html_escaped_print(my_cell, fout);
930928
free(my_cell);
931929
}
@@ -1064,10 +1062,8 @@ print_latex_text(const char *title, const char *const *headers,
10641062
{
10651063
if (opt_numeric_locale)
10661064
{
1067-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
1068-
1069-
strcpy(my_cell, *ptr);
1070-
format_numeric_locale(my_cell);
1065+
char *my_cell = format_numeric_locale(*ptr);
1066+
10711067
latex_escaped_print(my_cell, fout);
10721068
free(my_cell);
10731069
}
@@ -1177,10 +1173,8 @@ print_latex_vertical(const char *title, const char *const *headers,
11771173
{
11781174
if (opt_numeric_locale)
11791175
{
1180-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
1176+
char *my_cell = format_numeric_locale(*ptr);
11811177

1182-
strcpy(my_cell, *ptr);
1183-
format_numeric_locale(my_cell);
11841178
latex_escaped_print(my_cell, fout);
11851179
free(my_cell);
11861180
}
@@ -1277,10 +1271,8 @@ print_troff_ms_text(const char *title, const char *const *headers,
12771271
{
12781272
if (opt_numeric_locale)
12791273
{
1280-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
1281-
1282-
strcpy(my_cell, *ptr);
1283-
format_numeric_locale(my_cell);
1274+
char *my_cell = format_numeric_locale(*ptr);
1275+
12841276
troff_ms_escaped_print(my_cell, fout);
12851277
free(my_cell);
12861278
}
@@ -1389,10 +1381,8 @@ print_troff_ms_vertical(const char *title, const char *const *headers,
13891381
fputc('\t', fout);
13901382
if (opt_numeric_locale)
13911383
{
1392-
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1);
1393-
1394-
strcpy(my_cell, *ptr);
1395-
format_numeric_locale(my_cell);
1384+
char *my_cell = format_numeric_locale(*ptr);
1385+
13961386
troff_ms_escaped_print(my_cell, fout);
13971387
free(my_cell);
13981388
}

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