git: 8e5a2135210d - stable/13 - strfmon: Fix alignment when enclosed by parentheses

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 01 Nov 2022 00:46:32 UTC
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=8e5a2135210d2c53ef8bdcbb1b268d4bb5081f04

commit 8e5a2135210d2c53ef8bdcbb1b268d4bb5081f04
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-10-14 23:26:32 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-11-01 00:44:41 +0000

    strfmon: Fix alignment when enclosed by parentheses
    
    (cherry picked from commit 947efadc3d6e778a824618d82f53f061bec69b77)
---
 lib/libc/stdlib/strfmon.c            | 12 ++++++++++--
 lib/libc/tests/stdlib/strfmon_test.c |  4 ++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/lib/libc/stdlib/strfmon.c b/lib/libc/stdlib/strfmon.c
index d6619f4d7d54..a26521bb694d 100644
--- a/lib/libc/stdlib/strfmon.c
+++ b/lib/libc/stdlib/strfmon.c
@@ -373,8 +373,12 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 			PRINTS(signstr);
 		}
 
-		if (sign_posn == 0 && (flags & IS_NEGATIVE))
-			PRINT(')');
+		if (sign_posn == 0) {
+			if (flags & IS_NEGATIVE)
+				PRINT(')');
+			else if (left_prec >= 0)
+				PRINT(' ');
+		}
 
 		if (dst - tmpptr < width) {
 			if (flags & LEFT_JUSTIFY) {
@@ -466,6 +470,10 @@ __calc_left_pad(int flags, char *cur_symb)
 	}
 
 	switch (sign_posn) {
+		case 0:
+			if (flags & IS_NEGATIVE)
+				left_chars++;
+			break;
 		case 1:
 			left_chars += strlen(signstr);
 			break;
diff --git a/lib/libc/tests/stdlib/strfmon_test.c b/lib/libc/tests/stdlib/strfmon_test.c
index b5b22f9a483d..d4d1f6a580d9 100644
--- a/lib/libc/tests/stdlib/strfmon_test.c
+++ b/lib/libc/tests/stdlib/strfmon_test.c
@@ -81,8 +81,8 @@ ATF_TC_BODY(strfmon_examples, tc)
 	    { "%^#5n", "[ $  123.45] [-$  123.45] [ $ 3456.78]" },
 	    { "%^#5.0n", "[ $  123] [-$  123] [ $ 3457]" },
 	    { "%^#5.4n", "[ $  123.4500] [-$  123.4500] [ $ 3456.7810]" },
-	    { "%(#5n", "[$   123.45] [($   123.45)] [$ 3,456.78]" }, /* XXX */
-	    { "%!(#5n", "[   123.45] [(   123.45)] [ 3,456.78]" }, /* XXX */
+	    { "%(#5n", "[ $   123.45 ] [($   123.45)] [ $ 3,456.78 ]" },
+	    { "%!(#5n", "[    123.45 ] [(   123.45)] [  3,456.78 ]" },
 	    { "%-14#5.4n", "[ $   123.4500 ] [-$   123.4500 ] [ $ 3,456.7810 ]" },
 	    { "%14#5.4n", "[  $   123.4500] [ -$   123.4500] [  $ 3,456.7810]" },
 	};