public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/104964] New: Wrong *** buffer overflow detected ***: terminated - acl
@ 2022-03-17 10:04 marxin at gcc dot gnu.org
  2022-03-17 10:04 ` [Bug tree-optimization/104964] " marxin at gcc dot gnu.org
                   ` (20 more replies)
  0 siblings, 21 replies; 22+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-03-17 10:04 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104964
           Summary: Wrong *** buffer overflow detected ***: terminated -
                    acl
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marxin at gcc dot gnu.org
  Target Milestone: ---

The test-case is reduced from acl:

$ cat x.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct __string_ext {
 char s_str[0];
};

struct string_obj_tag {
 struct __string_ext i;
};

typedef struct string_obj_tag string_obj;

static void 
writeto(char *text_p, ssize_t size)
{
  fprintf (stderr, "Write to: %p, size=%d\n", text_p, size);
  strncpy(text_p, "sparta", size);
}

int main()
{
  ssize_t size = 30;
  string_obj *string_obj_p = (string_obj *)malloc (sizeof(string_obj) + size);

  fprintf (stderr, "allocated: %d B starting at %p\n", size,
string_obj_p->i.s_str);
  writeto(string_obj_p->i.s_str, size);
  fprintf (stderr, "result STR(%p)=%s\n", string_obj_p->i.s_str,
string_obj_p->i.s_str);

  return 0;
}

$ gcc x.c -D_FORTIFY_SOURCE=2 -O2  && ./a.out
In file included from /usr/include/string.h:535,
                 from x.c:3:
In function ‘strncpy’,
    inlined from ‘writeto’ at x.c:19:3,
    inlined from ‘main’ at x.c:28:3:
/usr/include/bits/string_fortified.h:95:10: warning: ‘__builtin___strncpy_chk’
writing 30 bytes into a region of size 0 overflows the destination
[-Wstringop-overflow=]
   95 |   return __builtin___strncpy_chk (__dest, __src, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   96 |                                   __glibc_objsize (__dest));
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~
allocated: 30 B starting at 0x4052a0
Write to: 0x4052a0, size=30
*** buffer overflow detected ***: terminated
Aborted (core dumped)

While clang is fine:

$ clang x.c -D_FORTIFY_SOURCE=2 -O2  && ./a.out
allocated: 30 B starting at 0x4052a0
Write to: 0x4052a0, size=30
result STR(0x4052a0)=sparta

and ASAN,UBSAN as well:

$ gcc-11 x.c -fsanitize=address,undefined  && ./a.out
allocated: 30 B starting at 0x603000000040
Write to: 0x603000000040, size=30
result STR(0x603000000040)=sparta

I see the error happens also with older GCC compilers.

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

end of thread, other threads:[~2022-05-24 18:52 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-17 10:04 [Bug tree-optimization/104964] New: Wrong *** buffer overflow detected ***: terminated - acl marxin at gcc dot gnu.org
2022-03-17 10:04 ` [Bug tree-optimization/104964] " marxin at gcc dot gnu.org
2022-03-17 10:07 ` marxin at gcc dot gnu.org
2022-03-17 10:09 ` marxin at gcc dot gnu.org
2022-03-17 11:16 ` marxin at gcc dot gnu.org
2022-03-17 11:25 ` marxin at gcc dot gnu.org
2022-03-17 11:34 ` siddhesh at gcc dot gnu.org
2022-03-22  8:20 ` marxin at gcc dot gnu.org
2022-03-24 13:32 ` marxin at gcc dot gnu.org
2022-03-24 13:32 ` marxin at gcc dot gnu.org
2022-03-24 13:34 ` marxin at gcc dot gnu.org
2022-03-25  7:37 ` siddhesh at gcc dot gnu.org
2022-03-25  7:38 ` siddhesh at gcc dot gnu.org
2022-03-25 13:53 ` marxin at gcc dot gnu.org
2022-03-25 14:22 ` jakub at gcc dot gnu.org
2022-03-25 15:00 ` siddhesh at gcc dot gnu.org
2022-03-25 15:02 ` jakub at gcc dot gnu.org
2022-03-28  6:33 ` siddhesh at gcc dot gnu.org
2022-05-23 22:53 ` sam at gentoo dot org
2022-05-24  7:08 ` marxin at gcc dot gnu.org
2022-05-24 17:00 ` sam at gentoo dot org
2022-05-24 18:52 ` marxin 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).