Skip to content

Commit

Permalink
strfmon: Fix formatting of a second fixed-width value
Browse files Browse the repository at this point in the history
There is a bug when formatting two consecutive values using fixed-widths
and the values need padding.  This was because the value of pad_size
was zeroed only every other time.

Format           Before                         After
[%8n] [%8n]      [ $123.45] [       $123.45]    [ $123.45] [ $123.45]

Reviewed by:	kib
PR:	267282
Github PR:	#619
MFC after:	1 week
  • Loading branch information
jlduran authored and kostikbel committed Oct 25, 2022
1 parent 750fe3e commit 34f8852
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/libc/stdlib/strfmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
fmt = format;
asciivalue = NULL;
currency_symbol = NULL;
pad_size = 0;

while (*fmt) {
/* pass nonformating characters AS IS */
Expand All @@ -155,6 +154,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
/* set up initial values */
flags = (NEED_GROUPING|LOCALE_POSN);
pad_char = ' '; /* padding character is "space" */
pad_size = 0; /* no padding initially */
left_prec = -1; /* no left precision specified */
right_prec = -1; /* no right precision specified */
width = -1; /* no width specified */
Expand Down
2 changes: 1 addition & 1 deletion lib/libc/tests/stdlib/strfmon_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ATF_TC_BODY(strfmon_examples, tc)
const char *expected;
} tests[] = {
{ "%n", "[$123.45] [-$123.45] [$3,456.78]" },
{ "%11n", "[ $123.45] [ -$123.45] [ $3,456.78]" }, /* XXX */
{ "%11n", "[ $123.45] [ -$123.45] [ $3,456.78]" },
{ "%#5n", "[ $ 123.45] [-$ 123.45] [ $ 3,456.78]" },
{ "%=*#5n", "[ $***123.45] [-$***123.45] [ $*3,456.78]" },
{ "%=0#5n", "[ $000123.45] [-$000123.45] [ $03,456.78]" },
Expand Down

0 comments on commit 34f8852

Please sign in to comment.