This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] relax -Wsizeof-pointer-memaccess for strncpy with size of source (PR 85931)
- From: Jeff Law <law at redhat dot com>
- To: Martin Sebor <msebor at gmail dot com>, Gcc Patch List <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 11 Jun 2018 15:57:29 -0600
- Subject: Re: [PATCH] relax -Wsizeof-pointer-memaccess for strncpy with size of source (PR 85931)
- References: <da9a3582-33e1-6f0a-cb91-36b0e699806d@gmail.com>
On 05/29/2018 08:57 PM, Martin Sebor wrote:
> Warning for a strncpy call whose bound is the same as the size
> of the source and suggesting to use the size of the source is
> less than helpful when both sizes are the same, as in:
>
> char a[4], b[4];
> strncpy (a, b, sizeof b);
>
> The attached patch suppresses the -Wsizeof-pointer-memaccess
> warning for these cases. To do that even for VLAs (in some
> cases), the patch enhances operand_equal_p() to handle
> SAVE_EXPR to detect when VLA in sizeof VLA refers to the size
> of a variable-length array.
>
> Is this okay for trunk and GCC 8?
>
> Martin
>
> gcc-85931.diff
>
>
> PR c/85931 - -Wsizeof-pointer-memaccess for strncpy with size of source
>
> gcc/c-family/ChangeLog:
>
> PR c/85931
> * c-warn.c (sizeof_pointer_memaccess_warning): Avoid warning when
> sizeof source and destination yields the same value.
>
> gcc/ChangeLog:
>
> PR c/85931
> * fold-const.c (operand_equal_p): Handle SAVE_EXPR.
>
> gcc/testsuite/ChangeLog:
>
> PR c/85931
> * gcc.dg/Wstringop-truncation-3.c: New test.
OK for the trunk. Richi and Jakub have the final say for the branch.
I'm a bit surprised that you don't just use operand_equal_p for both the
INTEGER_CST and !INTEGER_CST cases when testing dstsz == srcsz
Jeff