This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/78858] New: Bogus -Wnonnull warning involving strcmp.
- From: "marc.mutz at kdab dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 19 Dec 2016 12:11:14 +0000
- Subject: [Bug c++/78858] New: Bogus -Wnonnull warning involving strcmp.
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78858
Bug ID: 78858
Summary: Bogus -Wnonnull warning involving strcmp.
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: marc.mutz at kdab dot com
Target Milestone: ---
Latest trunk emits spurious -Wnonnull warnings, eg. in Qt:
qt5/qtbase/src/corelib/kernel/qcoreapplication.cpp: In member function ‘void
QCoreApplicationPrivate::processCommandLineArguments()’:
qt5/qtbase/src/corelib/kernel/qcoreapplication.cpp:195:20: error: argument 1
null where non-null expected [-Werror=nonnull]
if (strncmp(arg, "-qmljsdebugger=", 15) == 0) {
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
The surrounding code reads:
int j = argc ? 1 : 0;
for (int i = 1; i < argc; ++i) {
if (!argv[i])
continue;
if (*argv[i] != '-') {
argv[j++] = argv[i];
continue;
}
const char *arg = argv[i];
if (arg[1] == '-') // startsWith("--")
++arg;
if (strncmp(arg, "-qmljsdebugger=", 15) == 0) {
qmljs_debug_arguments = QString::fromLocal8Bit(arg + 15);
where argc and argv are member variables (of types int and char**, resp.).
This warning is bogus for two reasons: First, we deref 'arg' in the preceding
if-statements. If it was nullptr, then that would invoke UB, so the compiler
can assume that arg is non-null. Second, we explicitly check for a nullptr
'arg' as the first thing in the loop, and we call no functions in-between that
could change the state. We also don't do perform any atomic operations.
This is not an isolated error. GCC creates dozens of warnings in Qt code, but
most of them are not as clearly wrong as this one.
This one I cannot even fix by moving the arg definition to the first line of
the loop and checking for !arg in the first if:
const char *arg = argv[i];
if (!arg)
continue;