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

Commit 6325527

Browse files
committed
Further fix for psql's code for locale-aware formatting of numeric output.
On closer inspection, those seemingly redundant atoi() calls were not so much inefficient as just plain wrong: the author of this code either had not read, or had not understood, the POSIX specification for localeconv(). The grouping field is *not* a textual digit string but separate integers encoded as chars. We'll follow the existing code as well as the backend's cash.c in only honoring the first group width, but let's at least honor it correctly. This doesn't actually result in any behavioral change in any of the locales I have installed on my Linux box, which may explain why nobody's complained; grouping width 3 is close enough to universal that it's barely worth considering other cases. Still, wrong is wrong, so back-patch.
1 parent 4778a0b commit 6325527

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/bin/psql/print.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -3227,16 +3227,24 @@ setDecimalLocale(void)
32273227

32283228
extlconv = localeconv();
32293229

3230+
/* Don't accept an empty decimal_point string */
32303231
if (*extlconv->decimal_point)
32313232
decimal_point = pg_strdup(extlconv->decimal_point);
32323233
else
32333234
decimal_point = "."; /* SQL output standard */
32343235

3235-
if (*extlconv->grouping && atoi(extlconv->grouping) > 0)
3236-
groupdigits = atoi(extlconv->grouping);
3237-
else
3236+
/*
3237+
* Although the Open Group standard allows locales to supply more than one
3238+
* group width, we consider only the first one, and we ignore any attempt
3239+
* to suppress grouping by specifying CHAR_MAX. As in the backend's
3240+
* cash.c, we must apply a range check to avoid being fooled by variant
3241+
* CHAR_MAX values.
3242+
*/
3243+
groupdigits = *extlconv->grouping;
3244+
if (groupdigits <= 0 || groupdigits > 6)
32383245
groupdigits = 3; /* most common */
32393246

3247+
/* Don't accept an empty thousands_sep string, either */
32403248
/* similar code exists in formatting.c */
32413249
if (*extlconv->thousands_sep)
32423250
thousands_sep = pg_strdup(extlconv->thousands_sep);

0 commit comments

Comments
 (0)