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: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected],
                    [email protected]

```
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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to