Paul Eggert wrote:
> That being said, I'll try to keep in mind that you prefer "default: 
> break;"s in code you've written.
Definitely, yes, please keep them.

It's a maintainability issue: Assume that in the future the enum definition
gets extended. When the programmer had omitted the 'default: break;'
clause, they are now *relying* on gcc's -Wswitch-enum to tell them when to
adapt the code. Whereas with the 'default: break;' clause, the code
will at least do something reasonable (maybe not fully correct) without
programmer intervention.

> I got reports of a couple more places where -Wswitch-enum complains 
> about Gnulib code, when building coreutils.

But with yesterday's patches, you can now ignore the warnings. The coreutils'
choice of compiler warning options MUST NOT force coding style changes
on Gnulib.

> For now I installed the attached to pacify GCC there.

I'm adding this patch to refine the 'default:' case behaviour.


2025-02-04  Bruno Haible  <br...@clisp.org>

        *vasnprintf: Add a stricter runtime check.
        * lib/printf-args.c (PRINTF_FETCHARGS): Abort in case of an unknown
        type.

diff --git a/lib/printf-args.c b/lib/printf-args.c
index e0bf87c7ee..b83ec1e8a8 100644
--- a/lib/printf-args.c
+++ b/lib/printf-args.c
@@ -32,6 +32,9 @@
 /* Get INT_WIDTH.  */
 #include <limits.h>
 
+/* Get abort().  */
+#include <stdlib.h>
+
 #ifdef STATIC
 STATIC
 #endif
@@ -297,9 +300,18 @@ PRINTF_FETCHARGS (va_list args, arguments *a)
         break;
 #endif
       case TYPE_NONE:
-      default:
-        /* Unknown type.  */
+        /* Argument i is not used by any directive, but some argument with
+           number > i is used by a format directive.  POSIX says that this
+           is invalid:
+             "When numbered argument specifications are used, specifying the
+              Nth argument requires that all the leading arguments, from the
+              first to the (N-1)th, are specified in the format string."
+           The reason is that we cannot know how many bytes to skip in the
+           va_arg sequence.  */
         return -1;
+      default:
+        /* Unknown type.  Should not happen.  */
+        abort ();
       }
   return 0;
 }




Reply via email to