public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "nrk at disroot dot org" <gcc-bugzilla@gcc.gnu.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	[thread overview]
Message-ID: <bug-107395-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107395

            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 != '\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 and
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 <string.h>

        size_t f(void) {
                char s[1] = "h";
                return strlen(s); /* "optimized out" to 0 */
        }

        size_t f2(void) {
                char s[] = "hi";
                return strlen(s); /* optimized out to 2 */
        }

        size_t f3(void) {
                char s[2] = "hi";
                return strlen(s); /* calls strlen */
        }


Tested with gcc 12.2, compiled with -O2 (https://godbolt.org/z/1KW7qna1E)

             reply	other threads:[~2022-10-25 16:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-25 16:03 nrk at disroot dot org [this message]
2022-10-25 19:28 ` [Bug tree-optimization/107395] Missed warning opportunity on builtin " marxin at gcc dot gnu.org
2022-10-25 19:41 ` nrk at disroot dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107395-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).