]> git.proxmox.com Git - mirror_edk2.git/commitdiff
CryptoPkg: BaseCryptLib: support free(NULL)
authorLaszlo Ersek <lersek@redhat.com>
Wed, 24 Feb 2016 20:00:04 +0000 (21:00 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Thu, 25 Feb 2016 10:04:02 +0000 (11:04 +0100)
The ISO C standard says about free(),

  If ptr is a null pointer, no action occurs.

This is not true of the FreePool() interface of the MemoryAllocationLib
class:

  Buffer must have been allocated on a previous call to the pool
  allocation services of the Memory Allocation Library. [...] If Buffer
  was not allocated with a pool allocation function in the Memory
  Allocation Library, then ASSERT().

Therefore we must not forward the argument of free() to FreePool() without
checking.

This bug can be triggered by upstream OpenSSL commit 8e704858f219
("RT3955: Reduce some stack usage"), for example.

Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Qin Long <qin.long@intel.com>
Cc: Ting Ye <ting.ye@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Qin Long <qin.long@intel.com>
CryptoPkg/Library/BaseCryptLib/SysCall/BaseMemAllocation.c

index 544f07215b8f1b5dbdd89a1a233b822cf3634aae..964545f143cc5e45df4f397f3129a6da95aa21ff 100644 (file)
@@ -38,5 +38,11 @@ void *realloc (void *ptr, size_t size)
 /* De-allocates or frees a memory block */\r
 void free (void *ptr)\r
 {\r
-  FreePool (ptr);\r
+  //\r
+  // In Standard C, free() handles a null pointer argument transparently. This\r
+  // is not true of FreePool() below, so protect it.\r
+  //\r
+  if (ptr != NULL) {\r
+    FreePool (ptr);\r
+  }\r
 }\r