Commit 0e6a044d by Nicolas Capens Committed by Nicolas Capens

Fix undefined behavior in OFFSET().

Accessing members of a null pointer is undefined behavior, even when only used to obtain the address again. So use a non-zero value as the base pointer address instead. 32 was chosen to provide sufficient alignment guarantees. Bug b/119823623 Change-Id: Ia6d24dd6c2740261948860c45eb35cc489a3a827 Reviewed-on: https://swiftshader-review.googlesource.com/c/22788Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com>
parent 46988abe
...@@ -151,7 +151,10 @@ namespace sw ...@@ -151,7 +151,10 @@ namespace sw
return v; return v;
} }
#define OFFSET(s,m) (int)(size_t)&reinterpret_cast<const volatile char&>((((s*)0)->m)) // The OFFSET macro is a generalization of the offsetof() macro defined in <cstddef>.
// It allows e.g. getting the offset of array elements, even when indexed dynamically.
// We cast the address '32' and subtract it again, because null-dereference is undefined behavior.
#define OFFSET(s,m) ((int)(size_t)&reinterpret_cast<const volatile char&>((((s*)32)->m)) - 32)
} }
#endif // sw_Types_hpp #endif // sw_Types_hpp
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment