Hi Bruno,

On NetBSD I can see the following warning when compiling
lib/c-vasnprintf.c:

    depbase=`echo c-vasnprintf.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`; gcc 
-DHAVE_CONFIG_H -I. -I..  -DGNULIB_STRICT_CHECKING=1    -g -O2 -MT 
c-vasnprintf.o -MD -MP -MF $depbase.Tpo -c -o c-vasnprintf.o c-vasnprintf.c && 
mv -f $depbase.Tpo $depbase.Po
    In file included from c-vasnprintf.c:46:
    vasnprintf.c: In function 'c_vasnprintf':
    vasnprintf.c:6662:35: warning: statement will never be executed 
[-Wswitch-unreachable]
     6662 |                     pad_ourselves = 1;
          |                     ~~~~~~~~~~~~~~^~~

The cause is PAD_OURSELVES being initialized in a switch statement with
no preceding case statement. In the C23 standard ยง 6.8.5.3, Example 7,
it explains that initilizations and function calls with no preceding
case statement cannot be reached.

It happens on a few other platforms IIRC, based on skimming the CI a
while ago.

Any objections to the attached patch? I know we don't care too much
about warnings on other platforms, but since this is enabled by default,
I think it is worth fixing.

Collin

>From 6f21a61a6458de619ad88fc0363fb2475646a150 Mon Sep 17 00:00:00 2001
Message-ID: <6f21a61a6458de619ad88fc0363fb2475646a150.1751835887.git.collin.fu...@gmail.com>
From: Collin Funk <collin.fu...@gmail.com>
Date: Sun, 6 Jul 2025 13:48:53 -0700
Subject: [PATCH] vasnprintf: Silence -Wswitch-unreachable warning.

* lib/vasnprintf.c (VASNPRINTF): Don't place code in the switch
statement unless there is a case statement before it.
---
 ChangeLog        | 4 ++++
 lib/vasnprintf.c | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 286d589d23..be650c7dde 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2025-07-06  Collin Funk  <collin.fu...@gmail.com>
 
+	vasnprintf: Silence -Wswitch-unreachable warning.
+	* lib/vasnprintf.c (VASNPRINTF): Don't place code in the switch
+	statement unless there is a case statement before it.
+
 	pagealign_alloc: Don't assume pointers fit in 'unsigned long'.
 	* modules/pagealign_alloc (Depends-on): Add stdint-h.
 	* lib/pagealign_alloc.c: Include stdint.h.
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index f46e8701bd..f84a1690c7 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -6659,8 +6659,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                   case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
                   case 'a': case 'A':
 # endif
+# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO
                     pad_ourselves = 1;
                     break;
+# endif
                   default:
                     pad_ourselves = prec_ourselves | group_ourselves;
                     break;
-- 
2.50.0

Reply via email to