[libunwind] [libunwind] Undefined behaviour pointer arithmetic with null pointer (PR #98648)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 12 08:22:22 PDT 2024


================
@@ -230,8 +230,13 @@ void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
 }
 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
 
-
-#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
+template <typename TYPE, typename FIELD>
+__attribute__((no_sanitize("undefined"))) static inline size_t
+_arrayoffsetof(int index, FIELD TYPE::*field) {
+  return ((size_t)(&(((TYPE *)0)[index].*field)));
+}
+#define arrayoffsetof(type, index, field)                                      \
+  _arrayoffsetof<struct type>(index, &type::field)
----------------
nikic wrote:

You can replace this with something like `sizeof(type) * index + offsetof(type, field)`, which is safe.

https://github.com/llvm/llvm-project/pull/98648


More information about the cfe-commits mailing list