From f3550a3f8a954d7e1316067998fd3b4b13574f5f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 4 Feb 2025 11:46:34 +0100 Subject: [PATCH] *vasnprintf: Add a stricter runtime check. * lib/printf-args.c (PRINTF_FETCHARGS): Abort in case of an unknown type. --- ChangeLog | 8 +++++++- lib/printf-args.c | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index a5fdf782e1..5b85354635 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2025-02-04 Bruno Haible + + *vasnprintf: Add a stricter runtime check. + * lib/printf-args.c (PRINTF_FETCHARGS): Abort in case of an unknown + type. + 2025-02-03 Bruno Haible bootstrap: Remove undesired output. @@ -7,7 +13,7 @@ 2025-02-03 Paul Eggert - c-vasnprintf: pacify -Wswitch-enum + *vasnprintf: pacify -Wswitch-enum * lib/printf-args.c (PRINTF_FETCHARGS): Mention TYPE_NONE as being a default case. * lib/vasnprintf.c (VASNPRINTF): Use switch (+E). 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 +/* Get abort(). */ +#include + #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; } -- 2.39.5