[Bug c/83671] New: Fix for false positive reported by -Wstringop-overflow does not work with inlining

bugzilla@poradnik-webmastera.com gcc-bugzilla@gcc.gnu.org
Wed Jan 3 17:43:00 GMT 2018


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

            Bug ID: 83671
           Summary: Fix for false positive reported by -Wstringop-overflow
                    does not work with inlining
           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: ---

Fix for bug 83373 does not work well with inlining:

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

char dest[20];
char src[10];

__attribute((nonnull(1, 2)))
inline char* my_strcpy(char* __restrict__ dst, const char* __restrict__ src,
size_t size)
{
    size_t len = strlen(src);
    if (len < size)
        memcpy(dst, src, len + 1);
    else
    {
        memcpy(dst, src, size - 1);
        dst[size - 1] = '\0';
    }
    return dst;
}

inline void func1()
{
    my_strcpy(dest, src, sizeof(dest));
}

void func2()
{
    func1();
}
[/code]

[out]
$ g++ -c -o test.o test.cc -Wall -Wstringop-overflow=2 -O1
In function ‘char* my_strcpy(char*, const char*, size_t)’,
    inlined from ‘void func2()’ at test.cc:23:14:
test.cc:15:15: warning: ‘void* memcpy(void*, const void*, size_t)’ forming
offset [11, 19] is out of the bounds [0, 10] of object ‘src’ with type ‘char
[10]’ [-Warray-bounds]
         memcpy(dst, src, size - 1);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~
test.cc: In function ‘void func2()’:
test.cc:5:6: note: ‘src’ declared here
 char src[10];
      ^~~
In function ‘char* my_strcpy(char*, const char*, size_t)’,
    inlined from ‘void func2()’ at test.cc:23:14:
test.cc:15:15: warning: ‘void* memcpy(void*, const void*, size_t)’ reading 19
bytes from a region of size 10 [-Wstringop-overflow=]
         memcpy(dst, src, size - 1);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~

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


More information about the Gcc-bugs mailing list