<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - c/c++ invalid -fsanitize=undefined crash with "offsetof" like code"
   href="https://bugs.llvm.org/show_bug.cgi?id=49174">49174</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>c/c++ invalid -fsanitize=undefined crash with "offsetof" like code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ryan_greenblatt@brown.edu
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>```
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
```

<a href="https://gcc.godbolt.org/z/dbWc6h">https://gcc.godbolt.org/z/dbWc6h</a>

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

The crash doesn't occur with `__builtin_offsetof`, so that is a viable work
around.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>