[PATCH] handle pathological anti-ranges in gimple_fold_builtin_memory_op (PR 81908)

Martin Sebor msebor@gmail.com
Wed Aug 23 20:49:00 GMT 2017


Bug 81908 is about a -Wstringop-overflow warning for a Fortran
test triggered by a recent VRP improvement.  A simple test case
that approximates the warning is:

   void f (char *d, const char *s, size_t n)
   {
     if (n > 0 && n <= SIZE_MAX / 2)
       n = 0;

     memcpy (d, s, n);   // n in ~[1, SIZE_MAX / 2]
   }

Since the only valid value of n is zero the call to memcpy can
be considered a no-op (a value of n > SIZE_MAX is in excess of
the maximum size of the largest object and would surely make
the call crash).

The important difference between the test case and Bug 81908
is that in the latter, the code is emitted by GCC itself from
what appears to be correct source (looks like it's the result
of the loop distribution pass).  I believe the warning for
the test case above and for other human-written code like it
is helpful, but warning for code emitted by GCC, even if it's
dead or unreachable, is obviously not (at least not to users).

The attached patch enhances the gimple_fold_builtin_memory_op
function to eliminate this patholohgical case by making use
of range information to fold into no-ops calls to memcpy whose
size argument is in a range where the only valid value is zero.
This gets rid of the warning and improves the emitted code.

Tested on x86_64-linux.

Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc-81908.diff
Type: text/x-patch
Size: 5398 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20170823/5a02f95d/attachment.bin>


More information about the Gcc-patches mailing list