Ping: https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00652.html

This change is independent of either of the two patches below:
  https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00429.html
  https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00652.html

On 11/18/19 11:22 AM, Martin Sebor wrote:
Ping: https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00652.html

On 11/8/19 3:11 PM, Martin Sebor wrote:
Unless it's used with _FORTIFY_SOURCE, -Wstringop-overflow
doesn't consider out-of-bounds accesses to objects allocated
by alloca, malloc, other functions declared with attribute
alloc_size, or even VLAs with variable bounds.  This was
a known limitation of the checks (done just before expansion)
relying on the the object size pass when they were introduced
in GCC 7.

But since its introduction in GCC 7, the warning has evolved
beyond some of the limitations of the object size pass.  Unlike
it, the warning considers non-constant offsets and stores with
non-constant sizes.  Attached is a simple enhancement that
(finally) adds the ability to also detect overflow in allocated
objects to the warning.

With the patch GCC detects the overflow in code like this:

   char* f (void)
   {
     char s[] = "12345";
     char *p = malloc (strlen (s));
     strcpy (p, s);   // warning here
     return p;
   }

but not (yet) in something like this:

   char* g (const char *s)
   {
     char *p = malloc (strlen (s));
     strcpy (p, s);   // no warning (yet)
     return p;
   }

and quite a few other examples.  Doing better requires extending
the strlen pass.  I'm working on this extension and expect to
submit a patch before stage 1 ends.

Martin

PS I was originally planning to do all the allocation checking
in the strlen pass but it occurred to me that by also enhancing
the compute_objsize function, all warnings that use it will
benefit.  Besides -Wstringop-overflow this includes a subset
of -Warray-bounds, -Wformat-overflow, and -Wrestrict.  It's
nice when a small enhancement has such a broad positive effect.


Reply via email to