From 42435671334345f91e7cb2e5e963ed457fbb61d6 Mon Sep 17 00:00:00 2001 From: Qiu Shumin Date: Wed, 31 Dec 2014 01:31:00 +0000 Subject: [PATCH] ShellPkg: Check the unrecognized environment variable name before it is removed from command line. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Qiu Shumin Reviewed-by: Jaben Carsey git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16563 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Application/Shell/Shell.c | 66 ++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index de46a72b44..695880197b 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -142,6 +142,53 @@ FindNextInstance( return (Temp); } +/** + Check whether the string between a pair of % is a valid envifronment variable name. + + @param[in] BeginPercent pointer to the first percent. + @param[in] EndPercent pointer to the last percent. + + @retval TRUE is a valid environment variable name. + @retval FALSE is NOT a valid environment variable name. +**/ +BOOLEAN +IsValidEnvironmentVariableName( + IN CONST CHAR16 *BeginPercent, + IN CONST CHAR16 *EndPercent + ) +{ + CONST CHAR16 *Walker; + + Walker = NULL; + + ASSERT (BeginPercent != NULL); + ASSERT (EndPercent != NULL); + ASSERT (BeginPercent < EndPercent); + + if ((BeginPercent + 1) == EndPercent) { + return FALSE; + } + + for (Walker = BeginPercent + 1; Walker < EndPercent; Walker++) { + if ( + (*Walker >= L'0' && *Walker <= L'9') || + (*Walker >= L'A' && *Walker <= L'Z') || + (*Walker >= L'a' && *Walker <= L'z') || + (*Walker == L'_') + ) { + if (Walker == BeginPercent + 1 && (*Walker >= L'0' && *Walker <= L'9')) { + return FALSE; + } else { + continue; + } + } else { + return FALSE; + } + } + + return TRUE; +} + /** Find a command line contains a split operation @@ -1355,14 +1402,17 @@ StripUnreplacedEnvironmentVariables( } ASSERT(FirstPercent < FirstQuote); if (SecondPercent < FirstQuote) { - FirstPercent[0] = L'\"'; - SecondPercent[0] = L'\"'; - - // - // We need to remove from FirstPercent to SecondPercent - // - CopyMem(FirstPercent + 1, SecondPercent, StrSize(SecondPercent)); - CurrentLocator = FirstPercent + 2; + if (IsValidEnvironmentVariableName(FirstPercent, SecondPercent)) { + // + // We need to remove from FirstPercent to SecondPercent + // + CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1)); + // + // dont need to update the locator. both % characters are gone. + // + } else { + CurrentLocator = SecondPercent + 1; + } continue; } ASSERT(FirstQuote < SecondPercent); -- 2.39.2