[Bug c/94815] New: Abusive -Wrestrict warning with sprintf

vincent.riviere at freesbee dot fr gcc-bugzilla@gcc.gnu.org
Tue Apr 28 11:56:29 GMT 2020


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

            Bug ID: 94815
           Summary: Abusive -Wrestrict warning with sprintf
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent.riviere at freesbee dot fr
  Target Milestone: ---
            Target: m68k-elf

Testcase:

$ cat foo.c && m68k-elf-gcc -c foo.c -Wall -O
char *strcpy(char *, const char *);
int sprintf(char *, const char *, ...);

char* myalloc(int n);

void f(void)
{
    char* buf = myalloc(20);
    char* str1 = buf;
    char* str2 = buf + 10;

    strcpy(str2, "123");
    sprintf(str1, "ABC%s", str2);
}
foo.c: In function 'f':
foo.c:13:5: warning: 'sprintf' argument 3 may overlap destination object 'buf'
[-Wrestrict]
   13 |     sprintf(str1, "ABC%s", str2);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

This warning is a unexpected because:

1) strcpy() and sprintf() are declared without __restrict, but __restrict rules
are still actually used.

2) In this simple example, it is obvious that the buffer will not overflow.

This is annoying, because it prevents creating several logical buffers from a
single allocation.


More information about the Gcc-bugs mailing list