[CMD] IF: Add extra validity checks in ExecuteIf().

This commit is contained in:
Hermès Bélusca-Maïto 2020-09-03 16:03:18 +02:00
parent 038daa6367
commit c81bf4f823
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 20 additions and 9 deletions

View file

@ -938,7 +938,7 @@ GetEnvVarOrSpecial(LPCTSTR varName)
/* %CMDEXTVERSION% */ /* %CMDEXTVERSION% */
else if (_tcsicmp(varName, _T("CMDEXTVERSION")) == 0) else if (_tcsicmp(varName, _T("CMDEXTVERSION")) == 0)
{ {
/* Set version number to CMDEXTVERSION */ /* Set Command Extensions version number to CMDEXTVERSION */
_itot(CMDEXTVERSION, ret, 10); _itot(CMDEXTVERSION, ret, 10);
return ret; return ret;
} }

View file

@ -78,20 +78,19 @@ INT ExecuteIf(PARSED_COMMAND *Cmd)
Right = DoDelayedExpansion(Cmd->If.RightArg); Right = DoDelayedExpansion(Cmd->If.RightArg);
if (!Right) if (!Right)
{ {
cmd_free(Left); if (Left) cmd_free(Left);
return 1; return 1;
} }
if (bEnableExtensions && (Cmd->If.Operator == IF_CMDEXTVERSION)) if (bEnableExtensions && (Cmd->If.Operator == IF_CMDEXTVERSION))
{ {
/* IF CMDEXTVERSION n: check if Command Extensions version /* IF CMDEXTVERSION n: check if Command Extensions
* is greater or equal to n */ * version is greater or equal to n. */
DWORD n = _tcstoul(Right, &param, 10); DWORD n = _tcstoul(Right, &param, 10);
if (*param != _T('\0')) if (*param != _T('\0'))
{ {
error_syntax(Right); error_syntax(Right);
cmd_free(Right); goto fail;
return 1;
} }
result = (CMDEXTVERSION >= n); result = (CMDEXTVERSION >= n);
} }
@ -107,8 +106,7 @@ INT ExecuteIf(PARSED_COMMAND *Cmd)
if (*param != _T('\0')) if (*param != _T('\0'))
{ {
error_syntax(Right); error_syntax(Right);
cmd_free(Right); goto fail;
return 1;
} }
result = (nErrorLevel >= n); result = (nErrorLevel >= n);
} }
@ -178,11 +176,19 @@ INT ExecuteIf(PARSED_COMMAND *Cmd)
case IF_LEQ: result = (result <= 0); break; case IF_LEQ: result = (result <= 0); break;
case IF_GTR: result = (result > 0); break; case IF_GTR: result = (result > 0); break;
case IF_GEQ: result = (result >= 0); break; case IF_GEQ: result = (result >= 0); break;
default: goto unknownOp;
} }
} }
else
{
unknownOp:
ERR("Unknown IF operator 0x%x\n", Cmd->If.Operator);
ASSERT(FALSE);
goto fail;
}
} }
cmd_free(Left); if (Left) cmd_free(Left);
cmd_free(Right); cmd_free(Right);
if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0)) if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0))
@ -195,6 +201,11 @@ INT ExecuteIf(PARSED_COMMAND *Cmd)
/* Full condition was false, do the "else" command if there is one */ /* Full condition was false, do the "else" command if there is one */
return ExecuteCommand(Cmd->Subcommands->Next); return ExecuteCommand(Cmd->Subcommands->Next);
} }
fail:
if (Left) cmd_free(Left);
cmd_free(Right);
return 1;
} }
/* EOF */ /* EOF */