https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116834
Bug ID: 116834
Summary: "warning: null format string" false positive with
UBSAN
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: other
Assignee: unassigned at gcc dot gnu.org
Reporter: kasper93 at gmail dot com
Target Milestone: ---
Hello,
I got the following warning:
test.c: In function 'foo':
test.c:7:5: warning: null format string [-Wformat-truncation=]
7 | vsnprintf(NULL, 0, fmt, ap);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
When compiling the code with `gcc -fsanitize=undefined -Wformat -O2`
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
void foo(const char *fmt, va_list ap)
{
vsnprintf(NULL, 0, fmt, ap);
if (!fmt)
abort();
}
This issue only occurs with UBSAN. The example provided is minimized and may
not make much sense. The main point is that if there is a check for the `fmt`
value after the `vsnprintf()` call, it will trigger a warning like the one
above. This warning is incorrect because the compiler cannot determine whether
the value is null at compile time in this case.