git: dd5393b28af2 - stable/14 - stdio: Fix positional %F conversion

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

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

commit dd5393b28af20abebc82fe1cdd32e415cc18d88d
Author:     Showta Ishizaki <zakinko@snowrabbit.org>
AuthorDate: 2026-09-28 21:55:42 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-20 12:04:05 +0000

    stdio: Fix positional %F conversion
    
    When a format uses positional arguments, __find_arguments() walks it
    first to learn the type of every argument, and lists the floating
    point conversions as A a E e f G g.  F is missing, so a double that
    is only referred to through %F never gets a type, its slot in the
    argument table stays unset, and the conversion reads whatever is
    there:
    
        printf("[%1$F]\n", -INFINITY);   [0.000000]
        printf("[%1$F]\n", 1.5);         [0.000000]
        printf("[%F]\n", 1.5);           [1.500000]
    
    With a width taken from another argument, "%1$*2$F", vsnprintf(3)
    returned lengths like 2103024 for a ten character result; abseil's
    str_format test allocates that much and was killed for it on
    FreeBSD.  The same format with %1$f, or with any other conversion of
    the same argument, works because those give it a type.
    
    Add F ahead of f in both __find_arguments() and __find_warguments(),
    and test %F, plain and positional, in snprintf_test and
    swprintf_test.
    
    MFC after:      1 week
    Signed-off-by:  Showta Ishizaki <zakinko@snowrabbit.org>
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2420
    
    (cherry picked from commit eb647dd28137c5f44030645a648e5a8c24e16f4f)
---
 lib/libc/stdio/printf-pos.c          | 14 ++++++++------
 lib/libc/tests/stdio/snprintf_test.c | 13 +++++++++++++
 lib/libc/tests/stdio/swprintf_test.c | 13 +++++++++++++
 3 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/lib/libc/stdio/printf-pos.c b/lib/libc/stdio/printf-pos.c
index 77a1218ccd91..394ec98e7e85 100644
--- a/lib/libc/stdio/printf-pos.c
+++ b/lib/libc/stdio/printf-pos.c
@@ -363,13 +363,14 @@ reswitch:	switch (ch) {
 				goto error;
 			break;
 #ifndef NO_FLOATING_POINT
-		case 'a':
 		case 'A':
-		case 'e':
+		case 'a':
 		case 'E':
+		case 'e':
+		case 'F':
 		case 'f':
-		case 'g':
 		case 'G':
+		case 'g':
 			error = addtype(&types,
 			    (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
 			if (error)
@@ -556,13 +557,14 @@ reswitch:	switch (ch) {
 				goto error;
 			break;
 #ifndef NO_FLOATING_POINT
-		case 'a':
 		case 'A':
-		case 'e':
+		case 'a':
 		case 'E':
+		case 'e':
+		case 'F':
 		case 'f':
-		case 'g':
 		case 'G':
+		case 'g':
 			error = addtype(&types,
 			    (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
 			if (error)
diff --git a/lib/libc/tests/stdio/snprintf_test.c b/lib/libc/tests/stdio/snprintf_test.c
index 29b908b65120..c8f04b7e5524 100644
--- a/lib/libc/tests/stdio/snprintf_test.c
+++ b/lib/libc/tests/stdio/snprintf_test.c
@@ -7,6 +7,7 @@
 #include <assert.h>
 #include <limits.h>
 #include <locale.h>
+#include <math.h>
 #include <stdint.h>
 #include <stdio.h>
 
@@ -81,6 +82,17 @@ ATF_TC_BODY(snprintf_d, tc)
 	SNPRINTF_TEST("2,147,483,647", "%'d", INT_MAX);
 }
 
+ATF_TC_WITHOUT_HEAD(snprintf_F);
+ATF_TC_BODY(snprintf_F, tc)
+{
+	SNPRINTF_TEST("1.500000", "%F", 1.5);
+	SNPRINTF_TEST("INF", "%F", INFINITY);
+	SNPRINTF_TEST("-INF", "%F", -INFINITY);
+	SNPRINTF_TEST("1.500000", "%1$F", 1.5);
+	SNPRINTF_TEST("-INF", "%1$F", -INFINITY);
+	SNPRINTF_TEST("  1.500000", "%1$*2$F", 1.5, 10);
+}
+
 ATF_TC_WITHOUT_HEAD(snprintf_x);
 ATF_TC_BODY(snprintf_x, tc)
 {
@@ -197,6 +209,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, snprintf_b);
 	ATF_TP_ADD_TC(tp, snprintf_B);
 	ATF_TP_ADD_TC(tp, snprintf_d);
+	ATF_TP_ADD_TC(tp, snprintf_F);
 	ATF_TP_ADD_TC(tp, snprintf_x);
 	ATF_TP_ADD_TC(tp, snprintf_X);
 	ATF_TP_ADD_TC(tp, snprintf_wN);
diff --git a/lib/libc/tests/stdio/swprintf_test.c b/lib/libc/tests/stdio/swprintf_test.c
index f00ecc7f1c1d..fe2957928344 100644
--- a/lib/libc/tests/stdio/swprintf_test.c
+++ b/lib/libc/tests/stdio/swprintf_test.c
@@ -7,6 +7,7 @@
 #include <assert.h>
 #include <limits.h>
 #include <locale.h>
+#include <math.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <wchar.h>
@@ -82,6 +83,17 @@ ATF_TC_BODY(swprintf_d, tc)
 	SWPRINTF_TEST("2,147,483,647", "%'d", INT_MAX);
 }
 
+ATF_TC_WITHOUT_HEAD(swprintf_F);
+ATF_TC_BODY(swprintf_F, tc)
+{
+	SWPRINTF_TEST("1.500000", "%F", 1.5);
+	SWPRINTF_TEST("INF", "%F", INFINITY);
+	SWPRINTF_TEST("-INF", "%F", -INFINITY);
+	SWPRINTF_TEST("1.500000", "%1$F", 1.5);
+	SWPRINTF_TEST("-INF", "%1$F", -INFINITY);
+	SWPRINTF_TEST("  1.500000", "%1$*2$F", 1.5, 10);
+}
+
 ATF_TC_WITHOUT_HEAD(swprintf_x);
 ATF_TC_BODY(swprintf_x, tc)
 {
@@ -198,6 +210,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, swprintf_b);
 	ATF_TP_ADD_TC(tp, swprintf_B);
 	ATF_TP_ADD_TC(tp, swprintf_d);
+	ATF_TP_ADD_TC(tp, swprintf_F);
 	ATF_TP_ADD_TC(tp, swprintf_x);
 	ATF_TP_ADD_TC(tp, swprintf_X);
 	ATF_TP_ADD_TC(tp, swprintf_wN);