diff --git a/ofstd/include/dcmtk/ofstd/offile.h b/ofstd/include/dcmtk/ofstd/offile.h index 4ddb768f4..1f8452841 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);