[Bug c/83688] New: Please check if buffers may overlap when copying strings

bugzilla@poradnik-webmastera.com gcc-bugzilla@gcc.gnu.org
Thu Jan 4 15:44:00 GMT 2018


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

            Bug ID: 83688
           Summary: Please check if buffers may overlap when copying
                    strings
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzilla@poradnik-webmastera.com
  Target Milestone: ---

Functions like strcpy internally use memcpy to copy data. This may cause
problems when someone will try to use them to move string in buffer, e.g. to
strip prefix. gcc is able to detect if overlapping buffers are used with
memcpy. Please add similar diagnostics to strcpy/sprintf functions too.

[code]
#include <string.h>
#include <stdio.h>

char buf[20];

void test()
{
    strcpy(buf, buf+5);
    memcpy(buf, buf+5, strlen(buf+5)+1);

    snprintf(buf, sizeof(buf), "%s", buf+5);

    memcpy(buf, buf+5, 10);
}
[/code]

[out]
$ g++ -c -o test.o test.cc -O3 -Wall -Wextra -Wformat-overflow
-Wformat-truncation -Wstringop-overflow=2 -Wstringop-truncation
test.cc: In function ‘void test()’:
test.cc:13:11: warning: ‘void* memcpy(void*, const void*, size_t)’ accessing 10
bytes at offsets 0 and 5 overlaps 5 bytes at offset 5 [-Wrestrict]
     memcpy(buf, buf+5, 10);
     ~~~~~~^~~~~~~~~~~~~~~~

$ g++ --version
g++ (GCC) 8.0.0 20171231 (experimental)
[/out]


More information about the Gcc-bugs mailing list