]> granicus.if.org Git - clang/commit
[Sema] Fix PR30346: relax __builtin_object_size checks.
authorGeorge Burgess IV <george.burgess.iv@gmail.com>
Mon, 12 Sep 2016 23:50:35 +0000 (23:50 +0000)
committerGeorge Burgess IV <george.burgess.iv@gmail.com>
Mon, 12 Sep 2016 23:50:35 +0000 (23:50 +0000)
commit6603cadc46b1aed3f61fdebdd41c328b28fe6f92
tree5d71730d8885d50a2ae9e4676504d835bc35cf98
parentfd2a481cd92f3a613baa0f218ac680115590ff6a
[Sema] Fix PR30346: relax __builtin_object_size checks.

This patch makes us act more conservatively when trying to determine
the objectsize for an array at the end of an object. This is in
response to code like the following:

```
struct sockaddr {
  /* snip */
  char sa_data[14];
};

void foo(const char *s) {
  size_t slen = strlen(s) + 1;
  size_t added_len = slen <= 14 ? 0 : slen - 14;
  struct sockaddr *sa = malloc(sizeof(struct sockaddr) + added_len);
  strcpy(sa->sa_data, s);
  // ...
}
```

`__builtin_object_size(sa->sa_data, 1)` would return 14, when there
could be more than 14 bytes at `sa->sa_data`.

Code like this is apparently not uncommon. FreeBSD's manual even
explicitly mentions this pattern:
https://www.freebsd.org/doc/en/books/developers-handbook/sockets-essential-functions.html
(section 7.5.1.1.2).

In light of this, we now just give up on any array at the end of an
object if we can't find the object's initial allocation.

I lack numbers for how much more conservative we actually become as a
result of this change, so I chose the fix that would make us as
compatible with GCC as possible. If we want to be more aggressive, I'm
happy to consider some kind of whitelist or something instead.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@281277 91177308-0d34-0410-b5e6-96231b3b80d8
lib/AST/ExprConstant.cpp
test/CodeGen/object-size.c
test/CodeGen/pass-object-size.c