From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C89E83858C2F; Tue, 25 Oct 2022 16:03:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C89E83858C2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666713825; bh=LD73Zb5RWjp9448tN/4lo8a2sOrID0gUmkNMen3AWpo=; h=From:To:Subject:Date:From; b=Iqsfan3nq7yJm22/zGLiLEFA1iqOMjNbziwUg4mY9oEwg6Zkf/q7yRrM8dZNkS5Cp cRCuURX7799JfhHSGgMX2ymYUpBQGqAiJa7aOh7Cwk+xQSy5AZGcFyAG6hEv3hGUyE mzW/6YWSjFCAONMryS7NF2ZNrvkv7+1PyXv3+XfQ= From: "nrk at disroot dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/107395] New: Missed warning opportunity on bultin string optimization Date: Tue, 25 Oct 2022 16:03:45 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: nrk at disroot dot 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107395 Bug ID: 107395 Summary: Missed warning opportunity on bultin string optimization Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: nrk at disroot dot org Target Milestone: --- In the first case, it optimizes the strlen call with 0, with the assumption that for a single byte array to be a valid string, it has to be 0 len; while it = can clearly see that *s !=3D '\0' In the 2nd case, it optimizes the call out to 2, which is correct. But in the 3rd case (with missing nul-byte) it figures something is wrong a= nd just goes ahead an let's the libc strlen deal with it. In both cases (f and f3) it should be possible (I assume without too much effort, given that f3 seems to be aware of something going wrong) to detect that strlen is being called with a non-nul-terminated byte array and issue a warning. Perhaps a good candidate for -Wstringop-overread ? #include size_t f(void) { char s[1] =3D "h"; return strlen(s); /* "optimized out" to 0 */ } size_t f2(void) { char s[] =3D "hi"; return strlen(s); /* optimized out to 2 */ } size_t f3(void) { char s[2] =3D "hi"; return strlen(s); /* calls strlen */ } Tested with gcc 12.2, compiled with -O2 (https://godbolt.org/z/1KW7qna1E)=