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)