Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 77130fc

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 6325527 commit 77130fc

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
@@ -227,18 +227,34 @@ additional_numeric_locale_len(const char *my_str)
227227
}
228228

229229
/*
230+
* Format a numeric value per current LC_NUMERIC locale setting
231+
*
230232
* Returns the appropriately formatted string in a new allocated block,
231-
* caller must free
233+
* caller must free.
234+
*
235+
* setDecimalLocale() must have been called earlier.
232236
*/
233237
static char *
234238
format_numeric_locale(const char *my_str)
235239
{
236-
int new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
237-
char *new_str = pg_malloc(new_len + 1);
238-
int int_len = integer_digits(my_str);
239-
int i,
240-
leading_digits;
241-
int new_str_pos = 0;
240+
char *new_str;
241+
int new_len,
242+
int_len,
243+
leading_digits,
244+
i,
245+
new_str_pos;
246+
247+
/*
248+
* If the string doesn't look like a number, return it unchanged. This
249+
* check is essential to avoid mangling already-localized "money" values.
250+
*/
251+
if (strspn(my_str, "0123456789+-.eE") != strlen(my_str))
252+
return pg_strdup(my_str);
253+
254+
new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
255+
new_str = pg_malloc(new_len + 1);
256+
new_str_pos = 0;
257+
int_len = integer_digits(my_str);
242258

243259
/* number of digits in first thousands group */
244260
leading_digits = int_len % groupdigits;

0 commit comments

Comments
 (0)