Mailing List Archive

[xen master] xen/efi: Rewrite DOS/PE magic checking without memcmp()
commit 4cd66fb56dc6972178f6543217b66bc122fbbd5a
Author: Andrew Cooper <andrew.cooper3@citrix.com>
AuthorDate: Tue Apr 16 16:21:34 2024 +0100
Commit: Andrew Cooper <andrew.cooper3@citrix.com>
CommitDate: Thu Apr 18 20:43:11 2024 +0100

xen/efi: Rewrite DOS/PE magic checking without memcmp()

Misra Rule 21.16 doesn't like the use of memcmp() against character arrays (a
string literal in this case). This is a rare piece of logic where we're
looking for a magic marker that just happens to make sense when expressed as
ASCII. Rewrite using plain compares.

No functional change.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
Acked-by: Roger Pau Monné <roger.pau@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
---
xen/common/efi/pe.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/xen/common/efi/pe.c b/xen/common/efi/pe.c
index a84992df9a..ef8a2543e0 100644
--- a/xen/common/efi/pe.c
+++ b/xen/common/efi/pe.c
@@ -111,7 +111,8 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,
UINTN offset, i;

if ( image_size < sizeof(*dos) ||
- memcmp(dos->Magic, "MZ", 2) != 0 )
+ dos->Magic[0] != 'M' ||
+ dos->Magic[1] != 'Z' )
return NULL;

offset = dos->ExeHeader;
@@ -119,7 +120,10 @@ const void *__init pe_find_section(const void *image, const UINTN image_size,

offset += sizeof(*pe);
if ( image_size < offset ||
- memcmp(pe->Magic, "PE\0\0", 4) != 0 )
+ pe->Magic[0] != 'P' ||
+ pe->Magic[1] != 'E' ||
+ pe->Magic[2] != '\0' ||
+ pe->Magic[3] != '\0' )
return NULL;

if ( pe->FileHeader.Machine != PE_HEADER_MACHINE )
--
generated by git-patchbot for /home/xen/git/xen.git#master