Skip to content

Commit dc64886

Browse files
committed
Introduce a SafeFreePool() that doesn't assert on NULL
* On release/consumer hardware, FreePool(NULL) is valid and does nothing, but on DEBUG builds, it may trigger an assert. * We therefore introduce SafeFreePool() to take care of these scenarios. * Closes #5.
1 parent 597535f commit dc64886

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

include/uefi-driver/uefi_support.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* uefi_support.h - UEFI support declarations */
22
/*
3-
* Copyright © 2014-2021 Pete Batard <pete@akeo.ie>
3+
* Copyright © 2014-2023 Pete Batard <pete@akeo.ie>
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -137,6 +137,12 @@ static __inline UINTN _SafeStrSize(CONST CHAR16* String, CONST CHAR8* File,
137137

138138
#define SafeStrSize(s) _SafeStrSize(s, __FILE__, __LINE__)
139139

140+
/*
141+
* Secure FreePool that doesn't assert on NULL and sets the pointer
142+
* passed as parameter to NULL after freeing it.
143+
*/
144+
#define SafeFreePool(p) if (p != NULL) { FreePool(p), p = NULL; }
145+
140146
/*
141147
* EDK2 does not provide a StrDup call, so we define one.
142148
*/

uefi-driver/uefi_bridge.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* uefi_bridge.c - libntfs-3g interface for UEFI */
22
/*
3-
* Copyright © 2021 Pete Batard <pete@akeo.ie>
3+
* Copyright © 2021-2023 Pete Batard <pete@akeo.ie>
44
*
55
* Parts taken from lowntfs-3g.c:
66
* Copyright © 2005-2007 Yura Pakhuchiy
@@ -629,7 +629,7 @@ NtfsFreeFile(EFI_NTFS_FILE* File)
629629
return;
630630
/* Only destroy a file that has no refs */
631631
if (File->RefCount <= 0) {
632-
FreePool(File->Path);
632+
SafeFreePool(File->Path);
633633
FreePool(File);
634634
}
635635
}

uefi-driver/uefi_driver.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* uefi_driver.c - ntfs-3g UEFI filesystem driver */
22
/*
3-
* Copyright © 2014-2021 Pete Batard <pete@akeo.ie>
3+
* Copyright © 2014-2023 Pete Batard <pete@akeo.ie>
44
* Based on iPXE's efi_driver.c and efi_file.c:
55
* Copyright © 2011,2013 Michael Brown <mbrown@fensystems.co.uk>
66
*
@@ -108,8 +108,7 @@ static VOID
108108
FreeFsInstance(EFI_FS* Instance) {
109109
if (Instance == NULL)
110110
return;
111-
if (Instance->DevicePathString != NULL)
112-
FreePool(Instance->DevicePathString);
111+
SafeFreePool(Instance->DevicePathString);
113112
FreePool(Instance);
114113
}
115114

uefi-driver/uefi_file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ FileOpen(EFI_FILE_HANDLE This, EFI_FILE_HANDLE* New,
192192
/* NB: This call only destroys the file if RefCount = 0 */
193193
NtfsFreeFile(NewFile);
194194
}
195-
FreePool(Path);
195+
SafeFreePool(Path);
196196
return Status;
197197
}
198198

0 commit comments

Comments
 (0)