From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CFF4B384B0C1; Tue, 21 Apr 2020 16:36:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CFF4B384B0C1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1587487004; bh=cg+ALohnKwd3utxmciUa+onS5hOXgqD37LaYD0E24tc=; h=From:To:Subject:Date:From; b=lNLnmK8XcIJgxRZuG0myGCWYFFAULsaJ54iLuMlJnTcV39Updvw/3l6yKIwlI8FHD D6ZCG3/kgkq1aX+Xb9ysgnPjvcH3w3gYxbS0W8b0tauJTTLj5QNexMN0eBNPWEG4kl WMJ3D6rP502WP/zmLDUCEHth5hjd0b4yNASbEtIQ= From: "msebor at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/94699] New: enhance -Wstring-compare to detect tautological or mutually exclusive strcmp expressions Date: Tue, 21 Apr 2020 16:36:44 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 10.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: msebor at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Apr 2020 16:36:44 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D94699 Bug ID: 94699 Summary: enhance -Wstring-compare to detect tautological or mutually exclusive strcmp expressions Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The article referenced in pr94629 (https://habr.com/en/company/pvs-studio/blog/497640/) points out the follow= ing suspicious-looking code in GCC: static bool dw_val_equal_p (dw_val_node *a, dw_val_node *b) { .... case dw_val_class_vms_delta: return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1) && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)); .... } and goes on to say: PVS-Studio warning: V501 There are identical sub-expressions '!strcmp(a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)' to the left and= to the right of the '&&' operator. dwarf2out.c 1481 Two strcmp functions compare the same pointers. That is, a clearly redund= ant check is performed. The -Wstring-compare warning introduced in GCC 10 is designed to detect lik= ely erroneous uses of strcmp and strncmp that evaluate to a constant result due= to the sizes of the pointed-to arrays. Detecting also this pattern seems like= a natural and useful extension. (The warning is implemented in the strlen pa= ss while these issues might perhaps be better diagnosed earlier.) A simple test case: int f0 (const char *a, const char *b) {=20 // second strcmp call is redundant and can be eliminated; should // be diagnosed by -Wstring-compare (ditto with ||) return 0 =3D=3D __builtin_strcmp (a, b) && 0 =3D=3D __builtin_strcmp (b, = a); } int f1 (const char *a) { // results of calls are mutually exclusive and the whole expression // can be folded to zero; should be diagnosed by -Wstring-compare return 0 =3D=3D __builtin_strcmp (a, "123") && 0 =3D=3D __builtin_strcmp = (a, "456"); } Another similar warning designed to catch some of these likely bugs is -Wtautological-compare (see also pr70181).=