Skip to content

Commit c961f40

Browse files
committed
Further fix for psql's code for locale-aware formatting of numeric output.
(Third time's the charm, I hope.) Additional testing disclosed that this code could mangle already-localized output from the "money" datatype. We can't very easily skip applying it to "money" values, because the logic is tied to column right-justification and people expect "money" output to be right-justified. Short of decoupling that, we can fix it in what should be a safe enough way by testing to make sure the string doesn't contain any characters that would not be expected in plain numeric output.
1 parent 49917ed commit c961f40

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/bin/psql/print.c

+23-7
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,34 @@ additional_numeric_locale_len(const char *my_str)
161161
}
162162

163163
/*
164+
* Format a numeric value per current LC_NUMERIC locale setting
165+
*
164166
* Returns the appropriately formatted string in a new allocated block,
165-
* caller must free
167+
* caller must free.
168+
*
169+
* setDecimalLocale() must have been called earlier.
166170
*/
167171
static char *
168172
format_numeric_locale(const char *my_str)
169173
{
170-
int new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
171-
char *new_str = pg_malloc(new_len + 1);
172-
int int_len = integer_digits(my_str);
173-
int i,
174-
leading_digits;
175-
int new_str_pos = 0;
174+
char *new_str;
175+
int new_len,
176+
int_len,
177+
leading_digits,
178+
i,
179+
new_str_pos;
180+
181+
/*
182+
* If the string doesn't look like a number, return it unchanged. This
183+
* check is essential to avoid mangling already-localized "money" values.
184+
*/
185+
if (strspn(my_str, "0123456789+-.eE") != strlen(my_str))
186+
return pg_strdup(my_str);
187+
188+
new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
189+
new_str = pg_malloc(new_len + 1);
190+
new_str_pos = 0;
191+
int_len = integer_digits(my_str);
176192

177193
/* number of digits in first thousands group */
178194
leading_digits = int_len % groupdigits;

0 commit comments

Comments
 (0)