Revision: 17201
          http://sourceforge.net/p/edk2/code/17201
Author:   shenshushi
Date:     2015-04-27 03:09:34 +0000 (Mon, 27 Apr 2015)
Log Message:
-----------
ShellPkg: Refine the logic about allocating memory for variable name and data.

The run time service 'QueryVariableInfo' is not proper to be used to get the 
variable name size. This patch refine the logic about allocating memory for 
variable name and data.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qiu Shumin <[email protected]>
Reviewed-by: Ruiyu Ni <[email protected]>

Modified Paths:
--------------
    trunk/edk2/ShellPkg/Application/Shell/ShellEnvVar.c
    trunk/edk2/ShellPkg/Application/Shell/ShellProtocol.c

Modified: trunk/edk2/ShellPkg/Application/Shell/ShellEnvVar.c
===================================================================
--- trunk/edk2/ShellPkg/Application/Shell/ShellEnvVar.c 2015-04-25 12:15:44 UTC 
(rev 17200)
+++ trunk/edk2/ShellPkg/Application/Shell/ShellEnvVar.c 2015-04-27 03:09:34 UTC 
(rev 17201)
@@ -1,7 +1,7 @@
 /** @file
   function declarations for shell environment functions.
 
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
@@ -14,6 +14,9 @@
 
 #include "Shell.h"
 
+#define INIT_NAME_BUFFER_SIZE  128
+#define INIT_DATA_BUFFER_SIZE  1024
+
 /**
   Reports whether an environment variable is Volatile or Non-Volatile.
 
@@ -125,59 +128,72 @@
 {
   CHAR16            *VariableName;
   UINTN             NameSize;
-  UINT64            MaxStorSize;
-  UINT64            RemStorSize;
-  UINT64            MaxVarSize;
+  UINTN             NameBufferSize;
   EFI_STATUS        Status;
   EFI_GUID          Guid;
   UINTN             ValSize;
+  UINTN             ValBufferSize;
   ENV_VAR_LIST      *VarList;
 
   if (ListHead == NULL) {
     return (EFI_INVALID_PARAMETER);
   }
-
-  if (gRT->Hdr.Revision >= EFI_2_00_SYSTEM_TABLE_REVISION) {
-    Status = 
gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS,
 &MaxStorSize, &RemStorSize, &MaxVarSize);
-    if (EFI_ERROR(Status)) {
-      return (Status);
-    }
-  } else {
-    Status = EFI_SUCCESS;
-    MaxVarSize = 16384;
-  }
-
-  NameSize = (UINTN)MaxVarSize;
-  VariableName = AllocateZeroPool(NameSize);
+  
+  Status = EFI_SUCCESS;
+  
+  ValBufferSize = INIT_DATA_BUFFER_SIZE;
+  NameBufferSize = INIT_NAME_BUFFER_SIZE;
+  VariableName = AllocateZeroPool(NameBufferSize);
   if (VariableName == NULL) {
     return (EFI_OUT_OF_RESOURCES);
   }
   *VariableName = CHAR_NULL;
 
   while (!EFI_ERROR(Status)) {
-    NameSize = (UINTN)MaxVarSize;
+    NameSize = NameBufferSize;
     Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);
     if (Status == EFI_NOT_FOUND){
       Status = EFI_SUCCESS;
       break;
+    } else if (Status == EFI_BUFFER_TOO_SMALL) {
+      NameBufferSize = NameSize > NameBufferSize * 2 ? NameSize : 
NameBufferSize * 2;
+      SHELL_FREE_NON_NULL(VariableName);
+      VariableName = AllocateZeroPool(NameBufferSize);
+      if (VariableName == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+        break;
+      }
+      NameSize = NameBufferSize;
+      Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);
     }
+    
     if (!EFI_ERROR(Status) && CompareGuid(&Guid, &gShellVariableGuid)){
       VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));
       if (VarList == NULL) {
         Status = EFI_OUT_OF_RESOURCES;
       } else {
-        ValSize = 0;
+        ValSize = ValBufferSize;
+        VarList->Val = AllocateZeroPool(ValSize);
+        if (VarList->Val == NULL) {
+            SHELL_FREE_NON_NULL(VarList);
+            Status = EFI_OUT_OF_RESOURCES;
+            break;
+        }
         Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, 
&VarList->Atts, &ValSize, VarList->Val);
         if (Status == EFI_BUFFER_TOO_SMALL){
-          VarList->Val = AllocateZeroPool(ValSize);
+          ValBufferSize = ValSize > ValBufferSize * 2 ? ValSize : 
ValBufferSize * 2;
+          SHELL_FREE_NON_NULL (VarList->Val);
+          VarList->Val = AllocateZeroPool(ValBufferSize);
           if (VarList->Val == NULL) {
             SHELL_FREE_NON_NULL(VarList);
             Status = EFI_OUT_OF_RESOURCES;
-          } else {
-            Status = 
SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, 
&ValSize, VarList->Val);
+            break;
           }
+          
+          ValSize = ValBufferSize;
+          Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, 
&VarList->Atts, &ValSize, VarList->Val);
         }
-        if (!EFI_ERROR(Status) && VarList != NULL) {
+        if (!EFI_ERROR(Status)) {
           VarList->Key = AllocateCopyPool(StrSize(VariableName), VariableName);
           if (VarList->Key == NULL) {
             SHELL_FREE_NON_NULL(VarList->Val);
@@ -186,11 +202,14 @@
           } else {
             InsertTailList(ListHead, &VarList->Link);
           }
+        } else {
+          SHELL_FREE_NON_NULL(VarList->Val);
+          SHELL_FREE_NON_NULL(VarList);
         }
-      }
+      } // if (VarList == NULL) ... else ...
     } // compare guid
   } // while
-  FreePool(VariableName);
+  SHELL_FREE_NON_NULL (VariableName);
 
   if (EFI_ERROR(Status)) {
     FreeEnvironmentVariableList(ListHead);

Modified: trunk/edk2/ShellPkg/Application/Shell/ShellProtocol.c
===================================================================
--- trunk/edk2/ShellPkg/Application/Shell/ShellProtocol.c       2015-04-25 
12:15:44 UTC (rev 17200)
+++ trunk/edk2/ShellPkg/Application/Shell/ShellProtocol.c       2015-04-27 
03:09:34 UTC (rev 17201)
@@ -3,7 +3,7 @@
   manipulation, and initialization of EFI_SHELL_PROTOCOL.
 
   (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
@@ -16,6 +16,8 @@
 
 #include "Shell.h"
 
+#define INIT_NAME_BUFFER_SIZE  128
+
 /**
   Close an open file handle.
 
@@ -3102,20 +3104,17 @@
 InternalEfiShellGetListAlias(
   )
 {
-  UINT64            MaxStorSize;
-  UINT64            RemStorSize;
-  UINT64            MaxVarSize;
+  
   EFI_STATUS        Status;
   EFI_GUID          Guid;
   CHAR16            *VariableName;
   UINTN             NameSize;
+  UINTN             NameBufferSize;
   CHAR16            *RetVal;
   UINTN             RetSize;
 
-  Status = 
gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS,
 &MaxStorSize, &RemStorSize, &MaxVarSize);
-  ASSERT_EFI_ERROR(Status);
-
-  VariableName  = AllocateZeroPool((UINTN)MaxVarSize);
+  NameBufferSize = INIT_NAME_BUFFER_SIZE;
+  VariableName  = AllocateZeroPool(NameBufferSize);
   RetSize       = 0;
   RetVal        = NULL;
 
@@ -3126,22 +3125,38 @@
   VariableName[0] = CHAR_NULL;
 
   while (TRUE) {
-    NameSize = (UINTN)MaxVarSize;
+    NameSize = NameBufferSize;
     Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);
     if (Status == EFI_NOT_FOUND){
       break;
+    } else if (Status == EFI_BUFFER_TOO_SMALL) {
+      NameBufferSize = NameSize > NameBufferSize * 2 ? NameSize : 
NameBufferSize * 2;
+      SHELL_FREE_NON_NULL(VariableName);
+      VariableName = AllocateZeroPool(NameBufferSize);
+      if (VariableName == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+        SHELL_FREE_NON_NULL(RetVal);
+        RetVal = NULL;
+        break;
+      }
+      
+      NameSize = NameBufferSize;
+      Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);
     }
-    ASSERT_EFI_ERROR(Status);
-    if (EFI_ERROR(Status)) {
+    
+    if (EFI_ERROR (Status)) {
+      SHELL_FREE_NON_NULL(RetVal);
+      RetVal = NULL;
       break;
     }
+    
     if (CompareGuid(&Guid, &gShellAliasGuid)){
       ASSERT((RetVal == NULL && RetSize == 0) || (RetVal != NULL));
       RetVal = StrnCatGrow(&RetVal, &RetSize, VariableName, 0);
       RetVal = StrnCatGrow(&RetVal, &RetSize, L";", 0);
     } // compare guid
   } // while
-  FreePool(VariableName);
+  SHELL_FREE_NON_NULL(VariableName);
 
   return (RetVal);
 }


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
edk2-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-commits

Reply via email to