https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94335
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |diagnostic CC| |msebor at gcc dot gnu.org --- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> --- This type of warning is new GCC 10; it was added in the commit below. It works as designed here. It sees the following IL (the memset calls don't do anything). The MEM[] = 65; statement is what triggers it. <bb 2> [local count: 1073741824]: aDummyBuffer_4 = malloc (10); it ={v} {CLOBBER}; if (aDummyBuffer_4 != 0B) goto <bb 3>; [70.00%] else goto <bb 18>; [30.00%] <bb 3> [local count: 751619281]: _24 = aDummyBuffer_4 - ⁢ it.d = _24; itCopy ={v} {CLOBBER}; if (_24 != -9223372036854775808) goto <bb 4>; [94.29%] else goto <bb 5>; [5.71%] <bb 4> [local count: 708669601]: _23 = aDummyBuffer_4 - &itCopy; itCopy.d = _23; *aDummyBuffer_4 = 65; aDummySource_97 = malloc (10); D.40357 ={v} {CLOBBER}; _17 = aDummyBuffer_4 - &D.40357; D.40357.d = _17; goto <bb 6>; [100.00%] <bb 5> [local count: 365072224]: itCopy.d = -424242; MEM[(char *)&itCopy + -424242B] = 65; <<< warning here aDummySource_105 = malloc (10); D.40357 ={v} {CLOBBER}; D.40357.d = -424242; ... <bb 18> [local count: 322122544]: it.d = -9223372036854775808; itCopy ={v} {CLOBBER}; goto <bb 5>; [100.00%] It doesn't matter (much) whether the initial address is or can be null (the warning persists even with operator new that doesn't return null or when the ctor never does set d to kEmptyPointer). The branch of the code that sets d to -424242 isn't eliminated because the pointer subtraction in either ctor could, as far as GCC can tell, result in the same value as kEmptyPointer. Asserting that the subtraction doesn't result in such a value, for instance like so: if (d == kEmptyPointer) __builtin_unreachable (); and also guaranteeing that the initial address isn't null (e.g., by using operator new) eliminates the warning. Short of teaching GCC that the magnitude of the difference between any two pointers must be less than PTRDIFF_MAX I don't think there's anything that can be done do improve things (either codegen, or avoid the warning in this case). I'll leave this report unresolved in case someone feels otherwise. commit b631bdb3c16e85f35d38e39b3d315c35e4a5747c Author: Martin Sebor <mse...@redhat.com> Date: Thu Jul 25 00:29:17 2019 +0000 PR tree-optimization/91183 - strlen of a strcpy result with a conditional source not folded PR tree-optimization/91183 - strlen of a strcpy result with a conditional source not folded PR tree-optimization/86688 - missing -Wstringop-overflow using a non-string local array in strnlen with excessive bound