public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/102386] New: [12 regression] bogus -Wrestrict for unreachable memcpy()
@ 2021-09-17 10:43 dimhen at gmail dot com
  2021-09-18 22:48 ` [Bug middle-end/102386] " msebor at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: dimhen at gmail dot com @ 2021-09-17 10:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102386

            Bug ID: 102386
           Summary: [12 regression] bogus -Wrestrict for unreachable
                    memcpy()
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dimhen at gmail dot com
  Target Milestone: ---

r12-3456 PASS
r12-3545 FAIL

$ cat x.i
static int GetHashParam_HP_OPAQUEBLOB_pdwDataLen = 0;
void *memcpy(void *, const void *, unsigned long);
typedef struct {
  int tSimpleBlobHeader;
  int bCurrKey[];
} CRYPT_OPAQUEHASHBLOB;
int GetHashParam_HP_OPAQUEBLOB() {
  CRYPT_OPAQUEHASHBLOB *pBlb;
  if (GetHashParam_HP_OPAQUEBLOB_pdwDataLen) {
    void *pcn = pBlb->bCurrKey, *pdwBuff = pBlb->bCurrKey;
    int dwDWBufLen = 8;
    memcpy(pcn, pdwBuff, sizeof(1) * dwDWBufLen);
  }
  return 1;
}

$ gcc -fpreprocessed -O --param max-early-inliner-iterations=2 -Wrestrict -c
x.i
x.i: In function 'GetHashParam_HP_OPAQUEBLOB':
x.i:12:5: warning: 'memcpy' accessing 32 bytes at offsets 4 and 4 overlaps 32
bytes at offset 4 [-Wrestrict]
   12 |     memcpy(pcn, pdwBuff, sizeof(1) * dwDWBufLen);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Test extracted from private codebase.
Key part:

static inline void foo(void *p, void *q, int N)
{
   if(p != q)
     memcpy(p, q, N);
}

void bar(void q, void p, int N)
{
   foo(p, q, N);
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug middle-end/102386] [12 regression] bogus -Wrestrict for unreachable memcpy()
  2021-09-17 10:43 [Bug middle-end/102386] New: [12 regression] bogus -Wrestrict for unreachable memcpy() dimhen at gmail dot com
@ 2021-09-18 22:48 ` msebor at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-09-18 22:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102386

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID
                 CC|                            |msebor at gcc dot gnu.org
           Keywords|                            |diagnostic
             Blocks|                            |84774

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning is correct (at -O0 GCC issues a -Wuninitialized for dereferencing
the uninitialized pBlb), but I suspect you're expecting it to be suppressed
because the memcpy call is eliminated.  I've simplified the test case and
changed the names in it to make it easier to see what I assume you're really
pointing out:

$ cat pr102386.c && gcc -O2 -S  -Wall pr102386.c
struct S { int n, a[8]; };

static int n = 0;                   // not const but not modified

void f (struct S *p)
{
  if (n)                            // folded to false by ccp2
    {
      void *q = p->a, *r = p->a;
      int n = 8;
      __builtin_memcpy (q, r, n);   // -Wrestrict
    }
}
pr102386.c: In function ‘f’:
pr102386.c:11:7: warning: ‘__builtin_memcpy’ accessing 8 bytes at offsets 4 and
4 overlaps 8 bytes at offset 4 [-Wrestrict]
   11 |       __builtin_memcpy (q, r, n);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~

Although the memcpy call does ultimately end up removed (by ccp2) as
unreachable because the static global variable N is never set in the file, the
warning is issued before that happens, during folding.  This might change
if/when warnings are deferred until the end of compilation but I don't see this
use case as compelling enough to consider this particular instance a bug.  Feel
free to reopen it if you have a more compelling use case.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84774
[Bug 84774] [meta-bug] bogus/missing -Wrestrict

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-18 22:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-17 10:43 [Bug middle-end/102386] New: [12 regression] bogus -Wrestrict for unreachable memcpy() dimhen at gmail dot com
2021-09-18 22:48 ` [Bug middle-end/102386] " msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).