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

--- Comment #18 from Boris Kolpackov <boris at kolpackov dot net> ---
This warning is still present when compiling the latest upstream release
(3.51.2) of sqlite3.c:

$ gcc-16 -c -O3 sqlite3.c

In function ‘sqlite3Strlen30’,
    inlined from ‘sqlite3ColumnSetColl’ at sqlite3.c:125075:10:
sqlite3.c:35961:28: warning: ‘strlen’ reading 1 or more bytes from a region of
size 0 [-Wstringop-overread]
35961 |   return 0x3fffffff & (int)strlen(z);
      |                            ^~~~~~~~~

Looking at the sqlite3Strlen30 function body we see:

35959: SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
35960:  if( z==0 ) return 0;
35961:   return 0x3fffffff & (int)strlen(z);
35962: }

Can someone explain to me how the strlen(z) call on line 35961 could possibly
dereference a NULL pointer when it is preceded by the NULL pointer check and
return on the previous line?

As an experiment, I replaced line 35961 with:

return 0x3fffffff & (z == 0 ? 0 : (int)strlen(z));

But got the same warning.

However, if I remove the preceding if(z==0) check, then the warning disappears.

Surely this is a work around for a GCC bug and not a "fix" we expect the
upstream to apply to their code?

Reply via email to