[llvm-bugs] [Bug 49174] New: c/c++ invalid -fsanitize=undefined crash with "offsetof" like code

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Feb 13 20:03:04 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=49174

            Bug ID: 49174
           Summary: c/c++ invalid -fsanitize=undefined crash with
                    "offsetof" like code
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ryan_greenblatt at brown.edu
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

```
typedef struct type {
    int member;
} type_t;

int main()
{
    (char *)&((type_t *)(0))->member;
}
```


This crashes with:
```
temp.c:7:31: runtime error: member access within null pointer of type 'type_t'
(aka 'struct type')
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior temp.c:7:31 in
```

https://gcc.godbolt.org/z/dbWc6h

Note that the crash still occurs when using `((size_t)((char *)&((type_t
*)0)->member - (char *)0))`. See https://en.wikipedia.org/wiki/Offsetof for an
explanation of what is going on here.

The crash doesn't occur with `__builtin_offsetof`, so that is a viable work
around.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210214/5f8ad7b2/attachment.html>


More information about the llvm-bugs mailing list