[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

@@ -188,18 +188,20 @@ BOOL add_entry (LPINT ac, LPTSTR **arg, LPCTSTR entry)
LPTSTR *oldarg;
q = cmd_alloc ((_tcslen(entry) + 1) * sizeof (TCHAR));
if (NULL == q)
if (!q)
{
WARN("Cannot allocate memory for q!\n");
return FALSE;
}
_tcscpy (q, entry);
oldarg = *arg;
*arg = cmd_realloc (oldarg, (*ac + 2) * sizeof (LPTSTR));
if (NULL == *arg)
if (!*arg)
{
cmd_free (q);
WARN("Cannot reallocate memory for arg!\n");
*arg = oldarg;
cmd_free (q);
return FALSE;
}
@@ -222,8 +224,9 @@ static BOOL expand (LPINT ac, LPTSTR **arg, LPCTSTR pattern)
if (NULL != pathend)
{
dirpart = cmd_alloc((pathend - pattern + 2) * sizeof(TCHAR));
if (NULL == dirpart)
if (!dirpart)
{
WARN("Cannot allocate memory for dirpart!\n");
return FALSE;
}
memcpy(dirpart, pattern, pathend - pattern + 1);
@@ -241,8 +244,9 @@ static BOOL expand (LPINT ac, LPTSTR **arg, LPCTSTR pattern)
if (NULL != dirpart)
{
fullname = cmd_alloc((_tcslen(dirpart) + _tcslen(FindData.cFileName) + 1) * sizeof(TCHAR));
if (NULL == fullname)
if (!fullname)
{
WARN("Cannot allocate memory for fullname!\n");
ok = FALSE;
}
else
@@ -286,7 +290,10 @@ LPTSTR *split (LPTSTR s, LPINT args, BOOL expand_wildcards, BOOL handle_plus)
arg = cmd_alloc (sizeof (LPTSTR));
if (!arg)
{
WARN("Cannot allocate memory for arg!\n");
return NULL;
}
*arg = NULL;
ac = 0;
@@ -333,6 +340,7 @@ LPTSTR *split (LPTSTR s, LPINT args, BOOL expand_wildcards, BOOL handle_plus)
q = cmd_alloc (((len = s - start) + 1) * sizeof (TCHAR));
if (!q)
{
WARN("Cannot allocate memory for q!\n");
return NULL;
}
memcpy (q, start, len * sizeof (TCHAR));
@@ -381,7 +389,10 @@ LPTSTR *splitspace (LPTSTR s, LPINT args)
arg = cmd_alloc (sizeof (LPTSTR));
if (!arg)
{
WARN("Cannot allocate memory for arg!\n");
return NULL;
}
*arg = NULL;
ac = 0;
@@ -409,6 +420,7 @@ LPTSTR *splitspace (LPTSTR s, LPINT args)
q = cmd_alloc (((len = s - start) + 1) * sizeof (TCHAR));
if (!q)
{
WARN("Cannot allocate memory for q!\n");
return NULL;
}
memcpy (q, start, len * sizeof (TCHAR));