summaryrefslogtreecommitdiff
path: root/ShellPkg/Application/Shell/ShellEnvVar.c
diff options
context:
space:
mode:
authorJaben Carsey <jaben.carsey@intel.com>2014-09-02 20:17:38 +0000
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>2014-09-02 20:17:38 +0000
commit7f79b01e8ef43901785dbccc1f4676cdf9a53d31 (patch)
treee47e56d49cd81748b1e48598a6e9f5a6c357311a /ShellPkg/Application/Shell/ShellEnvVar.c
parent8ac6e336ff573f6105d4f776bdf7db1d189c7093 (diff)
downloadedk2-platforms-7f79b01e8ef43901785dbccc1f4676cdf9a53d31.tar.xz
ShellPkg: Refactor string manipulation
This patch replaces StrCpy with StrnCpy or refactors out the usage of StrCpy through some other means. This patch replaces StrCat with StrnCat or refactors out the usage of StrCat through some other means. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jaben Carsey <jaben.carsey@intel.com> Reviewed-by: Erik Bjorge <erik.c.bjorge@intel.com> git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16038 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'ShellPkg/Application/Shell/ShellEnvVar.c')
-rw-r--r--ShellPkg/Application/Shell/ShellEnvVar.c46
1 files changed, 34 insertions, 12 deletions
diff --git a/ShellPkg/Application/Shell/ShellEnvVar.c b/ShellPkg/Application/Shell/ShellEnvVar.c
index f40a867cc7..b94711142a 100644
--- a/ShellPkg/Application/Shell/ShellEnvVar.c
+++ b/ShellPkg/Application/Shell/ShellEnvVar.c
@@ -152,7 +152,7 @@ GetEnvironmentVariableList(
if (VariableName == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
- StrCpy(VariableName, L"");
+ *VariableName = CHAR_NULL;
while (!EFI_ERROR(Status)) {
NameSize = (UINTN)MaxVarSize;
@@ -178,13 +178,12 @@ GetEnvironmentVariableList(
}
}
if (!EFI_ERROR(Status) && VarList != NULL) {
- VarList->Key = AllocateZeroPool(StrSize(VariableName));
+ VarList->Key = AllocateCopyPool(StrSize(VariableName), VariableName);
if (VarList->Key == NULL) {
SHELL_FREE_NON_NULL(VarList->Val);
SHELL_FREE_NON_NULL(VarList);
Status = EFI_OUT_OF_RESOURCES;
} else {
- StrCpy(VarList->Key, VariableName);
InsertTailList(ListHead, &VarList->Link);
}
}
@@ -286,7 +285,6 @@ SetEnvironmentVariables(
UINTN CurrentCount;
ENV_VAR_LIST *VarList;
ENV_VAR_LIST *Node;
- UINTN NewSize;
VarList = NULL;
@@ -307,20 +305,44 @@ SetEnvironmentVariables(
}
ASSERT(StrStr(CurrentString, L"=") != NULL);
Node = AllocateZeroPool(sizeof(ENV_VAR_LIST));
- ASSERT(Node != NULL);
+ if (Node == NULL) {
+ SetEnvironmentVariableList(&VarList->Link);
+ return (EFI_OUT_OF_RESOURCES);
+ }
+
Node->Key = AllocateZeroPool((StrStr(CurrentString, L"=") - CurrentString + 1) * sizeof(CHAR16));
- ASSERT(Node->Key != NULL);
+ if (Node->Key == NULL) {
+ SHELL_FREE_NON_NULL(Node);
+ SetEnvironmentVariableList(&VarList->Link);
+ return (EFI_OUT_OF_RESOURCES);
+ }
+
+ //
+ // Copy the string into the Key, leaving the last character allocated as NULL to terminate
+ //
StrnCpy(Node->Key, CurrentString, StrStr(CurrentString, L"=") - CurrentString);
- NewSize = StrSize(CurrentString);
- NewSize -= StrLen(Node->Key) - 1;
- Node->Val = AllocateZeroPool(NewSize);
- ASSERT(Node->Val != NULL);
- StrCpy(Node->Val, CurrentString + StrLen(Node->Key) + 1);
+
+ //
+ // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other)
+ //
+ Node->Val = AllocateCopyPool(StrSize(CurrentString) - StrSize(Node->Key), CurrentString + StrLen(Node->Key) + 1);
+ if (Node->Val == NULL) {
+ SHELL_FREE_NON_NULL(Node->Key);
+ SHELL_FREE_NON_NULL(Node);
+ SetEnvironmentVariableList(&VarList->Link);
+ return (EFI_OUT_OF_RESOURCES);
+ }
+
Node->Atts = EFI_VARIABLE_BOOTSERVICE_ACCESS;
if (VarList == NULL) {
VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));
- ASSERT(VarList != NULL);
+ if (VarList == NULL) {
+ SHELL_FREE_NON_NULL(Node->Key);
+ SHELL_FREE_NON_NULL(Node->Val);
+ SHELL_FREE_NON_NULL(Node);
+ return (EFI_OUT_OF_RESOURCES);
+ }
InitializeListHead(&VarList->Link);
}
InsertTailList(&VarList->Link, &Node->Link);