public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "siddhesh at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/104964] Wrong *** buffer overflow detected ***: terminated - acl
Date: Fri, 25 Mar 2022 07:37:00 +0000	[thread overview]
Message-ID: <bug-104964-4-4briapH7Hk@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104964-4@http.gcc.gnu.org/bugzilla/>

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

Siddhesh Poyarekar <siddhesh at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED

--- Comment #10 from Siddhesh Poyarekar <siddhesh at gcc dot gnu.org> ---
OK, I have a representative reproducer, which TBH is not too different from the
one you posted, just that it succeeds with __builtin_object_size and fails with
__builtin_dynamic_object_size:


struct __string_ext
{
  char s_str[0];
};

typedef struct
{
  int o_prefix;
  struct __string_ext i;
} string_obj;

#define SUFFIX ".suffix"

string_obj *
__acl_to_any_text (unsigned long n)
{
  unsigned long off = 0;
  unsigned long size = sizeof SUFFIX;

  string_obj *obj = __builtin_malloc (sizeof (string_obj) + size);

  if (n == 0)
          __builtin_unreachable ();

  while (n-- != 0)
    {
      if (off + 1 > size - sizeof SUFFIX)
        {
          size <<= 1;
          string_obj *tmp = __builtin_realloc (obj, sizeof (string_obj) +
                                               size);
          if (!tmp)
            __builtin_unreachable ();
          obj = tmp;
        }
      obj->i.s_str[off++] = 'A';
    }

  char *t = obj->i.s_str + off;
  __strcpy_chk (t, SUFFIX, __builtin_dynamic_object_size (t, 1));

  return obj;
}

int
main ()
{
  string_obj *s = __acl_to_any_text (32);

  __builtin_printf ("%zu: %s\n", __builtin_strlen (s->i.s_str), s->i.s_str);
  return 0;
}


$ gcc/cc1 -g -o test.s -quiet -Wall -O3 fs3.c
fs3.c: In function ‘__acl_to_any_text’:
fs3.c:40:3: warning: ‘__builtin___memcpy_chk’ writing 8 bytes into a region of
size 0 overflows the destination [-Wstringop-overflow=]
   40 |   __strcpy_chk (t, SUFFIX, __builtin_dynamic_object_size (t, 1));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


The only reason why __builtin_object_size fails is because of the non-constant
OFF.  If that is removed, __builtin_object_size also returns the declared size
of s_str, i.e. 0.  The check for a traditionally declared trailing array ()i.e.
a[0] or  a[1]) seems to be broken for nested structs like the above.  Change
that to s_str[] (the struct then needs another member above) and it works fine.

  parent reply	other threads:[~2022-03-25  7:37 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17 10:04 [Bug tree-optimization/104964] New: " 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104964-4-4briapH7Hk@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).