public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/106927] New: false-positive -Werror=restrict warnings for memmove (calling memcpy) within array
@ 2022-09-13 12:41 gcc at boris dot fau.re
  2022-09-13 13:39 ` [Bug c/106927] " gcc at boris dot fau.re
  2022-09-13 13:52 ` [Bug tree-optimization/106927] false-positive -Werror=restrict warnings for memmove (calling memcpy) within string literal rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: gcc at boris dot fau.re @ 2022-09-13 12:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106927
           Summary: false-positive -Werror=restrict warnings for memmove
                    (calling memcpy) within array
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at boris dot fau.re
  Target Milestone: ---

When compiling the following code with -Werror=restrict, gcc complains:
$cat foo.c
#include <string.h>

int main (void)
{
    char *s = "abcdefghijklmnopqrstuvwxyz012345";
    size_t d = strlen(s);

    __builtin_memmove(s, s + 1, d - 1);

    return 0;
}
$ gcc -c -O2 -Werror=restrict -o foo.o foo.c
foo.c: In function 'main':
foo.c:8:5: error: '__builtin_memcpy' accessing 31 bytes at offsets 0 and 1
overlaps 30 bytes at offset 1 [-Werror=restrict]
    8 |     __builtin_memmove(s, s + 1, d - 1);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This was reproduced on gentoo with gcc-11.3.0, on RHEL9 with gcc-11.2.1, on
debian bullseye with gcc-12.2.0, on debian buster with gcc-9.5.0, RHEL8 with
gcc-8.5.0.

Please note that, on gcc-12.2.0, without -O2, the compilation succeeds.

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

* [Bug c/106927] false-positive -Werror=restrict warnings for memmove (calling memcpy) within array
  2022-09-13 12:41 [Bug c/106927] New: false-positive -Werror=restrict warnings for memmove (calling memcpy) within array gcc at boris dot fau.re
@ 2022-09-13 13:39 ` gcc at boris dot fau.re
  2022-09-13 13:52 ` [Bug tree-optimization/106927] false-positive -Werror=restrict warnings for memmove (calling memcpy) within string literal rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: gcc at boris dot fau.re @ 2022-09-13 13:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Boris Faure <gcc at boris dot fau.re> ---
The code is indeed incorrect.

It should be:
#include <string.h>

int main (void)
{
    char s[] = "abcdefghijklmnopqrstuvwxyz012345";
    size_t d = strlen(s);

    __builtin_memmove(s, s + 1, d - 1);

    return 0;
}

However, the error message is not helping.

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

* [Bug tree-optimization/106927] false-positive -Werror=restrict warnings for memmove (calling memcpy) within string literal
  2022-09-13 12:41 [Bug c/106927] New: false-positive -Werror=restrict warnings for memmove (calling memcpy) within array gcc at boris dot fau.re
  2022-09-13 13:39 ` [Bug c/106927] " gcc at boris dot fau.re
@ 2022-09-13 13:52 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-09-13 13:52 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |diagnostic
     Ever confirmed|0                           |1
                 CC|                            |msebor at gcc dot gnu.org
   Last reconfirmed|                            |2022-09-13
          Component|c                           |tree-optimization
            Summary|false-positive              |false-positive
                   |-Werror=restrict warnings   |-Werror=restrict warnings
                   |for memmove (calling        |for memmove (calling
                   |memcpy) within array        |memcpy) within string
                   |                            |literal

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
We issue the diagnostic for

__builtin_memcpy ("abcdefghijklmnopqrstuvwxyz012345", &MEM <char[33]> [(void
*)"abcdefghijklmnopqrstuvwxyz012345" + 1B], 31);

but we'd better issue a -Wwrite-strings diagnostic.

The actually emitted diagnostic is indeed somewhat misleading but the
reason is we simplify the memmove call to memcpy as seen above because
we have

      if (code == BUILT_IN_MEMMOVE)
        {
          /* Both DEST and SRC must be pointer types.
             ??? This is what old code did.  Is the testing for pointer types
             really mandatory?

             If either SRC is readonly or length is 1, we can use memcpy.  */
          if (!dest_align || !src_align)
            return false;
          if (readonly_data_expr (src)
              || (tree_fits_uhwi_p (len)
                  && (MIN (src_align, dest_align) / BITS_PER_UNIT
                      >= tree_to_uhwi (len))))
            {
              tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
...

that should probably suppress -Wrestrict on the call?

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

end of thread, other threads:[~2022-09-13 13:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-13 12:41 [Bug c/106927] New: false-positive -Werror=restrict warnings for memmove (calling memcpy) within array gcc at boris dot fau.re
2022-09-13 13:39 ` [Bug c/106927] " gcc at boris dot fau.re
2022-09-13 13:52 ` [Bug tree-optimization/106927] false-positive -Werror=restrict warnings for memmove (calling memcpy) within string literal rguenth 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).