https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87041
Bug ID: 87041
Summary: GCC 8 regression: -Wformat "reading through null
pointer" on unreachable code
Product: gcc
Version: 8.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: achurch+gcc at achurch dot org
Target Milestone: ---
In GCC 8, the following code compiled with -O1 -Wformat warns about passing
NULL to a %s format token even though the call can never be executed:
extern int printf(const char *, ...);
void foo(void) {
const char * const s = 0;
if (s) printf("%s\n", s);
}
foo.c: In function 'foo':
foo.c:4:5: warning: reading through null pointer (argument 2) [-Wformat=]
if (s) printf("%s\n", s);
^~
This breaks testing macros of the following style, which worked fine through
GCC 7.3.0:
#define CHECK_STREQUAL(value, expected) do { \
const char * const _value = value; \
const char * const _expected = expected; \
if (!_value && _expected) FAIL("Got NULL, expected [%s]", _expected); \
if (_value && !_expected) FAIL("Got [%s], expected NULL", _value); \
if (_value && _expected && strcmp(_value, _expected) != 0) \
FAIL("Got [%s], expected [%s]", _value, _expected); \
} while (0)
Here, passing NULL for the value of "expected" triggers the warning on the
final FAIL() invocation, even though that case will never be executed when
"expected" is NULL.
The warning disappears at -O0 or if the variable is not const (e.g., "const
char *s" instead of "const char * const s").