From 3f892a8d6be415cdc9ff4fda29baae3dbf8d3790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sun, 3 Dec 2017 18:49:41 +0100 Subject: [PATCH] [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. --- base/shell/cmd/alias.c | 4 ++ base/shell/cmd/assoc.c | 77 +++++++++----------- base/shell/cmd/batch.c | 19 +++-- base/shell/cmd/batch.h | 10 +-- base/shell/cmd/cmd.h | 4 +- base/shell/cmd/cmdinput.c | 8 +-- base/shell/cmd/dir.c | 8 +-- base/shell/cmd/dirstack.c | 3 +- base/shell/cmd/for.c | 8 ++- base/shell/cmd/history.c | 102 +++++++++++++++++++-------- base/shell/cmd/misc.c | 22 ++++-- base/shell/cmd/parser.c | 144 +++++++++++++++++++++++++++----------- base/shell/cmd/path.c | 9 ++- base/shell/cmd/setlocal.c | 1 + base/shell/cmd/start.c | 3 +- base/shell/cmd/where.c | 18 ++++- 16 files changed, 293 insertions(+), 147 deletions(-) diff --git a/base/shell/cmd/alias.c b/base/shell/cmd/alias.c index ac5129aef9b..52783e5970b 100644 --- a/base/shell/cmd/alias.c +++ b/base/shell/cmd/alias.c @@ -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; } diff --git a/base/shell/cmd/assoc.c b/base/shell/cmd/assoc.c index 8e3e5fc9591..0e3d6f18f9c 100644 --- a/base/shell/cmd/assoc.c +++ b/base/shell/cmd/assoc.c @@ -33,44 +33,40 @@ 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); - 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) { @@ -78,19 +74,17 @@ PrintAssociation(LPTSTR extension) 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; @@ -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,8 +109,14 @@ 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++) + for (keyCtr = 0; keyCtr < numKeys; keyCtr++) { DWORD buffer_size = extLength; return_val = RegEnumKeyEx(hKey, keyCtr, extName, &buffer_size, NULL, NULL, NULL, NULL); @@ -139,9 +136,7 @@ PrintAllAssociations() RegCloseKey(hKey); - if (extName) - cmd_free(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 @@ -237,7 +228,7 @@ INT CommandAssoc (LPTSTR param) else /* otherwise, add the key and print out the association*/ { - AddAssociation( extension, fileType); + AddAssociation(extension, fileType); PrintAssociation(extension); } @@ -248,7 +239,7 @@ INT CommandAssoc (LPTSTR param) /* 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); } } diff --git a/base/shell/cmd/batch.c b/base/shell/cmd/batch.c index 148943a9904..3c384e279af 100644 --- a/base/shell/cmd/batch.c +++ b/base/shell/cmd/batch.c @@ -110,13 +110,14 @@ LPTSTR FindArg(TCHAR Char, BOOL *IsParam0) * */ -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; } @@ -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(); @@ -234,7 +235,7 @@ void BatchFile2Mem(HANDLE hBatchFile) * 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; @@ -396,12 +397,18 @@ VOID AddBatchRedirection(REDIRECTION **RedirList) * 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 @@ -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"); diff --git a/base/shell/cmd/batch.h b/base/shell/cmd/batch.h index 53d1903f4d4..8315e4ca8e2 100644 --- a/base/shell/cmd/batch.h +++ b/base/shell/cmd/batch.h @@ -45,10 +45,10 @@ extern BOOL bEcho; /* The echo flag */ 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 **); diff --git a/base/shell/cmd/cmd.h b/base/shell/cmd/cmd.h index 6a2f680a68f..69a769ee7c2 100644 --- a/base/shell/cmd/cmd.h +++ b/base/shell/cmd/cmd.h @@ -214,12 +214,12 @@ INT cmd_goto (LPTSTR); /* 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 */ diff --git a/base/shell/cmd/cmdinput.c b/base/shell/cmd/cmdinput.c index 481a04350dc..061b9101799 100644 --- a/base/shell/cmd/cmdinput.c +++ b/base/shell/cmd/cmdinput.c @@ -245,7 +245,7 @@ BOOL ReadCommand(LPTSTR str, INT maxlen) #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'); @@ -479,7 +479,7 @@ BOOL ReadCommand(LPTSTR str, INT maxlen) #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'); @@ -503,7 +503,7 @@ BOOL ReadCommand(LPTSTR str, INT maxlen) #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); @@ -516,7 +516,7 @@ BOOL ReadCommand(LPTSTR str, INT maxlen) #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); diff --git a/base/shell/cmd/dir.c b/base/shell/cmd/dir.c index b59bf6c6c65..e6c2f869111 100644 --- a/base/shell/cmd/dir.c +++ b/base/shell/cmd/dir.c @@ -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; } diff --git a/base/shell/cmd/dirstack.c b/base/shell/cmd/dirstack.c index 9b749b62f02..c1820d87c79 100644 --- a/base/shell/cmd/dirstack.c +++ b/base/shell/cmd/dirstack.c @@ -37,7 +37,8 @@ PushDirectory (LPTSTR pszPath) 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; } diff --git a/base/shell/cmd/for.c b/base/shell/cmd/for.c index e410b23a0b0..b8628145d47 100644 --- a/base/shell/cmd/for.c +++ b/base/shell/cmd/for.c @@ -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; } diff --git a/base/shell/cmd/history.c b/base/shell/cmd/history.c index f9354c22969..e6676a7ffdd 100644 --- a/base/shell/cmd/history.c +++ b/base/shell/cmd/history.c @@ -48,16 +48,16 @@ typedef struct tagHISTORY 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); @@ -68,7 +68,7 @@ static VOID add_at_bottom(LPTSTR string); VOID set_size(INT new_size); -INT CommandHistory (LPTSTR param) +INT CommandHistory(LPTSTR param) { LPTSTR tmp; INT tmp_int; @@ -120,6 +120,8 @@ INT CommandHistory (LPTSTR param) VOID set_size(INT new_size) { + ASSERT(Top && Bottom); + while (new_sizeprev); @@ -129,10 +131,22 @@ VOID set_size(INT new_size) 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; @@ -142,13 +156,15 @@ VOID InitHistory(VOID) 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); @@ -160,14 +176,16 @@ VOID History_del_current_entry(LPTSTR str) { 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; @@ -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,8 +226,10 @@ 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) + while (size>=max_size) del(Top->prev); while (_istspace(*string)) @@ -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*/ - Bottom->string=cmd_alloc((_tcslen(string)+1)*sizeof(TCHAR)); + /*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,13 +279,17 @@ VOID add_at_bottom(LPTSTR string) 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; @@ -283,12 +323,14 @@ LPCTSTR PeekHistory(INT dir) 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; } @@ -303,9 +345,9 @@ VOID History (INT dir, LPTSTR commandline) 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 @@ -321,15 +363,15 @@ VOID History (INT dir, LPTSTR commandline) 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); } diff --git a/base/shell/cmd/misc.c b/base/shell/cmd/misc.c index a6d2f545595..bcefe215002 100644 --- a/base/shell/cmd/misc.c +++ b/base/shell/cmd/misc.c @@ -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)); diff --git a/base/shell/cmd/parser.c b/base/shell/cmd/parser.c index 821ef78d715..cdea54a8f08 100644 --- a/base/shell/cmd/parser.c +++ b/base/shell/cmd/parser.c @@ -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,8 +889,10 @@ 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); + RedirString[Redir->Mode], Buf); + } } } @@ -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; } diff --git a/base/shell/cmd/path.c b/base/shell/cmd/path.c index 3e586928608..c00851dae40 100644 --- a/base/shell/cmd/path.c +++ b/base/shell/cmd/path.c @@ -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; } diff --git a/base/shell/cmd/setlocal.c b/base/shell/cmd/setlocal.c index ae60036c771..ca913dc2a6e 100644 --- a/base/shell/cmd/setlocal.c +++ b/base/shell/cmd/setlocal.c @@ -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; } diff --git a/base/shell/cmd/start.c b/base/shell/cmd/start.c index 190ed39a6a8..de07c3e0e7e 100644 --- a/base/shell/cmd/start.c +++ b/base/shell/cmd/start.c @@ -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; } diff --git a/base/shell/cmd/where.c b/base/shell/cmd/where.c index 0e64d2aa123..0ba4c08c5ec 100644 --- a/base/shell/cmd/where.c +++ b/base/shell/cmd/where.c @@ -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;