public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 1/1] ShellPkg/CommandLib: avoid NULL derefence and memory leak
@ 2019-04-05  1:29 Heinrich Schuchardt
  2019-04-09  5:33 ` [edk2-devel] " Gao, Zhichao
  0 siblings, 1 reply; 5+ messages in thread
From: Heinrich Schuchardt @ 2019-04-05  1:29 UTC (permalink / raw)
  To: Jaben Carsey, Ray Ni, Leif Lindholm, Ard Biesheuvel
  Cc: Ruiyu Ni, devel, Heinrich Schuchardt

Since TianoCore EDK2 commit d65f2cea36d1 ("ShellPkg/CommandLib: Locate
proper UnicodeCollation instance") in edk2 the UEFI Shell crashes if EFI
variable PlatformLang is not defined due to dereferencing gUnicodeCollation
gUnicodeCollation (= NULL) in ShellCommandRegisterCommandName().

Furthermore CommandInit() is leaking PlatformLang if
gUnicodeCollation != NULL.

Close the memory leak and use the first UnicodeCollation instance if
PlatfomLang is not defined.

Fixes: d65f2cea36d1 ("ShellPkg/CommandLib: Locate proper UnicodeCollation
instance")
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 .../UefiShellCommandLib/UefiShellCommandLib.c | 20 +++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
index ddc4bb1567..e60279e5ac 100644
--- a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
+++ b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
@@ -80,12 +80,10 @@ CommandInit(
   EFI_STATUS                      Status;
   CHAR8                           *PlatformLang;

-  GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME, (VOID**)&PlatformLang, NULL);
-  if (PlatformLang == NULL) {
-    return EFI_UNSUPPORTED;
-  }
-
   if (gUnicodeCollation == NULL) {
+
+    GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME, (VOID**)&PlatformLang, NULL);
+
     Status = gBS->LocateHandleBuffer (
                     ByProtocol,
                     &gEfiUnicodeCollation2ProtocolGuid,
@@ -113,6 +111,14 @@ CommandInit(
         continue;
       }

+      //
+      // Without clue provided use the first Unicode Collation2 protocol.
+      //
+      if (PlatformLang == NULL) {
+        gUnicodeCollation = Uc;
+        break;
+      }
+
       //
       // Find the best matching matching language from the supported languages
       // of Unicode Collation2 protocol.
@@ -132,7 +138,9 @@ CommandInit(
     if (Handles != NULL) {
       FreePool (Handles);
     }
-    FreePool (PlatformLang);
+    if (PlatformLang != NULL) {
+      FreePool (PlatformLang);
+    }
   }

   return (gUnicodeCollation == NULL) ? EFI_UNSUPPORTED : EFI_SUCCESS;
--
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-04-10  0:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-05  1:29 [PATCH 1/1] ShellPkg/CommandLib: avoid NULL derefence and memory leak Heinrich Schuchardt
2019-04-09  5:33 ` [edk2-devel] " Gao, Zhichao
2019-04-09  6:03   ` Heinrich Schuchardt
2019-04-09 16:37   ` Heinrich Schuchardt
2019-04-10  0:44     ` Gao, Zhichao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox