From 471501ae4439a9270437c5e47784f9d9d34b7151 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Fri, 10 Jul 2026 16:43:37 -0500 Subject: [PATCH] Added printf format attribute to OFFile printf wrappers. OFFile::fprintf and OFFile::vfprintf forward their format parameter to ::vfprintf, which triggers clang/gcc -Wformat-nonliteral in every translation unit that includes offile.h. Annotating the wrappers with __attribute__((format(printf, ...))) marks the parameter as a valid forwarding target and gives callers full format/argument checking. --- ofstd/include/dcmtk/ofstd/offile.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ofstd/include/dcmtk/ofstd/offile.h b/ofstd/include/dcmtk/ofstd/offile.h index 4ddb768f41..1f8452841c 100644 --- a/ofstd/include/dcmtk/ofstd/offile.h +++ b/ofstd/include/dcmtk/ofstd/offile.h @@ -47,6 +47,20 @@ END_EXTERN_C #undef clearerr #endif +/* Enable compile-time checking of arguments passed to the printf-style + * member functions of OFFile, and mark their format parameter as a valid + * forwarding target so -Wformat-nonliteral does not warn inside them. + * FMT is the 1-based position of the format string parameter and ARGS the + * position of the first variadic argument (0 for va_list functions); the + * implicit 'this' parameter of non-static member functions counts as + * parameter 1. + */ +#if defined(__GNUC__) || defined(__clang__) +#define OFFILE_FORMAT_PRINTF(FMT, ARGS) __attribute__((format(printf, FMT, ARGS))) +#else +#define OFFILE_FORMAT_PRINTF(FMT, ARGS) +#endif + /* When using the ISO C++ include files such as , etc., * all ANSI C functions like fopen() are declared in namespace std, * (e.g. we have to use std::fopen()), but non-ANSI Posix functions remain @@ -884,6 +898,7 @@ class DCMTK_OFSTD_EXPORT OFFile * @param ... further parameters according to format string * @return number of characters printed */ + OFFILE_FORMAT_PRINTF(2, 3) int fprintf(const char *format, ...) { int result = 0; @@ -899,6 +914,7 @@ class DCMTK_OFSTD_EXPORT OFFile * @param arg list of further parameters according to format string * @return number of characters printed */ + OFFILE_FORMAT_PRINTF(2, 0) int vfprintf(const char *format, va_list arg) { return :: vfprintf(file_, format, arg);