git: 1d8c4da381e8 - stable/15 - stdio: Fix positional %wN / %wfN conversion

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 12:04:35 UTC
The branch stable/15 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=1d8c4da381e8dd00b83ddc748366595792d9fc8d

commit 1d8c4da381e8dd00b83ddc748366595792d9fc8d
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-15 16:37:42 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-20 12:03:56 +0000

    stdio: Fix positional %wN / %wfN conversion
    
    MFC after:      1 week
    Fixes:          bce0bef3c6ab ("libc: Implement N2680.")
    Fixes:          12b1c1e3fb44 ("libc: Add test cases for N2680.")
    
    (cherry picked from commit 5e809bf1c93fdc4c52fa46a7eb0e6f881b6c6140)
---
 lib/libc/stdio/printf-pos.c          | 68 ++++++++++++++++++++++++++++++++++++
 lib/libc/tests/stdio/snprintf_test.c |  8 +++++
 lib/libc/tests/stdio/swprintf_test.c |  8 +++++
 3 files changed, 84 insertions(+)

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)