efi: beef up efivar_get_xyz() to accept NULL return values
authorLennart Poettering <lennart@poettering.net>
Fri, 8 Feb 2019 15:23:40 +0000 (16:23 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 1 Mar 2019 11:41:32 +0000 (12:41 +0100)
src/boot/efi/util.c
src/boot/efi/util.h

index f1f1674..bc1d4ae 100644 (file)
@@ -117,6 +117,9 @@ EFI_STATUS efivar_get(const CHAR16 *name, CHAR16 **value) {
         if ((size % 2) != 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*) buf;
@@ -141,7 +144,7 @@ EFI_STATUS efivar_get_int(const CHAR16 *name, UINTN *i) {
         EFI_STATUS err;
 
         err = efivar_get(name, &val);
-        if (!EFI_ERROR(err))
+        if (!EFI_ERROR(err) && i)
                 *i = Atoi(val);
 
         return err;
@@ -159,8 +162,10 @@ EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const CHAR16 *name, CHAR8 **bu
 
         err = uefi_call_wrapper(RT->GetVariable, 5, (CHAR16*) name, (EFI_GUID *)vendor, NULL, &l, buf);
         if (!EFI_ERROR(err)) {
-                *buffer = buf;
-                buf = NULL;
+
+                if (buffer)
+                        *buffer = TAKE_PTR(buf);
+
                 if (size)
                         *size = l;
         }
index 4ba7050..8c5e35a 100644 (file)
@@ -55,3 +55,10 @@ const EFI_GUID loader_guid;
 
 #define UINTN_MAX (~(UINTN)0)
 #define INTN_MAX ((INTN)(UINTN_MAX>>1))
+
+#define TAKE_PTR(ptr)                           \
+        ({                                      \
+                typeof(ptr) _ptr_ = (ptr);      \
+                (ptr) = NULL;                   \
+                _ptr_;                          \
+        })