Bug 93519 - [9 Regression] bogus -Wrestrict for strcpy(d, s) call guarded by d != s
Summary: [9 Regression] bogus -Wrestrict for strcpy(d, s) call guarded by d != s
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 10.0
: P2 normal
Target Milestone: 10.0
Assignee: Richard Biener
URL: https://bugzilla.redhat.com/show_bug....
Keywords: diagnostic, patch
Depends on:
Blocks: Wrestrict
  Show dependency treegraph
Reported: 2020-01-31 00:45 UTC by Martin Sebor
Modified: 2022-05-27 08:41 UTC (History)
2 users (show)

See Also:
Known to work: 10.0
Known to fail:
Last reconfirmed: 2020-01-31 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2020-01-31 00:45:00 UTC
This was reported in RHBZ #1796688 (and previously for memcpy in bug 83456).  The warning is obviously incorrect here:

#include <string.h>

static char *
str_numth(char *dest, char *num, int type)
        if (dest != num)
                strcpy(dest, num);
        strcat(dest, "foo");
        return dest;

DCH_to_char(char *in, char *out, int collid)
        char       *s  = out;

        str_numth(s, s, 42);

$ gcc -Wall -c -O2 wtest.c
wtest.c: In function 'DCH_to_char':
wtest.c:7:3: warning: 'strcpy' source argument is the same as destination [-Wrestrict]
    7 |   strcpy(dest, num);
      |   ^~~~~~~~~~~~~~~~~
Comment 1 Jakub Jelinek 2020-01-31 12:10:31 UTC
Started with r8-5363-gcc8bea0aeeeeb6ed8046ede1a577ee681da2ca6a

I'd say the bug is in trying to do this from whenever we try to fold such a call, because there is no way to cleanup the cfg after inlining before it happens.
So, it would be much better to do this only in specific well chosen passes, say one among early opts, but sufficiently after einline so that unreachable code there could be optimized away, and once or twice in late opts.
The overlapping arguments is UB, so I don't really think it is neccessary to fold it immediately.

Another option is to turn it into IFN_WARNING and defer the diagnostics after some cleanups.
Comment 2 Martin Sebor 2020-02-04 00:44:54 UTC
Patch: https://gcc.gnu.org/ml/gcc-patches/2020-02/msg00130.html
Comment 3 Richard Biener 2020-02-07 11:58:27 UTC
Fixed on trunk sofar.
Comment 4 CVS Commits 2020-02-07 11:58:33 UTC
The master branch has been updated by Richard Guenther <rguenth@gcc.gnu.org>:


commit r10-6505-g3c7a03bc360c3511fae3747a71e579e9fd0824f9
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Feb 7 12:56:18 2020 +0100

    middle-end/93519 - avoid folding stmts in obviously unreachable code
    The inliner folds stmts delayed, the following arranges things so
    to not fold stmts that are obviously not reachable to avoid warnings
    from those code regions.
    2020-02-07  Richard Biener  <rguenther@suse.de>
    	PR middle-end/93519
    	* tree-inline.c (fold_marked_statements): Do a PRE walk,
    	skipping unreachable regions.
    	(optimize_inline_calls): Skip folding stmts when we didn't
    	* gcc.dg/Wrestrict-21.c: New testcase.
Comment 5 Jakub Jelinek 2020-03-04 09:37:01 UTC
GCC 8.4.0 has been released, adjusting target milestone.
Comment 6 Jakub Jelinek 2021-05-14 09:52:40 UTC
GCC 8 branch is being closed.
Comment 7 Richard Biener 2021-06-01 08:16:02 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Comment 8 Richard Biener 2022-05-27 08:41:35 UTC
Fixed in GCC 10.