aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2026-11-15 16:37:42 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2026-11-15 17:16:55 +0000
commit5e809bf1c93fdc4c52fa46a7eb0e6f881b6c6140 (patch)
tree6aff862af4feb54d8943344773a232d3520677eb /lib
parent0e3fce268adeec8f4c87b99759dae6a592b3d365 (diff)
stdio: Fix positional %wN / %wfN conversion
MFC after: 1 week Fixes: bce0bef3c6ab ("libc: Implement N2680.") Fixes: 12b1c1e3fb44 ("libc: Add test cases for N2680.")
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdio/printf-pos.c68
-rw-r--r--lib/libc/tests/stdio/snprintf_test.c8
-rw-r--r--lib/libc/tests/stdio/swprintf_test.c8
3 files changed, 84 insertions, 0 deletions
diff --git a/lib/libc/stdio/printf-pos.c b/lib/libc/stdio/printf-pos.c
index 27b59f25b67d..fd92b3d5ad29 100644
--- a/lib/libc/stdio/printf-pos.c
+++ b/lib/libc/stdio/printf-pos.c
@@ -337,6 +337,40 @@ reswitch: switch (ch) {
case 't':
flags |= PTRDIFFT;
goto rflag;
+ case 'w':
+ flags &= ~(CHARINT|SHORTINT|LONGINT|LLONGINT|INTMAXT);
+ if (fmt[0] == 'f') {
+ flags |= FASTINT;
+ fmt++;
+ } else {
+ flags &= ~FASTINT;
+ }
+ if (fmt[0] == '8') {
+ if (!(flags & FASTINT))
+ flags |= CHARINT;
+ else
+ /* no flag set = 32 */ ;
+ fmt += 1;
+ } else if (fmt[0] == '1' && fmt[1] == '6') {
+ if (!(flags & FASTINT))
+ flags |= SHORTINT;
+ else
+ /* no flag set = 32 */ ;
+ fmt += 2;
+ } else if (fmt[0] == '3' && fmt[1] == '2') {
+ /* no flag set = 32 */ ;
+ fmt += 2;
+ } else if (fmt[0] == '6' && fmt[1] == '4') {
+ flags |= LLONGINT;
+ fmt += 2;
+ } else {
+ if (flags & FASTINT) {
+ flags &= ~FASTINT;
+ fmt--;
+ }
+ goto error;
+ }
+ goto rflag;
case 'z':
flags |= SIZET;
goto rflag;
@@ -532,6 +566,40 @@ reswitch: switch (ch) {
case 't':
flags |= PTRDIFFT;
goto rflag;
+ case 'w':
+ flags &= ~(CHARINT|SHORTINT|LONGINT|LLONGINT|INTMAXT);
+ if (fmt[0] == 'f') {
+ flags |= FASTINT;
+ fmt++;
+ } else {
+ flags &= ~FASTINT;
+ }
+ if (fmt[0] == '8') {
+ if (!(flags & FASTINT))
+ flags |= CHARINT;
+ else
+ /* no flag set = 32 */ ;
+ fmt += 1;
+ } else if (fmt[0] == '1' && fmt[1] == '6') {
+ if (!(flags & FASTINT))
+ flags |= SHORTINT;
+ else
+ /* no flag set = 32 */ ;
+ fmt += 2;
+ } else if (fmt[0] == '3' && fmt[1] == '2') {
+ /* no flag set = 32 */ ;
+ fmt += 2;
+ } else if (fmt[0] == '6' && fmt[1] == '4') {
+ flags |= LLONGINT;
+ fmt += 2;
+ } else {
+ if (flags & FASTINT) {
+ flags &= ~FASTINT;
+ fmt--;
+ }
+ goto error;
+ }
+ goto rflag;
case 'z':
flags |= SIZET;
goto rflag;
diff --git a/lib/libc/tests/stdio/snprintf_test.c b/lib/libc/tests/stdio/snprintf_test.c
index d5f056b9542d..b805f513c402 100644
--- a/lib/libc/tests/stdio/snprintf_test.c
+++ b/lib/libc/tests/stdio/snprintf_test.c
@@ -171,6 +171,10 @@ ATF_TC_BODY(snprintf_wN, tc)
SNPRINTF_TEST("wd", "%wd", 0);
SNPRINTF_TEST("w1d", "%w1d", 0);
SNPRINTF_TEST("w128d", "%w128d", 0);
+
+ SNPRINTF_TEST(" 1", "%1$*2$w16d", (int16_t)1, 2);
+ SNPRINTF_TEST(" 1", "%1$*2$w32d", (int32_t)1, 2);
+ SNPRINTF_TEST(" 1", "%1$*2$w64d", (int64_t)1, 2);
}
ATF_TC_WITHOUT_HEAD(snprintf_wfN);
@@ -203,6 +207,10 @@ ATF_TC_BODY(snprintf_wfN, tc)
SNPRINTF_TEST("wfd", "%wfd", 0);
SNPRINTF_TEST("wf1d", "%wf1d", 0);
SNPRINTF_TEST("wf128d", "%wf128d", 0);
+
+ SNPRINTF_TEST(" 1", "%1$*2$w16d", (int_fast16_t)1, 2);
+ SNPRINTF_TEST(" 1", "%1$*2$w32d", (int_fast32_t)1, 2);
+ SNPRINTF_TEST(" 1", "%1$*2$w64d", (int_fast64_t)1, 2);
}
ATF_TP_ADD_TCS(tp)
diff --git a/lib/libc/tests/stdio/swprintf_test.c b/lib/libc/tests/stdio/swprintf_test.c
index 5d9c60fb9ee7..6f3d56657f4c 100644
--- a/lib/libc/tests/stdio/swprintf_test.c
+++ b/lib/libc/tests/stdio/swprintf_test.c
@@ -172,6 +172,10 @@ ATF_TC_BODY(swprintf_wN, tc)
SWPRINTF_TEST("wd", "%wd", 0);
SWPRINTF_TEST("w1d", "%w1d", 0);
SWPRINTF_TEST("w128d", "%w128d", 0);
+
+ SWPRINTF_TEST(" 1", "%1$*2$w16d", (int16_t)1, 2);
+ SWPRINTF_TEST(" 1", "%1$*2$w32d", (int32_t)1, 2);
+ SWPRINTF_TEST(" 1", "%1$*2$w64d", (int64_t)1, 2);
}
ATF_TC_WITHOUT_HEAD(swprintf_wfN);
@@ -204,6 +208,10 @@ ATF_TC_BODY(swprintf_wfN, tc)
SWPRINTF_TEST("wfd", "%wfd", 0);
SWPRINTF_TEST("wf1d", "%wf1d", 0);
SWPRINTF_TEST("wf128d", "%wf128d", 0);
+
+ SWPRINTF_TEST(" 1", "%1$*2$w16d", (int_fast16_t)1, 2);
+ SWPRINTF_TEST(" 1", "%1$*2$w32d", (int_fast32_t)1, 2);
+ SWPRINTF_TEST(" 1", "%1$*2$w64d", (int_fast64_t)1, 2);
}
ATF_TP_ADD_TCS(tp)