From 9411c6e7c769db09f10eacf2b0ac4eba5423ddbc Mon Sep 17 00:00:00 2001 From: Martin Roth Date: Wed, 30 Jul 2025 13:16:08 -0600 Subject: [PATCH] util/amdfwtool: Fix NULL pointer dereference in fill_dir_header Move the NULL pointer check to the beginning of the fill_dir_header function before any dereference of the directory pointer. This prevents the potential segmentation fault that could occur if directory is NULL. This fixes CID 1540835 - Dereference before null check (REVERSE_NULL). Change-Id: I12bb146d59839381478034f974b7d408f92ae677 Signed-off-by: Martin Roth Reviewed-on: https://review.coreboot.org/c/coreboot/+/88617 Reviewed-by: Patrick Rudolph Tested-by: build bot (Jenkins) --- util/amdfwtool/amdfwtool.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/util/amdfwtool/amdfwtool.c b/util/amdfwtool/amdfwtool.c index 2b70a0527b..d071a5be7c 100644 --- a/util/amdfwtool/amdfwtool.c +++ b/util/amdfwtool/amdfwtool.c @@ -586,6 +586,11 @@ static void copy_psp_header(void *bak, void *orig) static void fill_dir_header(void *directory, uint32_t count, context *ctx) { + if (ctx == NULL || directory == NULL) { + fprintf(stderr, "Calling %s with NULL pointers\n", __func__); + return; + } + psp_combo_directory *cdir = directory; psp_directory_table *dir = directory; bios_directory_table *bdir = directory; @@ -593,11 +598,6 @@ static void fill_dir_header(void *directory, uint32_t count, context *ctx) uint32_t cookie = ((psp_directory_table *)directory)->header.cookie; uint32_t table_size = 0; - if (ctx == NULL || directory == NULL) { - fprintf(stderr, "Calling %s with NULL pointers\n", __func__); - return; - } - /* The table size needs to be 0x1000 aligned. So align the end of table. */ adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);