Adapted from a patch by Jacob S. Preciado.
Bring also the code suggestions emitted during review.
/* 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;
buffer = cmd_alloc(maxlen);
if (!buffer)
{
+ WARN("Cannot allocate memory for alias buffer!\n");
cmd_free(tmp);
return;
}
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);
- if (return_val == ERROR_FILE_NOT_FOUND) /* no default value, don't display */
+ 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);
-
+ return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, (LPBYTE)fileType, &fileTypeLength);
RegCloseKey(hInsideKey);
- RegCloseKey(hKey);
if (return_val != ERROR_SUCCESS)
{
return -2;
}
- if (fileTypeLength != 0) /* if there is a default key, display relevant information */
+ if (fileTypeLength != 0) /* if there is a default key, display relevant information */
{
ConOutPrintf(_T("%s=%s\n"), extension, fileType);
}
- if (fileTypeLength)
- cmd_free(fileType);
-
+ cmd_free(fileType);
return 1;
}
static INT
-PrintAllAssociations()
+PrintAllAssociations(VOID)
{
DWORD return_val = 0;
HKEY hKey = NULL;
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);
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++)
+ for (keyCtr = 0; keyCtr < numKeys; keyCtr++)
{
DWORD buffer_size = extLength;
return_val = RegEnumKeyEx(hKey, keyCtr, extName, &buffer_size, NULL, NULL, NULL, NULL);
RegCloseKey(hKey);
- if (extName)
- cmd_free(extName);
-
+ cmd_free(extName);
return numKeys;
}
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)
{
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 */
nErrorLevel = 0;
if (_tcslen(param) == 0)
+ {
PrintAllAssociations();
+ }
else
{
LPTSTR lpEqualSign = _tcschr(param, _T('='));
{
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
else
/* otherwise, add the key and print out the association*/
{
- AddAssociation( extension, fileType);
+ AddAssociation(extension, fileType);
PrintAssociation(extension);
}
/* no equal sign, print all associations */
INT retval = PrintAssociation(param);
- if (retval == 0) /* if nothing printed out */
+ if (retval == 0) /* if nothing printed out */
ConOutResPrintf(STRING_ASSOC_ERROR, param);
}
}
*
*/
-LPTSTR BatchParams (LPTSTR s1, LPTSTR s2)
+LPTSTR BatchParams(LPTSTR s1, LPTSTR s2)
{
LPTSTR dp = (LPTSTR)cmd_alloc ((_tcslen(s1) + _tcslen(s2) + 3) * sizeof (TCHAR));
/* JPP 20-Jul-1998 added error checking */
if (dp == NULL)
{
+ WARN("Cannot allocate memory for dp!\n");
error_out_of_memory();
return NULL;
}
/*
* free the allocated memory of a batch file
*/
-VOID ClearBatch()
+VOID ClearBatch(VOID)
{
TRACE ("ClearBatch mem = %08x free = %d\n", bc->mem, bc->memfree);
* message
*/
-VOID ExitBatch()
+VOID ExitBatch(VOID)
{
ClearBatch();
* The firstword parameter is the full filename of the batch file.
*
*/
-INT Batch (LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
+INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
{
BATCH_CONTEXT new;
LPFOR_CONTEXT saved_fc;
* Read a single line from the batch file from the current batch/memory position.
* Almost a copy of FileGetString with same UNICODE handling
*/
-BOOL BatchGetString (LPTSTR lpBuffer, INT nBufferLength)
+BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength)
{
LPSTR lpString;
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
*
* Set eflag to 0 if line is not to be echoed else 1
*/
-LPTSTR ReadBatchLine ()
+LPTSTR ReadBatchLine(VOID)
{
TRACE ("ReadBatchLine ()\n");
extern TCHAR textline[BATCH_BUFFSIZE]; /* Buffer for reading Batch file lines */
-LPTSTR FindArg (TCHAR, BOOL *);
-LPTSTR BatchParams (LPTSTR, LPTSTR);
-VOID ExitBatch (VOID);
-INT Batch (LPTSTR, LPTSTR, LPTSTR, PARSED_COMMAND *);
-BOOL BatchGetString (LPTSTR lpBuffer, INT nBufferLength);
+LPTSTR FindArg(TCHAR, BOOL *);
+LPTSTR BatchParams(LPTSTR, LPTSTR);
+VOID ExitBatch(VOID);
+INT Batch(LPTSTR, LPTSTR, LPTSTR, PARSED_COMMAND *);
+BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength);
LPTSTR ReadBatchLine(VOID);
VOID AddBatchRedirection(REDIRECTION **);
/* Prototypes for HISTORY.C */
#ifdef FEATURE_HISTORY
LPCTSTR PeekHistory(INT);
-VOID History (INT, LPTSTR);/*add entries browse history*/
+VOID History(INT, LPTSTR);/*add entries browse history*/
VOID History_move_to_bottom(VOID);/*F3*/
VOID InitHistory(VOID);
VOID CleanHistory(VOID);
VOID History_del_current_entry(LPTSTR str);/*CTRL-D*/
-INT CommandHistory (LPTSTR param);
+INT CommandHistory(LPTSTR param);
#endif
/* Prototypes for IF.C */
#ifdef FEATURE_HISTORY
/* add to the history */
if (str[0])
- History (0, str);
+ History(0, str);
#endif /*FEATURE_HISTORY*/
str[charcount++] = _T('\n');
str[charcount] = _T('\0');
#ifdef FEATURE_HISTORY
/* add to the history */
if (str[0])
- History (0, str);
+ History(0, str);
#endif
str[charcount++] = _T('\n');
str[charcount] = _T('\0');
#ifdef FEATURE_HISTORY
/* get previous command from buffer */
ClearCommandLine (str, maxlen, orgx, orgy);
- History (-1, str);
+ History(-1, str);
current = charcount = _tcslen (str);
if (((charcount + orgx) / maxx) + orgy > maxy - 1)
orgy += maxy - ((charcount + orgx) / maxx + orgy + 1);
#ifdef FEATURE_HISTORY
/* get next command from buffer */
ClearCommandLine (str, maxlen, orgx, orgy);
- History (1, str);
+ History(1, str);
current = charcount = _tcslen (str);
if (((charcount + orgx) / maxx) + orgy > maxy - 1)
orgy += maxy - ((charcount + orgx) / maxx + orgy + 1);
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;
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;
*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);
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;
}
LPDIRENTRY lpDir = cmd_alloc(FIELD_OFFSET(DIRENTRY, szPath[_tcslen(pszPath) + 1]));
if (!lpDir)
{
- error_out_of_memory ();
+ WARN("Cannot allocate memory for lpDir\n");
+ error_out_of_memory();
return -1;
}
{
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))
{
Contents = cmd_realloc(Contents, (AllocLen *= 2) * sizeof(TCHAR));
if (!Contents)
{
+ WARN("Cannot reallocate memory for Contents!\n");
cmd_free(OldContents);
return NULL;
}
return Ret;
}
-BOOL
+INT
ExecuteFor(PARSED_COMMAND *Cmd)
{
TCHAR Buffer[CMDLINE_LENGTH]; /* Buffer to hold the variable value */
lpNew = cmd_alloc(sizeof(FOR_CONTEXT));
if (!lpNew)
{
+ WARN("Cannot allocate memory for lpNew!\n");
cmd_free(List);
return 1;
}
LPTSTR string;
} HIST_ENTRY, * LPHIST_ENTRY;
-static INT size, max_size=100;
+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);
-VOID History (INT dir, LPTSTR commandline);
+VOID History(INT dir, LPTSTR commandline);
VOID CleanHistory(VOID);
VOID History_del_current_entry(LPTSTR str);
VOID set_size(INT new_size);
-INT CommandHistory (LPTSTR param)
+INT CommandHistory(LPTSTR param)
{
LPTSTR tmp;
INT tmp_int;
VOID set_size(INT new_size)
{
+ ASSERT(Top && Bottom);
+
while (new_size<size)
del(Top->prev);
VOID InitHistory(VOID)
{
- size=0;
+ 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;
Bottom->next = Top;
Bottom->string = NULL;
- curr_ptr=Bottom;
+ curr_ptr = Bottom;
}
VOID CleanHistory(VOID)
{
- while (Bottom->next!=Top)
+ ASSERT(Top && Bottom);
+
+ while (Bottom->next != Top)
del(Bottom->next);
cmd_free(Top);
{
LPHIST_ENTRY tmp;
+ ASSERT(Top && Bottom);
+
if (size == 0)
return;
if (curr_ptr == Bottom)
- curr_ptr=Bottom->next;
+ curr_ptr = Bottom->next;
if (curr_ptr == Top)
- curr_ptr=Top->prev;
+ curr_ptr = Top->prev;
tmp = curr_ptr;
static
VOID del(LPHIST_ENTRY item)
{
+ ASSERT(Top && Bottom);
+
if (item==NULL || item==Top || item==Bottom)
{
TRACE ("del in " __FILE__ ": returning\n"
{
LPHIST_ENTRY tmp;
+ ASSERT(Top && Bottom);
+
/*delete first entry if maximum number of entries is reached*/
- while(size>=max_size)
+ while (size>=max_size)
del(Top->prev);
while (_istspace(*string))
/*if new entry is the same than the last do not add it*/
if (size)
+ {
if (_tcscmp(string,Bottom->next->string)==0)
return;
+ }
+
+ /*create new empty Bottom*/
+ tmp = cmd_alloc(sizeof(HIST_ENTRY));
+ if (!tmp)
+ {
+ WARN("Cannot allocate memory for new Bottom!\n");
+ return;
+ }
- /*fill bottom with string, it will become Bottom->next*/
- Bottom->string=cmd_alloc((_tcslen(string)+1)*sizeof(TCHAR));
+ /*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++;
VOID History_move_to_bottom(VOID)
{
- curr_ptr=Bottom;
+ ASSERT(Top && Bottom);
+
+ curr_ptr = Bottom;
}
LPCTSTR PeekHistory(INT dir)
{
LPHIST_ENTRY entry = curr_ptr;
+ ASSERT(Top && Bottom);
+
if (dir == 0)
return NULL;
return entry->string;
}
-VOID History (INT dir, LPTSTR commandline)
+VOID History(INT dir, LPTSTR commandline)
{
+ ASSERT(Top && Bottom);
+
if (dir==0)
{
add_at_bottom(commandline);
- curr_ptr=Bottom;
+ curr_ptr = Bottom;
return;
}
if (curr_ptr->next==Top || curr_ptr==Top)
{
#ifdef WRAP_HISTORY
- curr_ptr=Bottom;
+ curr_ptr = Bottom;
#else
- curr_ptr=Top;
+ curr_ptr = Top;
commandline[0]=_T('\0');
return;
#endif
if (curr_ptr->prev==Bottom || curr_ptr==Bottom)
{
#ifdef WRAP_HISTORY
- curr_ptr=Top;
+ curr_ptr = Top;
#else
- curr_ptr=Bottom;
+ curr_ptr = Bottom;
commandline[0]=_T('\0');
return;
#endif
}
- curr_ptr=curr_ptr->prev;
+ curr_ptr = curr_ptr->prev;
if (curr_ptr->string)
_tcscpy(commandline,curr_ptr->string);
}
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;
}
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);
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
arg = cmd_alloc (sizeof (LPTSTR));
if (!arg)
+ {
+ WARN("Cannot allocate memory for arg!\n");
return NULL;
+ }
*arg = NULL;
ac = 0;
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));
arg = cmd_alloc (sizeof (LPTSTR));
if (!arg)
+ {
+ WARN("Cannot allocate memory for arg!\n");
return NULL;
+ }
*arg = NULL;
ac = 0;
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));
TCHAR Char;
if (bParseError)
- return CurChar = 0;
+ return (CurChar = 0);
restart:
/*
}
}
}
- return CurChar = Char;
+ return (CurChar = Char);
}
static void ParseError(void)
}
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;
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;
/* 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;
*/
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;
*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;
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,
}
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;
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);
+ RedirString[Redir->Mode], Buf);
+ }
}
}
* 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)
{
* 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;
}
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)
{
{
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;
}
Saved = cmd_alloc(sizeof(SETLOCAL));
if (!Saved)
{
+ WARN("Cannot allocate memory for Saved!\n");
error_out_of_memory();
return 1;
}
/* 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;
}
/* 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;
}
/* 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;