summaryrefslogtreecommitdiffstats
path: root/ShellPkg/Application/Shell/ShellProtocol.c
diff options
context:
space:
mode:
authorQiu Shumin <shumin.qiu@intel.com>2015-04-27 03:09:34 +0000
committershenshushi <shenshushi@Edk2>2015-04-27 03:09:34 +0000
commit654a012ba5e99c7439df392ed44b3b4e02aaca2f (patch)
tree5bcafe2ee9b7fcf8b9a115ccbdbee975bfc74c18 /ShellPkg/Application/Shell/ShellProtocol.c
parent43bfa5273d380290f55af17398ab0354110de5bb (diff)
downloadedk2-654a012ba5e99c7439df392ed44b3b4e02aaca2f.tar.gz
edk2-654a012ba5e99c7439df392ed44b3b4e02aaca2f.tar.bz2
edk2-654a012ba5e99c7439df392ed44b3b4e02aaca2f.zip
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 <shumin.qiu@intel.com> Reviewed-by: Ruiyu Ni <Ruiyu.ni@intel.com> git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17201 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'ShellPkg/Application/Shell/ShellProtocol.c')
-rw-r--r--ShellPkg/Application/Shell/ShellProtocol.c41
1 files changed, 28 insertions, 13 deletions
diff --git a/ShellPkg/Application/Shell/ShellProtocol.c b/ShellPkg/Application/Shell/ShellProtocol.c
index 6fc566856f..72d42d7222 100644
--- a/ShellPkg/Application/Shell/ShellProtocol.c
+++ b/ShellPkg/Application/Shell/ShellProtocol.c
@@ -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 @@ EFIAPI
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 @@ InternalEfiShellGetListAlias(
VariableName[0] = CHAR_NULL;
while (TRUE) {
- NameSize = (UINTN)MaxVarSize;
+ NameSize = NameBufferSize;
Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);
if (Status == EFI_NOT_FOUND){
break;
- }
- ASSERT_EFI_ERROR(Status);
- if (EFI_ERROR(Status)) {
+ } 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);
+ }
+
+ 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);
}