From d3b3ee09a675cca1617a21a4f967457f92ebe3a6 Mon Sep 17 00:00:00 2001 From: Laszlo Ersek Date: Fri, 12 Apr 2019 15:10:09 +0200 Subject: [PATCH] BaseTools/PiFirmwareFile: fix undefined behavior in SECTION_SIZE Sync SECTION_SIZE() from MdePkg to BaseTools, from an earlier patch in this series. Cc: Bob Feng Cc: Liming Gao Cc: Yonghong Zhu Bugzilla: https://bugzilla.tianocore.org/show_bug.cgi?id=1710 Signed-off-by: Laszlo Ersek Reviewed-by: Philippe Mathieu-Daude Reviewed-by: Liming Gao Reviewed-by: Bob Feng Reviewed-by: Michael D Kinney Reviewed-by: Jordan Justen --- BaseTools/Source/C/Include/Common/PiFirmwareFile.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/BaseTools/Source/C/Include/Common/PiFirmwareFile.h b/BaseTools/Source/C/Include/Common/PiFirmwareFile.h index 5bc871df48..7d8acb669b 100644 --- a/BaseTools/Source/C/Include/Common/PiFirmwareFile.h +++ b/BaseTools/Source/C/Include/Common/PiFirmwareFile.h @@ -300,8 +300,15 @@ typedef struct { CHAR16 VersionString[1]; } EFI_VERSION_SECTION2; -#define SECTION_SIZE(SectionHeaderPtr) \ - ((UINT32) (*((UINT32 *) ((EFI_COMMON_SECTION_HEADER *) SectionHeaderPtr)->Size) & 0x00ffffff)) +// +// The argument passed as the SectionHeaderPtr parameter to the SECTION_SIZE() +// function-like macro below must not have side effects: SectionHeaderPtr is +// evaluated multiple times. +// +#define SECTION_SIZE(SectionHeaderPtr) ((UINT32) ( \ + (((EFI_COMMON_SECTION_HEADER *) (SectionHeaderPtr))->Size[0] ) | \ + (((EFI_COMMON_SECTION_HEADER *) (SectionHeaderPtr))->Size[1] << 8) | \ + (((EFI_COMMON_SECTION_HEADER *) (SectionHeaderPtr))->Size[2] << 16))) #pragma pack() -- 2.39.2