From b2cf9922a4cdf8a649c1cc91ba6a3436987d26fa Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 11 Aug 2021 14:59:46 +0200 Subject: [PATCH] sd-boot: Don't use magic integer constants --- src/boot/efi/util.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index c6b78154ce3..b048df0764a 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -101,7 +101,7 @@ EFI_STATUS efivar_set(const EFI_GUID *vendor, const CHAR16 *name, const CHAR16 * EFI_STATUS efivar_set_uint_string(const EFI_GUID *vendor, const CHAR16 *name, UINTN i, UINT32 flags) { CHAR16 str[32]; - SPrint(str, 32, L"%u", i); + SPrint(str, ELEMENTSOF(str), L"%u", i); return efivar_set(vendor, name, str, flags); } @@ -132,35 +132,35 @@ EFI_STATUS efivar_set_uint64_le(const EFI_GUID *vendor, const CHAR16 *name, UINT } EFI_STATUS efivar_get(const EFI_GUID *vendor, const CHAR16 *name, CHAR16 **value) { - _cleanup_freepool_ CHAR8 *buf = NULL; + _cleanup_freepool_ CHAR16 *buf = NULL; EFI_STATUS err; CHAR16 *val; UINTN size; - err = efivar_get_raw(vendor, name, &buf, &size); + err = efivar_get_raw(vendor, name, (CHAR8**)&buf, &size); if (EFI_ERROR(err)) return err; /* Make sure there are no incomplete characters in the buffer */ - if ((size % 2) != 0) + if ((size % sizeof(CHAR16)) != 0) return EFI_INVALID_PARAMETER; if (!value) return EFI_SUCCESS; /* Return buffer directly if it happens to be NUL terminated already */ - if (size >= 2 && buf[size-2] == 0 && buf[size-1] == 0) { - *value = (CHAR16*) TAKE_PTR(buf); + if (size >= sizeof(CHAR16) && buf[size/sizeof(CHAR16)] == 0) { + *value = TAKE_PTR(buf); return EFI_SUCCESS; } /* Make sure a terminating NUL is available at the end */ - val = AllocatePool(size + 2); + val = AllocatePool(size + sizeof(CHAR16)); if (!val) return EFI_OUT_OF_RESOURCES; CopyMem(val, buf, size); - val[size/2] = 0; /* NUL terminate */ + val[size / sizeof(CHAR16)] = 0; /* NUL terminate */ *value = val; return EFI_SUCCESS; @@ -255,7 +255,7 @@ VOID efivar_set_time_usec(const EFI_GUID *vendor, const CHAR16 *name, UINT64 use if (usec == 0) return; - SPrint(str, 32, L"%ld", usec); + SPrint(str, ELEMENTSOF(str), L"%ld", usec); efivar_set(vendor, name, str, 0); }