mirror of
https://github.com/reactos/reactos.git
synced 2024-06-21 13:31:38 +00:00
[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:
parent
73798d2e71
commit
3f892a8d6b
|
@ -64,7 +64,10 @@ PrintAlias (VOID)
|
|||
/* allocate memory for an extra \0 char to make parsing easier */
|
||||
ptr = cmd_alloc(len + sizeof(TCHAR));
|
||||
if (!ptr)
|
||||
{
|
||||
WARN("Cannot allocate memory for ptr!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
Aliases = ptr;
|
||||
|
||||
|
@ -107,6 +110,7 @@ VOID ExpandAlias (LPTSTR cmd, INT maxlen)
|
|||
buffer = cmd_alloc(maxlen);
|
||||
if (!buffer)
|
||||
{
|
||||
WARN("Cannot allocate memory for alias buffer!\n");
|
||||
cmd_free(tmp);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -33,19 +33,13 @@ PrintAssociation(LPTSTR extension)
|
|||
return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey);
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(hKey);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return_val = RegOpenKeyEx(hKey, extension, 0, KEY_READ, &hInsideKey);
|
||||
RegCloseKey(hKey);
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(hKey);
|
||||
RegCloseKey(hInsideKey);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* obtain string length */
|
||||
return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, NULL, &fileTypeLength);
|
||||
|
@ -53,24 +47,26 @@ PrintAssociation(LPTSTR extension)
|
|||
if (return_val == ERROR_FILE_NOT_FOUND) /* no default value, don't display */
|
||||
{
|
||||
RegCloseKey(hInsideKey);
|
||||
RegCloseKey(hKey);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(hInsideKey);
|
||||
RegCloseKey(hKey);
|
||||
return -2;
|
||||
}
|
||||
|
||||
fileType = cmd_alloc(fileTypeLength * sizeof(TCHAR));
|
||||
if (!fileType)
|
||||
{
|
||||
WARN("Cannot allocate memory for fileType!\n");
|
||||
RegCloseKey(hInsideKey);
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* obtain actual file type */
|
||||
return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, (LPBYTE)fileType, &fileTypeLength);
|
||||
|
||||
RegCloseKey(hInsideKey);
|
||||
RegCloseKey(hKey);
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
|
@ -83,14 +79,12 @@ PrintAssociation(LPTSTR extension)
|
|||
ConOutPrintf(_T("%s=%s\n"), extension, fileType);
|
||||
}
|
||||
|
||||
if (fileTypeLength)
|
||||
cmd_free(fileType);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static INT
|
||||
PrintAllAssociations()
|
||||
PrintAllAssociations(VOID)
|
||||
{
|
||||
DWORD return_val = 0;
|
||||
HKEY hKey = NULL;
|
||||
|
@ -103,10 +97,7 @@ PrintAllAssociations()
|
|||
return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey);
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(hKey);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return_val = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numKeys, &extLength, NULL, NULL, NULL, NULL, NULL, NULL);
|
||||
|
||||
|
@ -118,6 +109,12 @@ PrintAllAssociations()
|
|||
|
||||
extLength++;
|
||||
extName = cmd_alloc(extLength * sizeof(TCHAR));
|
||||
if (!extName)
|
||||
{
|
||||
WARN("Cannot allocate memory for extName!\n");
|
||||
RegCloseKey(hKey);
|
||||
return -2;
|
||||
}
|
||||
|
||||
for (keyCtr = 0; keyCtr < numKeys; keyCtr++)
|
||||
{
|
||||
|
@ -139,9 +136,7 @@ PrintAllAssociations()
|
|||
|
||||
RegCloseKey(hKey);
|
||||
|
||||
if (extName)
|
||||
cmd_free(extName);
|
||||
|
||||
return numKeys;
|
||||
}
|
||||
|
||||
|
@ -157,28 +152,20 @@ AddAssociation(LPTSTR extension, LPTSTR type)
|
|||
return -1;
|
||||
|
||||
return_val = RegCreateKeyEx(hKey, extension, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &insideKey, NULL);
|
||||
RegCloseKey(hKey);
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(hKey);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return_val = RegSetValueEx(insideKey, NULL, 0, REG_SZ, (LPBYTE)type, (_tcslen(type) + 1) * sizeof(TCHAR));
|
||||
RegCloseKey(insideKey);
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(insideKey);
|
||||
RegCloseKey(hKey);
|
||||
return -2;
|
||||
}
|
||||
|
||||
RegCloseKey(insideKey);
|
||||
RegCloseKey(hKey);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
RemoveAssociation(LPTSTR extension)
|
||||
{
|
||||
|
@ -191,19 +178,15 @@ RemoveAssociation(LPTSTR extension)
|
|||
return -1;
|
||||
|
||||
return_val = RegDeleteKey(hKey, extension);
|
||||
RegCloseKey(hKey);
|
||||
|
||||
if (return_val != ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(hKey);
|
||||
return -2;
|
||||
}
|
||||
|
||||
RegCloseKey(hKey);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
INT CommandAssoc (LPTSTR param)
|
||||
{
|
||||
/* print help */
|
||||
|
@ -216,7 +199,9 @@ INT CommandAssoc (LPTSTR param)
|
|||
nErrorLevel = 0;
|
||||
|
||||
if (_tcslen(param) == 0)
|
||||
{
|
||||
PrintAllAssociations();
|
||||
}
|
||||
else
|
||||
{
|
||||
LPTSTR lpEqualSign = _tcschr(param, _T('='));
|
||||
|
@ -224,9 +209,15 @@ INT CommandAssoc (LPTSTR param)
|
|||
{
|
||||
LPTSTR fileType = lpEqualSign + 1;
|
||||
LPTSTR extension = cmd_alloc((lpEqualSign - param + 1) * sizeof(TCHAR));
|
||||
if (!extension)
|
||||
{
|
||||
WARN("Cannot allocate memory for extension!\n");
|
||||
error_out_of_memory();
|
||||
return 1;
|
||||
}
|
||||
|
||||
_tcsncpy(extension, param, lpEqualSign - param);
|
||||
extension[lpEqualSign - param] = (TCHAR)0;
|
||||
extension[lpEqualSign - param] = _T('\0');
|
||||
|
||||
if (_tcslen(fileType) == 0)
|
||||
/* if the equal sign is the last character
|
||||
|
|
|
@ -117,6 +117,7 @@ LPTSTR BatchParams (LPTSTR s1, LPTSTR s2)
|
|||
/* JPP 20-Jul-1998 added error checking */
|
||||
if (dp == NULL)
|
||||
{
|
||||
WARN("Cannot allocate memory for dp!\n");
|
||||
error_out_of_memory();
|
||||
return NULL;
|
||||
}
|
||||
|
@ -158,7 +159,7 @@ LPTSTR BatchParams (LPTSTR s1, LPTSTR s2)
|
|||
/*
|
||||
* free the allocated memory of a batch file
|
||||
*/
|
||||
VOID ClearBatch()
|
||||
VOID ClearBatch(VOID)
|
||||
{
|
||||
TRACE ("ClearBatch mem = %08x free = %d\n", bc->mem, bc->memfree);
|
||||
|
||||
|
@ -182,7 +183,7 @@ VOID ClearBatch()
|
|||
* message
|
||||
*/
|
||||
|
||||
VOID ExitBatch()
|
||||
VOID ExitBatch(VOID)
|
||||
{
|
||||
ClearBatch();
|
||||
|
||||
|
@ -402,6 +403,12 @@ BOOL BatchGetString (LPTSTR lpBuffer, INT nBufferLength)
|
|||
INT len = 0;
|
||||
#ifdef _UNICODE
|
||||
lpString = cmd_alloc(nBufferLength);
|
||||
if (!lpString)
|
||||
{
|
||||
WARN("Cannot allocate memory for lpString\n");
|
||||
error_out_of_memory();
|
||||
return FALSE;
|
||||
}
|
||||
#else
|
||||
lpString = lpBuffer;
|
||||
#endif
|
||||
|
@ -443,7 +450,7 @@ BOOL BatchGetString (LPTSTR lpBuffer, INT nBufferLength)
|
|||
*
|
||||
* Set eflag to 0 if line is not to be echoed else 1
|
||||
*/
|
||||
LPTSTR ReadBatchLine ()
|
||||
LPTSTR ReadBatchLine(VOID)
|
||||
{
|
||||
TRACE ("ReadBatchLine ()\n");
|
||||
|
||||
|
|
|
@ -1390,7 +1390,7 @@ DirList(IN OUT LPTSTR szFullPath, /* [IN] The full path we are listing with tr
|
|||
ptrStartNode = cmd_alloc(sizeof(DIRFINDLISTNODE));
|
||||
if (ptrStartNode == NULL)
|
||||
{
|
||||
WARN("DEBUG: Cannot allocate memory for ptrStartNode!\n");
|
||||
WARN("Cannot allocate memory for ptrStartNode!\n");
|
||||
return 1; /* Error cannot allocate memory for 1st object */
|
||||
}
|
||||
ptrStartNode->stInfo.ptrHead = NULL;
|
||||
|
@ -1408,7 +1408,7 @@ DirList(IN OUT LPTSTR szFullPath, /* [IN] The full path we are listing with tr
|
|||
ptrNextNode->ptrNext = cmd_alloc(sizeof(DIRFINDLISTNODE));
|
||||
if (ptrNextNode->ptrNext == NULL)
|
||||
{
|
||||
WARN("DEBUG: Cannot allocate memory for ptrNextNode->ptrNext!\n");
|
||||
WARN("Cannot allocate memory for ptrNextNode->ptrNext!\n");
|
||||
DirNodeCleanup(ptrStartNode, &dwCount);
|
||||
FindClose(hSearch);
|
||||
return 1;
|
||||
|
@ -1458,7 +1458,7 @@ DirList(IN OUT LPTSTR szFullPath, /* [IN] The full path we are listing with tr
|
|||
*ptrCurNode = cmd_alloc(sizeof(DIRFINDSTREAMNODE));
|
||||
if (*ptrCurNode == NULL)
|
||||
{
|
||||
WARN("DEBUG: Cannot allocate memory for *ptrCurNode!\n");
|
||||
WARN("Cannot allocate memory for *ptrCurNode!\n");
|
||||
DirNodeCleanup(ptrStartNode, &dwCount);
|
||||
FindClose(hStreams);
|
||||
FindClose(hSearch);
|
||||
|
@ -1512,7 +1512,7 @@ DirList(IN OUT LPTSTR szFullPath, /* [IN] The full path we are listing with tr
|
|||
ptrFileArray = cmd_alloc(sizeof(PDIRFINDINFO) * dwCount);
|
||||
if (ptrFileArray == NULL)
|
||||
{
|
||||
WARN("DEBUG: Cannot allocate memory for ptrFileArray!\n");
|
||||
WARN("Cannot allocate memory for ptrFileArray!\n");
|
||||
DirNodeCleanup(ptrStartNode, &dwCount);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ PushDirectory (LPTSTR pszPath)
|
|||
LPDIRENTRY lpDir = cmd_alloc(FIELD_OFFSET(DIRENTRY, szPath[_tcslen(pszPath) + 1]));
|
||||
if (!lpDir)
|
||||
{
|
||||
WARN("Cannot allocate memory for lpDir\n");
|
||||
error_out_of_memory();
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -50,10 +50,10 @@ typedef struct tagHISTORY
|
|||
|
||||
static INT size, max_size = 100;
|
||||
|
||||
static LPHIST_ENTRY Top;
|
||||
static LPHIST_ENTRY Bottom;
|
||||
static LPHIST_ENTRY Top = NULL;
|
||||
static LPHIST_ENTRY Bottom = NULL;
|
||||
|
||||
static LPHIST_ENTRY curr_ptr=0;
|
||||
static LPHIST_ENTRY curr_ptr = NULL;
|
||||
|
||||
VOID InitHistory(VOID);
|
||||
VOID History_move_to_bottom(VOID);
|
||||
|
@ -120,6 +120,8 @@ INT CommandHistory (LPTSTR param)
|
|||
|
||||
VOID set_size(INT new_size)
|
||||
{
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
while (new_size<size)
|
||||
del(Top->prev);
|
||||
|
||||
|
@ -132,7 +134,19 @@ VOID InitHistory(VOID)
|
|||
size = 0;
|
||||
|
||||
Top = cmd_alloc(sizeof(HIST_ENTRY));
|
||||
if (!Top)
|
||||
{
|
||||
WARN("Cannot allocate memory for Top!\n");
|
||||
return;
|
||||
}
|
||||
Bottom = cmd_alloc(sizeof(HIST_ENTRY));
|
||||
if (!Bottom)
|
||||
{
|
||||
WARN("Cannot allocate memory for Bottom!\n");
|
||||
cmd_free(Top);
|
||||
Top = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
Top->prev = Bottom;
|
||||
Top->next = NULL;
|
||||
|
@ -148,6 +162,8 @@ VOID InitHistory(VOID)
|
|||
|
||||
VOID CleanHistory(VOID)
|
||||
{
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
while (Bottom->next != Top)
|
||||
del(Bottom->next);
|
||||
|
||||
|
@ -160,6 +176,8 @@ VOID History_del_current_entry(LPTSTR str)
|
|||
{
|
||||
LPHIST_ENTRY tmp;
|
||||
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
if (size == 0)
|
||||
return;
|
||||
|
||||
|
@ -180,6 +198,8 @@ VOID History_del_current_entry(LPTSTR str)
|
|||
static
|
||||
VOID del(LPHIST_ENTRY item)
|
||||
{
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
if (item==NULL || item==Top || item==Bottom)
|
||||
{
|
||||
TRACE ("del in " __FILE__ ": returning\n"
|
||||
|
@ -206,6 +226,8 @@ VOID add_at_bottom(LPTSTR string)
|
|||
{
|
||||
LPHIST_ENTRY tmp;
|
||||
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
/*delete first entry if maximum number of entries is reached*/
|
||||
while (size>=max_size)
|
||||
del(Top->prev);
|
||||
|
@ -218,23 +240,37 @@ VOID add_at_bottom(LPTSTR string)
|
|||
|
||||
/*if new entry is the same than the last do not add it*/
|
||||
if (size)
|
||||
{
|
||||
if (_tcscmp(string,Bottom->next->string)==0)
|
||||
return;
|
||||
}
|
||||
|
||||
/*fill bottom with string, it will become Bottom->next*/
|
||||
/*create new empty Bottom*/
|
||||
tmp = cmd_alloc(sizeof(HIST_ENTRY));
|
||||
if (!tmp)
|
||||
{
|
||||
WARN("Cannot allocate memory for new Bottom!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/*fill old bottom with string, it will become new Bottom->next*/
|
||||
Bottom->string = cmd_alloc((_tcslen(string)+1)*sizeof(TCHAR));
|
||||
if (!Bottom->string)
|
||||
{
|
||||
WARN("Cannot allocate memory for Bottom->string!\n");
|
||||
cmd_free(tmp);
|
||||
return;
|
||||
}
|
||||
_tcscpy(Bottom->string,string);
|
||||
|
||||
/*save Bottom value*/
|
||||
tmp=Bottom;
|
||||
tmp->next = Bottom;
|
||||
tmp->prev = NULL;
|
||||
tmp->string = NULL;
|
||||
|
||||
/*create new void Bottom*/
|
||||
Bottom=cmd_alloc(sizeof(HIST_ENTRY));
|
||||
Bottom->next=tmp;
|
||||
Bottom->prev=NULL;
|
||||
Bottom->string=NULL;
|
||||
Bottom->prev = tmp;
|
||||
|
||||
tmp->prev=Bottom;
|
||||
/*save the new Bottom value*/
|
||||
Bottom = tmp;
|
||||
|
||||
/*set new size*/
|
||||
size++;
|
||||
|
@ -243,6 +279,8 @@ VOID add_at_bottom(LPTSTR string)
|
|||
|
||||
VOID History_move_to_bottom(VOID)
|
||||
{
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
curr_ptr = Bottom;
|
||||
}
|
||||
|
||||
|
@ -250,6 +288,8 @@ LPCTSTR PeekHistory(INT dir)
|
|||
{
|
||||
LPHIST_ENTRY entry = curr_ptr;
|
||||
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
if (dir == 0)
|
||||
return NULL;
|
||||
|
||||
|
@ -285,6 +325,8 @@ LPCTSTR PeekHistory(INT dir)
|
|||
|
||||
VOID History(INT dir, LPTSTR commandline)
|
||||
{
|
||||
ASSERT(Top && Bottom);
|
||||
|
||||
if (dir==0)
|
||||
{
|
||||
add_at_bottom(commandline);
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -60,7 +60,7 @@ static TCHAR ParseChar(void)
|
|||
TCHAR Char;
|
||||
|
||||
if (bParseError)
|
||||
return CurChar = 0;
|
||||
return (CurChar = 0);
|
||||
|
||||
restart:
|
||||
/*
|
||||
|
@ -90,7 +90,7 @@ restart:
|
|||
}
|
||||
}
|
||||
}
|
||||
return CurChar = Char;
|
||||
return (CurChar = Char);
|
||||
}
|
||||
|
||||
static void ParseError(void)
|
||||
|
@ -272,6 +272,11 @@ static BOOL ParseRedirection(REDIRECTION **List)
|
|||
}
|
||||
|
||||
Redir = cmd_alloc(FIELD_OFFSET(REDIRECTION, Filename[_tcslen(Tok) + 1]));
|
||||
if (!Redir)
|
||||
{
|
||||
WARN("Cannot allocate memory for Redir!\n");
|
||||
goto fail;
|
||||
}
|
||||
Redir->Next = NULL;
|
||||
Redir->OldHandle = INVALID_HANDLE_VALUE;
|
||||
Redir->Number = Number;
|
||||
|
@ -293,7 +298,15 @@ static PARSED_COMMAND *ParseCommandOp(int OpType);
|
|||
static PARSED_COMMAND *ParseBlock(REDIRECTION *RedirList)
|
||||
{
|
||||
PARSED_COMMAND *Cmd, *Sub, **NextPtr;
|
||||
|
||||
Cmd = cmd_alloc(sizeof(PARSED_COMMAND));
|
||||
if (!Cmd)
|
||||
{
|
||||
WARN("Cannot allocate memory for Cmd!\n");
|
||||
ParseError();
|
||||
FreeRedirection(RedirList);
|
||||
return NULL;
|
||||
}
|
||||
Cmd->Type = C_BLOCK;
|
||||
Cmd->Next = NULL;
|
||||
Cmd->Subcommands = NULL;
|
||||
|
@ -340,8 +353,16 @@ static PARSED_COMMAND *ParseBlock(REDIRECTION *RedirList)
|
|||
/* Parse an IF statement */
|
||||
static PARSED_COMMAND *ParseIf(void)
|
||||
{
|
||||
PARSED_COMMAND *Cmd = cmd_alloc(sizeof(PARSED_COMMAND));
|
||||
int Type;
|
||||
PARSED_COMMAND *Cmd;
|
||||
|
||||
Cmd = cmd_alloc(sizeof(PARSED_COMMAND));
|
||||
if (!Cmd)
|
||||
{
|
||||
WARN("Cannot allocate memory for Cmd!\n");
|
||||
ParseError();
|
||||
return NULL;
|
||||
}
|
||||
memset(Cmd, 0, sizeof(PARSED_COMMAND));
|
||||
Cmd->Type = C_IF;
|
||||
|
||||
|
@ -432,10 +453,17 @@ condition_done:
|
|||
*/
|
||||
static PARSED_COMMAND *ParseFor(void)
|
||||
{
|
||||
PARSED_COMMAND *Cmd = cmd_alloc(sizeof(PARSED_COMMAND));
|
||||
PARSED_COMMAND *Cmd;
|
||||
TCHAR List[CMDLINE_LENGTH];
|
||||
TCHAR *Pos = List;
|
||||
|
||||
Cmd = cmd_alloc(sizeof(PARSED_COMMAND));
|
||||
if (!Cmd)
|
||||
{
|
||||
WARN("Cannot allocate memory for Cmd!\n");
|
||||
ParseError();
|
||||
return NULL;
|
||||
}
|
||||
memset(Cmd, 0, sizeof(PARSED_COMMAND));
|
||||
Cmd->Type = C_FOR;
|
||||
|
||||
|
@ -609,6 +637,13 @@ static DECLSPEC_NOINLINE PARSED_COMMAND *ParseCommandPart(REDIRECTION *RedirList
|
|||
*Pos++ = _T('\0');
|
||||
|
||||
Cmd = cmd_alloc(FIELD_OFFSET(PARSED_COMMAND, Command.First[Pos - ParsedLine]));
|
||||
if (!Cmd)
|
||||
{
|
||||
WARN("Cannot allocate memory for Cmd!\n");
|
||||
ParseError();
|
||||
FreeRedirection(RedirList);
|
||||
return NULL;
|
||||
}
|
||||
Cmd->Type = C_COMMAND;
|
||||
Cmd->Next = NULL;
|
||||
Cmd->Subcommands = NULL;
|
||||
|
@ -647,6 +682,12 @@ static PARSED_COMMAND *ParsePrimary(void)
|
|||
PARSED_COMMAND *Cmd;
|
||||
ParseChar();
|
||||
Cmd = cmd_alloc(sizeof(PARSED_COMMAND));
|
||||
if (!Cmd)
|
||||
{
|
||||
WARN("Cannot allocate memory for Cmd!\n");
|
||||
ParseError();
|
||||
return NULL;
|
||||
}
|
||||
Cmd->Type = C_QUIET;
|
||||
Cmd->Next = NULL;
|
||||
/* @ acts like a unary operator with low precedence,
|
||||
|
@ -702,6 +743,14 @@ static PARSED_COMMAND *ParseCommandOp(int OpType)
|
|||
}
|
||||
|
||||
Cmd = cmd_alloc(sizeof(PARSED_COMMAND));
|
||||
if (!Cmd)
|
||||
{
|
||||
WARN("Cannot allocate memory for Cmd!\n");
|
||||
ParseError();
|
||||
FreeCommand(Left);
|
||||
FreeCommand(Right);
|
||||
return NULL;
|
||||
}
|
||||
Cmd->Type = OpType;
|
||||
Cmd->Next = NULL;
|
||||
Cmd->Redirections = NULL;
|
||||
|
@ -840,10 +889,12 @@ EchoCommand(PARSED_COMMAND *Cmd)
|
|||
for (Redir = Cmd->Redirections; Redir; Redir = Redir->Next)
|
||||
{
|
||||
if (SubstituteForVars(Redir->Filename, Buf))
|
||||
{
|
||||
ConOutPrintf(_T(" %c%s%s"), _T('0') + Redir->Number,
|
||||
RedirString[Redir->Mode], Buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* "Unparse" a command into a text form suitable for passing to CMD /C.
|
||||
|
@ -862,19 +913,27 @@ Unparse(PARSED_COMMAND *Cmd, TCHAR *Out, TCHAR *OutEnd)
|
|||
* overflowing the supplied buffer, define some helper macros to make
|
||||
* this less painful.
|
||||
*/
|
||||
#define CHAR(Char) { \
|
||||
#define CHAR(Char) \
|
||||
do { \
|
||||
if (Out == OutEnd) return NULL; \
|
||||
*Out++ = Char; }
|
||||
#define STRING(String) { \
|
||||
*Out++ = Char; \
|
||||
} while (0)
|
||||
#define STRING(String) \
|
||||
do { \
|
||||
if (Out + _tcslen(String) > OutEnd) return NULL; \
|
||||
Out = _stpcpy(Out, String); }
|
||||
#define PRINTF(Format, ...) { \
|
||||
Out = _stpcpy(Out, String); \
|
||||
} while (0)
|
||||
#define PRINTF(Format, ...) \
|
||||
do { \
|
||||
UINT Len = _sntprintf(Out, OutEnd - Out, Format, __VA_ARGS__); \
|
||||
if (Len > (UINT)(OutEnd - Out)) return NULL; \
|
||||
Out += Len; }
|
||||
#define RECURSE(Subcommand) { \
|
||||
Out += Len; \
|
||||
} while (0)
|
||||
#define RECURSE(Subcommand) \
|
||||
do { \
|
||||
Out = Unparse(Subcommand, Out, OutEnd); \
|
||||
if (!Out) return NULL; }
|
||||
if (!Out) return NULL; \
|
||||
} while (0)
|
||||
|
||||
switch (Cmd->Type)
|
||||
{
|
||||
|
@ -883,70 +942,71 @@ Unparse(PARSED_COMMAND *Cmd, TCHAR *Out, TCHAR *OutEnd)
|
|||
* Windows doesn't bother escaping them, so for compatibility
|
||||
* we probably shouldn't do it either */
|
||||
if (!SubstituteForVars(Cmd->Command.First, Buf)) return NULL;
|
||||
STRING(Buf)
|
||||
STRING(Buf);
|
||||
if (!SubstituteForVars(Cmd->Command.Rest, Buf)) return NULL;
|
||||
STRING(Buf)
|
||||
STRING(Buf);
|
||||
break;
|
||||
case C_QUIET:
|
||||
CHAR(_T('@'))
|
||||
RECURSE(Cmd->Subcommands)
|
||||
CHAR(_T('@'));
|
||||
RECURSE(Cmd->Subcommands);
|
||||
break;
|
||||
case C_BLOCK:
|
||||
CHAR(_T('('))
|
||||
CHAR(_T('('));
|
||||
for (Sub = Cmd->Subcommands; Sub; Sub = Sub->Next)
|
||||
{
|
||||
RECURSE(Sub)
|
||||
RECURSE(Sub);
|
||||
if (Sub->Next)
|
||||
CHAR(_T('&'))
|
||||
CHAR(_T('&'));
|
||||
}
|
||||
CHAR(_T(')'))
|
||||
CHAR(_T(')'));
|
||||
break;
|
||||
case C_MULTI:
|
||||
case C_IFFAILURE:
|
||||
case C_IFSUCCESS:
|
||||
case C_PIPE:
|
||||
Sub = Cmd->Subcommands;
|
||||
RECURSE(Sub)
|
||||
PRINTF(_T(" %s "), OpString[Cmd->Type - C_OP_LOWEST])
|
||||
RECURSE(Sub->Next)
|
||||
RECURSE(Sub);
|
||||
PRINTF(_T(" %s "), OpString[Cmd->Type - C_OP_LOWEST]);
|
||||
RECURSE(Sub->Next);
|
||||
break;
|
||||
case C_IF:
|
||||
STRING(_T("if"))
|
||||
STRING(_T("if"));
|
||||
if (Cmd->If.Flags & IFFLAG_IGNORECASE)
|
||||
STRING(_T(" /I"))
|
||||
STRING(_T(" /I"));
|
||||
if (Cmd->If.Flags & IFFLAG_NEGATE)
|
||||
STRING(_T(" not"))
|
||||
STRING(_T(" not"));
|
||||
if (Cmd->If.LeftArg && SubstituteForVars(Cmd->If.LeftArg, Buf))
|
||||
PRINTF(_T(" %s"), Buf)
|
||||
PRINTF(_T(" %s"), Buf);
|
||||
PRINTF(_T(" %s"), IfOperatorString[Cmd->If.Operator]);
|
||||
if (!SubstituteForVars(Cmd->If.RightArg, Buf)) return NULL;
|
||||
PRINTF(_T(" %s "), Buf)
|
||||
PRINTF(_T(" %s "), Buf);
|
||||
Sub = Cmd->Subcommands;
|
||||
RECURSE(Sub)
|
||||
RECURSE(Sub);
|
||||
if (Sub->Next)
|
||||
{
|
||||
STRING(_T(" else "))
|
||||
RECURSE(Sub->Next)
|
||||
STRING(_T(" else "));
|
||||
RECURSE(Sub->Next);
|
||||
}
|
||||
break;
|
||||
case C_FOR:
|
||||
STRING(_T("for"))
|
||||
if (Cmd->For.Switches & FOR_DIRS) STRING(_T(" /D"))
|
||||
if (Cmd->For.Switches & FOR_F) STRING(_T(" /F"))
|
||||
if (Cmd->For.Switches & FOR_LOOP) STRING(_T(" /L"))
|
||||
if (Cmd->For.Switches & FOR_RECURSIVE) STRING(_T(" /R"))
|
||||
STRING(_T("for"));
|
||||
if (Cmd->For.Switches & FOR_DIRS) STRING(_T(" /D"));
|
||||
if (Cmd->For.Switches & FOR_F) STRING(_T(" /F"));
|
||||
if (Cmd->For.Switches & FOR_LOOP) STRING(_T(" /L"));
|
||||
if (Cmd->For.Switches & FOR_RECURSIVE) STRING(_T(" /R"));
|
||||
if (Cmd->For.Params)
|
||||
PRINTF(_T(" %s"), Cmd->For.Params)
|
||||
PRINTF(_T(" %%%c in (%s) do "), Cmd->For.Variable, Cmd->For.List)
|
||||
RECURSE(Cmd->Subcommands)
|
||||
PRINTF(_T(" %s"), Cmd->For.Params);
|
||||
PRINTF(_T(" %%%c in (%s) do "), Cmd->For.Variable, Cmd->For.List);
|
||||
RECURSE(Cmd->Subcommands);
|
||||
break;
|
||||
}
|
||||
|
||||
for (Redir = Cmd->Redirections; Redir; Redir = Redir->Next)
|
||||
{
|
||||
if (!SubstituteForVars(Redir->Filename, Buf)) return NULL;
|
||||
if (!SubstituteForVars(Redir->Filename, Buf))
|
||||
return NULL;
|
||||
PRINTF(_T(" %c%s%s"), _T('0') + Redir->Number,
|
||||
RedirString[Redir->Mode], Buf)
|
||||
RedirString[Redir->Mode], Buf);
|
||||
}
|
||||
return Out;
|
||||
}
|
||||
|
|
|
@ -50,6 +50,12 @@ INT cmd_path (LPTSTR param)
|
|||
LPTSTR pszBuffer;
|
||||
|
||||
pszBuffer = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
|
||||
if (!pszBuffer)
|
||||
{
|
||||
WARN("Cannot allocate memory for pszBuffer!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
dwBuffer = GetEnvironmentVariable (_T("PATH"), pszBuffer, ENV_BUFFER_SIZE);
|
||||
if (dwBuffer == 0)
|
||||
{
|
||||
|
@ -61,8 +67,9 @@ INT cmd_path (LPTSTR param)
|
|||
{
|
||||
LPTSTR pszOldBuffer = pszBuffer;
|
||||
pszBuffer = (LPTSTR)cmd_realloc (pszBuffer, dwBuffer * sizeof (TCHAR));
|
||||
if (pszBuffer == NULL)
|
||||
if (!pszBuffer)
|
||||
{
|
||||
WARN("Cannot reallocate memory for pszBuffer!\n");
|
||||
cmd_free(pszOldBuffer);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -49,6 +49,7 @@ INT cmd_setlocal(LPTSTR param)
|
|||
Saved = cmd_alloc(sizeof(SETLOCAL));
|
||||
if (!Saved)
|
||||
{
|
||||
WARN("Cannot allocate memory for Saved!\n");
|
||||
error_out_of_memory();
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -178,8 +178,9 @@ INT cmd_start (LPTSTR Rest)
|
|||
|
||||
/* get comspec */
|
||||
comspec = cmd_alloc ( MAX_PATH * sizeof(TCHAR));
|
||||
if (comspec == NULL)
|
||||
if (!comspec)
|
||||
{
|
||||
WARN("Cannot allocate memory for start comspec!\n");
|
||||
error_out_of_memory();
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -149,13 +149,20 @@ SearchForExecutable (LPCTSTR pFileName, LPTSTR pFullName)
|
|||
|
||||
/* load environment variable PATHEXT */
|
||||
pszPathExt = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
|
||||
if (!pszPathExt)
|
||||
{
|
||||
WARN("Cannot allocate memory for pszPathExt!\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
dwBuffer = GetEnvironmentVariable (_T("PATHEXT"), pszPathExt, ENV_BUFFER_SIZE);
|
||||
if (dwBuffer > ENV_BUFFER_SIZE)
|
||||
{
|
||||
LPTSTR pszOldPathExt = pszPathExt;
|
||||
pszPathExt = (LPTSTR)cmd_realloc (pszPathExt, dwBuffer * sizeof (TCHAR));
|
||||
if (pszPathExt == NULL)
|
||||
if (!pszPathExt)
|
||||
{
|
||||
WARN("Cannot reallocate memory for pszPathExt!\n");
|
||||
cmd_free(pszOldPathExt);
|
||||
return FALSE;
|
||||
}
|
||||
|
@ -187,13 +194,20 @@ SearchForExecutable (LPCTSTR pFileName, LPTSTR pFullName)
|
|||
|
||||
/* load environment variable PATH into buffer */
|
||||
pszPath = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
|
||||
if (!pszPath)
|
||||
{
|
||||
WARN("Cannot allocate memory for pszPath!\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
dwBuffer = GetEnvironmentVariable (_T("PATH"), pszPath, ENV_BUFFER_SIZE);
|
||||
if (dwBuffer > ENV_BUFFER_SIZE)
|
||||
{
|
||||
LPTSTR pszOldPath = pszPath;
|
||||
pszPath = (LPTSTR)cmd_realloc (pszPath, dwBuffer * sizeof (TCHAR));
|
||||
if (pszPath == NULL)
|
||||
if (!pszPath)
|
||||
{
|
||||
WARN("Cannot reallocate memory for pszPath!\n");
|
||||
cmd_free(pszOldPath);
|
||||
cmd_free(pszPathExt);
|
||||
return FALSE;
|
||||
|
|
Loading…
Reference in a new issue