Fix incorrect psql \x memory allocation for numericlocale. Redesign API

to be less error-prone.
This commit is contained in:
Bruce Momjian 2005-09-27 16:30:25 +00:00
parent 451cd7d426
commit 8ddd22f245
1 changed files with 41 additions and 51 deletions

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2005, PostgreSQL Global Development Group * Copyright (c) 2000-2005, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.75 2005/09/26 18:09:57 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.76 2005/09/27 16:30:25 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
@ -86,22 +86,26 @@ strlen_with_numeric_locale(const char *my_str)
return strlen(my_str) + additional_numeric_locale_len(my_str); return strlen(my_str) + additional_numeric_locale_len(my_str);
} }
static void static char *
format_numeric_locale(char *my_str) format_numeric_locale(const char *my_str)
{ {
int i, j, int_len = integer_digits(my_str), leading_digits; int i, j, int_len = integer_digits(my_str), leading_digits;
int groupdigits = atoi(grouping); int groupdigits = atoi(grouping);
char *new_str; int new_str_start = 0;
char *new_str = new_str = pg_local_malloc(
if (my_str[0] == '-') strlen_with_numeric_locale(my_str) + 1);
my_str++;
new_str = pg_local_malloc(strlen_with_numeric_locale(my_str) + 1);
leading_digits = (int_len % groupdigits != 0) ? leading_digits = (int_len % groupdigits != 0) ?
int_len % groupdigits : groupdigits; int_len % groupdigits : groupdigits;
for (i=0, j=0; ; i++, j++) if (my_str[0] == '-') /* skip over sign, affects grouping calculations */
{
new_str[0] = my_str[0];
my_str++;
new_str_start = 1;
}
for (i=0, j=new_str_start; ; i++, j++)
{ {
/* Hit decimal point? */ /* Hit decimal point? */
if (my_str[i] == '.') if (my_str[i] == '.')
@ -130,8 +134,7 @@ format_numeric_locale(char *my_str)
new_str[j] = my_str[i]; new_str[j] = my_str[i];
} }
strcpy(my_str, new_str); return new_str;
free(new_str);
} }
/*************************/ /*************************/
@ -185,10 +188,8 @@ print_unaligned_text(const char *title, const char *const *headers,
} }
if (opt_align[i % col_count] == 'r' && opt_numeric_locale) if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
fputs(my_cell, fout); fputs(my_cell, fout);
free(my_cell); free(my_cell);
} }
@ -261,10 +262,8 @@ print_unaligned_vertical(const char *title, const char *const *headers,
fputs(opt_fieldsep, fout); fputs(opt_fieldsep, fout);
if (opt_align[i % col_count] == 'r' && opt_numeric_locale) if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
fputs(my_cell, fout); fputs(my_cell, fout);
free(my_cell); free(my_cell);
} }
@ -488,13 +487,11 @@ print_aligned_text(const char *title, const char *const *headers,
{ {
if (opt_numeric_locale) if (opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(cell_w[i] + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", my_cell); fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", my_cell);
free(my_cell); free(my_cell);
} }
else else
fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", *ptr); fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", *ptr);
} }
@ -697,18 +694,23 @@ print_aligned_vertical(const char *title, const char *const *headers,
else else
fputs(" ", fout); fputs(" ", fout);
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(cell_w[i] + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
format_numeric_locale(my_cell);
if (opt_border < 2) if (opt_border < 2)
fprintf(fout, "%s\n", my_cell); fprintf(fout, "%s\n", my_cell);
else else
fprintf(fout, "%-s%*s |\n", my_cell, dwidth - cell_w[i], ""); fprintf(fout, "%-s%*s |\n", my_cell, dwidth - cell_w[i], "");
free(my_cell); free(my_cell);
} }
else
{
if (opt_border < 2)
fprintf(fout, "%s\n", *ptr);
else
fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], "");
}
} }
if (opt_border == 2) if (opt_border == 2)
@ -837,10 +839,8 @@ print_html_text(const char *title, const char *const *headers,
fputs("&nbsp; ", fout); fputs("&nbsp; ", fout);
else if (opt_align[i % col_count] == 'r' && opt_numeric_locale) else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
html_escaped_print(my_cell, fout); html_escaped_print(my_cell, fout);
free(my_cell); free(my_cell);
} }
@ -922,10 +922,8 @@ print_html_vertical(const char *title, const char *const *headers,
fputs("&nbsp; ", fout); fputs("&nbsp; ", fout);
else if (opt_align[i % col_count] == 'r' && opt_numeric_locale) else if (opt_align[i % col_count] == 'r' && opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
html_escaped_print(my_cell, fout); html_escaped_print(my_cell, fout);
free(my_cell); free(my_cell);
} }
@ -1064,10 +1062,8 @@ print_latex_text(const char *title, const char *const *headers,
{ {
if (opt_numeric_locale) if (opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
latex_escaped_print(my_cell, fout); latex_escaped_print(my_cell, fout);
free(my_cell); free(my_cell);
} }
@ -1177,10 +1173,8 @@ print_latex_vertical(const char *title, const char *const *headers,
{ {
if (opt_numeric_locale) if (opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
latex_escaped_print(my_cell, fout); latex_escaped_print(my_cell, fout);
free(my_cell); free(my_cell);
} }
@ -1277,10 +1271,8 @@ print_troff_ms_text(const char *title, const char *const *headers,
{ {
if (opt_numeric_locale) if (opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
troff_ms_escaped_print(my_cell, fout); troff_ms_escaped_print(my_cell, fout);
free(my_cell); free(my_cell);
} }
@ -1389,10 +1381,8 @@ print_troff_ms_vertical(const char *title, const char *const *headers,
fputc('\t', fout); fputc('\t', fout);
if (opt_numeric_locale) if (opt_numeric_locale)
{ {
char *my_cell = pg_local_malloc(strlen_with_numeric_locale(*ptr) + 1); char *my_cell = format_numeric_locale(*ptr);
strcpy(my_cell, *ptr);
format_numeric_locale(my_cell);
troff_ms_escaped_print(my_cell, fout); troff_ms_escaped_print(my_cell, fout);
free(my_cell); free(my_cell);
} }