From f1feb3ac04e2c96aa751fda8d36bb50c04ffa58d Mon Sep 17 00:00:00 2001 From: Peter Jones Date: Thu, 18 Feb 2021 10:36:23 +0100 Subject: sbat: drop the struct sbat and just use two variables instead The struct sbat isn't doing anything and only has two fields so let's pass pass those two to the functions directly instead of storing it in a struct. Signed-off-by: Peter Jones --- include/sbat.h | 7 +------ pe.c | 15 ++++++++------- sbat.c | 26 +++++++++++++++----------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/include/sbat.h b/include/sbat.h index c49c645e..c34ad319 100644 --- a/include/sbat.h +++ b/include/sbat.h @@ -15,12 +15,7 @@ struct sbat_entry { const CHAR8 *vendor_url; }; -struct sbat { - unsigned int size; - struct sbat_entry **entries; -}; - -EFI_STATUS parse_sbat(char *sbat_base, size_t sbat_size, struct sbat *sbat); +EFI_STATUS parse_sbat(char *sbat_base, size_t sbat_size, size_t *sbats, struct sbat_entry ***sbat); #endif /* !SBAT_H_ */ // vim:fenc=utf-8:tw=75:noet diff --git a/pe.c b/pe.c index 160c6f8a..0f4e1afd 100644 --- a/pe.c +++ b/pe.c @@ -1041,7 +1041,8 @@ handle_image (void *data, unsigned int datasize, if (secure_mode ()) { unsigned int i; EFI_STATUS efi_status; - struct sbat sbat = { 0 }; + size_t n; + struct sbat_entry **entries; struct sbat_entry *entry = NULL; if (SBATBase && SBATSize) { @@ -1057,7 +1058,7 @@ handle_image (void *data, unsigned int datasize, CopyMem(sbat_data, SBATBase, SBATSize); sbat_data[SBATSize] = '\0'; - efi_status = parse_sbat(sbat_data, sbat_size, &sbat); + efi_status = parse_sbat(sbat_data, sbat_size, &n, &entries); if (EFI_ERROR(efi_status)) { perror(L"SBAT data not correct: %r\n", efi_status); @@ -1065,8 +1066,8 @@ handle_image (void *data, unsigned int datasize, } dprint(L"SBAT data\n"); - for (i = 0; i < sbat.size; i++) { - entry = sbat.entries[i]; + for (i = 0; i < n; i++) { + entry = entries[i]; dprint(L"%a, %a, %a, %a, %a, %a\n", entry->component_name, entry->component_generation, @@ -1083,9 +1084,9 @@ handle_image (void *data, unsigned int datasize, efi_status = verify_buffer(data, datasize, &context, sha256hash, sha1hash); - if (sbat.entries) - for (i = 0; i < sbat.size; i++) - FreePool(sbat.entries[i]); + if (entries) + for (i = 0; i < n; i++) + FreePool(entries[i]); if (EFI_ERROR(efi_status)) { if (verbose) diff --git a/sbat.c b/sbat.c index e996a419..c29340cf 100644 --- a/sbat.c +++ b/sbat.c @@ -68,21 +68,24 @@ error: } EFI_STATUS -parse_sbat(char *sbat_base, size_t sbat_size, struct sbat *sbat) +parse_sbat(char *sbat_base, size_t sbat_size, size_t *sbats, struct sbat_entry ***sbat) { CHAR8 *current = (CHAR8 *) sbat_base; CHAR8 *end = (CHAR8 *) sbat_base + sbat_size - 1; EFI_STATUS efi_status = EFI_SUCCESS; struct sbat_entry *entry; - struct sbat_entry **entries; - unsigned int i; + struct sbat_entry **entries = NULL; + unsigned int i = 0; - if (!sbat_base || !sbat || sbat_size == 0) + if (!sbat_base || sbat_size == 0 || !sbats || !sbat) return EFI_INVALID_PARAMETER; if (current == end) return EFI_INVALID_PARAMETER; + *sbats = 0; + *sbat = 0; + do { entry = NULL; efi_status = parse_sbat_entry(¤t, end, &entry); @@ -96,24 +99,25 @@ parse_sbat(char *sbat_base, size_t sbat_size, struct sbat *sbat) if (entry) { entries = ReallocatePool( - sbat->entries, sbat->size * sizeof(entry), - (sbat->size + 1) * sizeof(entry)); + entries, i * sizeof(entry), + (i + 1) * sizeof(entry)); if (!entries) { efi_status = EFI_OUT_OF_RESOURCES; goto error; } - sbat->entries = entries; - sbat->entries[sbat->size] = entry; - sbat->size++; + entries[i++] = entry; } } while (entry && *current != '\0'); + *sbats = i; + *sbat = entries; + return efi_status; error: perror(L"Failed to parse SBAT data: %r\n", efi_status); - for (i = 0; i < sbat->size; i++) - FreePool(sbat->entries[i]); + while (i) + FreePool(entries[i--]); return efi_status; } -- cgit v1.2.3