From 498cb29dfdcc7232f7b0693e266555c19c43ae20 Mon Sep 17 00:00:00 2001 From: "Marvin.Haeuser@outlook.com" Date: Thu, 1 Nov 2018 04:08:35 +0800 Subject: [PATCH] MdePkg/Base.h: Implement BASE_CR() via OFFSET_OF(). Replace the current NULL pointer dereference to retrieve Field's offset with a call to OFFSET_OF(). This is implemented via __builtin_offsetof for GCC and Clang, which eliminates UB caught by Clang UndefinedBehaviorSanitizer. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Marvin Haeuser Reviewed-by: Liming Gao --- MdePkg/Include/Base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MdePkg/Include/Base.h b/MdePkg/Include/Base.h index 523192fd79..bc877d8125 100644 --- a/MdePkg/Include/Base.h +++ b/MdePkg/Include/Base.h @@ -869,7 +869,7 @@ typedef UINTN *BASE_LIST; @return A pointer to the structure from one of it's elements. **/ -#define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - (CHAR8 *) &(((TYPE *) 0)->Field))) +#define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - OFFSET_OF (TYPE, Field))) /** Rounds a value up to the next boundary using a specified alignment.