[CMD] Add missing memory allocation NULL checks (#161). CORE-8304

Adapted from a patch by Jacob S. Preciado.

Bring also the code suggestions emitted during review.
This commit is contained in:
Hermès Bélusca-Maïto
2017-12-03 18:49:41 +01:00
parent 73798d2e71
commit 3f892a8d6b
16 changed files with 293 additions and 147 deletions

View File

@@ -96,9 +96,13 @@ static LPTSTR ReadFileContents(FILE *InputFile, TCHAR *Buffer)
{
SIZE_T Len = 0;
SIZE_T AllocLen = 1000;
LPTSTR Contents = cmd_alloc(AllocLen * sizeof(TCHAR));
if (!Contents)
{
WARN("Cannot allocate memory for Contents!\n");
return NULL;
}
while (_fgetts(Buffer, CMDLINE_LENGTH, InputFile))
{
@@ -109,6 +113,7 @@ static LPTSTR ReadFileContents(FILE *InputFile, TCHAR *Buffer)
Contents = cmd_realloc(Contents, (AllocLen *= 2) * sizeof(TCHAR));
if (!Contents)
{
WARN("Cannot reallocate memory for Contents!\n");
cmd_free(OldContents);
return NULL;
}
@@ -454,7 +459,7 @@ static INT ForRecursive(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *
return Ret;
}
BOOL
INT
ExecuteFor(PARSED_COMMAND *Cmd)
{
TCHAR Buffer[CMDLINE_LENGTH]; /* Buffer to hold the variable value */
@@ -470,6 +475,7 @@ ExecuteFor(PARSED_COMMAND *Cmd)
lpNew = cmd_alloc(sizeof(FOR_CONTEXT));
if (!lpNew)
{
WARN("Cannot allocate memory for lpNew!\n");
cmd_free(List);
return 1;
}