]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Also NUL-terminate for CHAR16 in file_reaad
authorJan Janssen <medhefgo@web.de>
Wed, 19 Jan 2022 12:28:32 +0000 (13:28 +0100)
committerJan Janssen <medhefgo@web.de>
Wed, 26 Jan 2022 17:07:00 +0000 (18:07 +0100)
src/boot/efi/util.c

index e023b97d2f0915b436b83d5a1f0f688a606afda0..362572cfadd067c958bd409606f8f3689a9ae40e 100644 (file)
@@ -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)