From c323c2f63c9315bd705a1f8acf8f3abae06b5073 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 19 Jan 2022 13:28:32 +0100 Subject: [PATCH] boot: Also NUL-terminate for CHAR16 in file_reaad --- src/boot/efi/util.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index e023b97d2f0..362572cfadd 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -455,7 +455,7 @@ EFI_STATUS file_read(EFI_FILE *dir, const CHAR16 *name, UINTN off, UINTN size, C if (EFI_ERROR(err)) return err; - size = info->FileSize+1; + size = info->FileSize; } if (off > 0) { @@ -464,12 +464,16 @@ EFI_STATUS file_read(EFI_FILE *dir, const CHAR16 *name, UINTN off, UINTN size, C return err; } - buf = xallocate_pool(size + 1); + /* Allocate some extra bytes to guarantee the result is NUL-terminated for CHAR8 and CHAR16 strings. */ + UINTN extra = size % sizeof(CHAR16) + sizeof(CHAR16); + + buf = xallocate_pool(size + extra); err = handle->Read(handle, &size, buf); if (EFI_ERROR(err)) return err; - buf[size] = '\0'; + /* Note that handle->Read() changes size to reflect the actualy bytes read. */ + ZeroMem(buf + size, extra); *ret = TAKE_PTR(buf); if (ret_size) -- 2.47.3