Reactos

[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.

+293 -147
+4
base/shell/cmd/alias.c
··· 64 64 /* allocate memory for an extra \0 char to make parsing easier */ 65 65 ptr = cmd_alloc(len + sizeof(TCHAR)); 66 66 if (!ptr) 67 + { 68 + WARN("Cannot allocate memory for ptr!\n"); 67 69 return; 70 + } 68 71 69 72 Aliases = ptr; 70 73 ··· 107 110 buffer = cmd_alloc(maxlen); 108 111 if (!buffer) 109 112 { 113 + WARN("Cannot allocate memory for alias buffer!\n"); 110 114 cmd_free(tmp); 111 115 return; 112 116 }
+34 -43
base/shell/cmd/assoc.c
··· 33 33 return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey); 34 34 35 35 if (return_val != ERROR_SUCCESS) 36 - { 37 - RegCloseKey(hKey); 38 36 return -1; 39 - } 40 37 41 38 return_val = RegOpenKeyEx(hKey, extension, 0, KEY_READ, &hInsideKey); 39 + RegCloseKey(hKey); 42 40 43 41 if (return_val != ERROR_SUCCESS) 44 - { 45 - RegCloseKey(hKey); 46 - RegCloseKey(hInsideKey); 47 42 return 0; 48 - } 49 43 50 44 /* obtain string length */ 51 45 return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, NULL, &fileTypeLength); 52 46 53 - if (return_val == ERROR_FILE_NOT_FOUND) /* no default value, don't display */ 47 + if (return_val == ERROR_FILE_NOT_FOUND) /* no default value, don't display */ 54 48 { 55 49 RegCloseKey(hInsideKey); 56 - RegCloseKey(hKey); 57 50 return 0; 58 51 } 59 52 60 53 if (return_val != ERROR_SUCCESS) 61 54 { 62 55 RegCloseKey(hInsideKey); 63 - RegCloseKey(hKey); 64 56 return -2; 65 57 } 66 58 67 59 fileType = cmd_alloc(fileTypeLength * sizeof(TCHAR)); 60 + if (!fileType) 61 + { 62 + WARN("Cannot allocate memory for fileType!\n"); 63 + RegCloseKey(hInsideKey); 64 + return -2; 65 + } 68 66 69 67 /* obtain actual file type */ 70 - return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, (LPBYTE) fileType, &fileTypeLength); 71 - 68 + return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, (LPBYTE)fileType, &fileTypeLength); 72 69 RegCloseKey(hInsideKey); 73 - RegCloseKey(hKey); 74 70 75 71 if (return_val != ERROR_SUCCESS) 76 72 { ··· 78 74 return -2; 79 75 } 80 76 81 - if (fileTypeLength != 0) /* if there is a default key, display relevant information */ 77 + if (fileTypeLength != 0) /* if there is a default key, display relevant information */ 82 78 { 83 79 ConOutPrintf(_T("%s=%s\n"), extension, fileType); 84 80 } 85 81 86 - if (fileTypeLength) 87 - cmd_free(fileType); 88 - 82 + cmd_free(fileType); 89 83 return 1; 90 84 } 91 85 92 86 static INT 93 - PrintAllAssociations() 87 + PrintAllAssociations(VOID) 94 88 { 95 89 DWORD return_val = 0; 96 90 HKEY hKey = NULL; ··· 103 97 return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey); 104 98 105 99 if (return_val != ERROR_SUCCESS) 106 - { 107 - RegCloseKey(hKey); 108 100 return -1; 109 - } 110 101 111 102 return_val = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numKeys, &extLength, NULL, NULL, NULL, NULL, NULL, NULL); 112 103 ··· 118 109 119 110 extLength++; 120 111 extName = cmd_alloc(extLength * sizeof(TCHAR)); 112 + if (!extName) 113 + { 114 + WARN("Cannot allocate memory for extName!\n"); 115 + RegCloseKey(hKey); 116 + return -2; 117 + } 121 118 122 - for(keyCtr = 0; keyCtr < numKeys; keyCtr++) 119 + for (keyCtr = 0; keyCtr < numKeys; keyCtr++) 123 120 { 124 121 DWORD buffer_size = extLength; 125 122 return_val = RegEnumKeyEx(hKey, keyCtr, extName, &buffer_size, NULL, NULL, NULL, NULL); ··· 139 136 140 137 RegCloseKey(hKey); 141 138 142 - if (extName) 143 - cmd_free(extName); 144 - 139 + cmd_free(extName); 145 140 return numKeys; 146 141 } 147 142 ··· 157 152 return -1; 158 153 159 154 return_val = RegCreateKeyEx(hKey, extension, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &insideKey, NULL); 155 + RegCloseKey(hKey); 160 156 161 157 if (return_val != ERROR_SUCCESS) 162 - { 163 - RegCloseKey(hKey); 164 158 return -1; 165 - } 166 159 167 160 return_val = RegSetValueEx(insideKey, NULL, 0, REG_SZ, (LPBYTE)type, (_tcslen(type) + 1) * sizeof(TCHAR)); 161 + RegCloseKey(insideKey); 168 162 169 163 if (return_val != ERROR_SUCCESS) 170 - { 171 - RegCloseKey(insideKey); 172 - RegCloseKey(hKey); 173 164 return -2; 174 - } 175 165 176 - RegCloseKey(insideKey); 177 - RegCloseKey(hKey); 178 166 return 0; 179 167 } 180 - 181 168 182 169 static int 183 170 RemoveAssociation(LPTSTR extension) ··· 191 178 return -1; 192 179 193 180 return_val = RegDeleteKey(hKey, extension); 181 + RegCloseKey(hKey); 194 182 195 183 if (return_val != ERROR_SUCCESS) 196 - { 197 - RegCloseKey(hKey); 198 184 return -2; 199 - } 200 185 201 - RegCloseKey(hKey); 202 186 return 0; 203 187 } 204 - 205 188 206 189 207 190 INT CommandAssoc (LPTSTR param) ··· 216 199 nErrorLevel = 0; 217 200 218 201 if (_tcslen(param) == 0) 202 + { 219 203 PrintAllAssociations(); 204 + } 220 205 else 221 206 { 222 207 LPTSTR lpEqualSign = _tcschr(param, _T('=')); ··· 224 209 { 225 210 LPTSTR fileType = lpEqualSign + 1; 226 211 LPTSTR extension = cmd_alloc((lpEqualSign - param + 1) * sizeof(TCHAR)); 212 + if (!extension) 213 + { 214 + WARN("Cannot allocate memory for extension!\n"); 215 + error_out_of_memory(); 216 + return 1; 217 + } 227 218 228 219 _tcsncpy(extension, param, lpEqualSign - param); 229 - extension[lpEqualSign - param] = (TCHAR)0; 220 + extension[lpEqualSign - param] = _T('\0'); 230 221 231 222 if (_tcslen(fileType) == 0) 232 223 /* if the equal sign is the last character ··· 237 228 else 238 229 /* otherwise, add the key and print out the association*/ 239 230 { 240 - AddAssociation( extension, fileType); 231 + AddAssociation(extension, fileType); 241 232 PrintAssociation(extension); 242 233 } 243 234 ··· 248 239 /* no equal sign, print all associations */ 249 240 INT retval = PrintAssociation(param); 250 241 251 - if (retval == 0) /* if nothing printed out */ 242 + if (retval == 0) /* if nothing printed out */ 252 243 ConOutResPrintf(STRING_ASSOC_ERROR, param); 253 244 } 254 245 }
+13 -6
base/shell/cmd/batch.c
··· 110 110 * 111 111 */ 112 112 113 - LPTSTR BatchParams (LPTSTR s1, LPTSTR s2) 113 + LPTSTR BatchParams(LPTSTR s1, LPTSTR s2) 114 114 { 115 115 LPTSTR dp = (LPTSTR)cmd_alloc ((_tcslen(s1) + _tcslen(s2) + 3) * sizeof (TCHAR)); 116 116 117 117 /* JPP 20-Jul-1998 added error checking */ 118 118 if (dp == NULL) 119 119 { 120 + WARN("Cannot allocate memory for dp!\n"); 120 121 error_out_of_memory(); 121 122 return NULL; 122 123 } ··· 158 159 /* 159 160 * free the allocated memory of a batch file 160 161 */ 161 - VOID ClearBatch() 162 + VOID ClearBatch(VOID) 162 163 { 163 164 TRACE ("ClearBatch mem = %08x free = %d\n", bc->mem, bc->memfree); 164 165 ··· 182 183 * message 183 184 */ 184 185 185 - VOID ExitBatch() 186 + VOID ExitBatch(VOID) 186 187 { 187 188 ClearBatch(); 188 189 ··· 234 235 * The firstword parameter is the full filename of the batch file. 235 236 * 236 237 */ 237 - INT Batch (LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd) 238 + INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd) 238 239 { 239 240 BATCH_CONTEXT new; 240 241 LPFOR_CONTEXT saved_fc; ··· 396 397 * Read a single line from the batch file from the current batch/memory position. 397 398 * Almost a copy of FileGetString with same UNICODE handling 398 399 */ 399 - BOOL BatchGetString (LPTSTR lpBuffer, INT nBufferLength) 400 + BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength) 400 401 { 401 402 LPSTR lpString; 402 403 INT len = 0; 403 404 #ifdef _UNICODE 404 405 lpString = cmd_alloc(nBufferLength); 406 + if (!lpString) 407 + { 408 + WARN("Cannot allocate memory for lpString\n"); 409 + error_out_of_memory(); 410 + return FALSE; 411 + } 405 412 #else 406 413 lpString = lpBuffer; 407 414 #endif ··· 443 450 * 444 451 * Set eflag to 0 if line is not to be echoed else 1 445 452 */ 446 - LPTSTR ReadBatchLine () 453 + LPTSTR ReadBatchLine(VOID) 447 454 { 448 455 TRACE ("ReadBatchLine ()\n"); 449 456
+5 -5
base/shell/cmd/batch.h
··· 45 45 extern TCHAR textline[BATCH_BUFFSIZE]; /* Buffer for reading Batch file lines */ 46 46 47 47 48 - LPTSTR FindArg (TCHAR, BOOL *); 49 - LPTSTR BatchParams (LPTSTR, LPTSTR); 50 - VOID ExitBatch (VOID); 51 - INT Batch (LPTSTR, LPTSTR, LPTSTR, PARSED_COMMAND *); 52 - BOOL BatchGetString (LPTSTR lpBuffer, INT nBufferLength); 48 + LPTSTR FindArg(TCHAR, BOOL *); 49 + LPTSTR BatchParams(LPTSTR, LPTSTR); 50 + VOID ExitBatch(VOID); 51 + INT Batch(LPTSTR, LPTSTR, LPTSTR, PARSED_COMMAND *); 52 + BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength); 53 53 LPTSTR ReadBatchLine(VOID); 54 54 VOID AddBatchRedirection(REDIRECTION **);
+2 -2
base/shell/cmd/cmd.h
··· 214 214 /* Prototypes for HISTORY.C */ 215 215 #ifdef FEATURE_HISTORY 216 216 LPCTSTR PeekHistory(INT); 217 - VOID History (INT, LPTSTR);/*add entries browse history*/ 217 + VOID History(INT, LPTSTR);/*add entries browse history*/ 218 218 VOID History_move_to_bottom(VOID);/*F3*/ 219 219 VOID InitHistory(VOID); 220 220 VOID CleanHistory(VOID); 221 221 VOID History_del_current_entry(LPTSTR str);/*CTRL-D*/ 222 - INT CommandHistory (LPTSTR param); 222 + INT CommandHistory(LPTSTR param); 223 223 #endif 224 224 225 225 /* Prototypes for IF.C */
+4 -4
base/shell/cmd/cmdinput.c
··· 245 245 #ifdef FEATURE_HISTORY 246 246 /* add to the history */ 247 247 if (str[0]) 248 - History (0, str); 248 + History(0, str); 249 249 #endif /*FEATURE_HISTORY*/ 250 250 str[charcount++] = _T('\n'); 251 251 str[charcount] = _T('\0'); ··· 479 479 #ifdef FEATURE_HISTORY 480 480 /* add to the history */ 481 481 if (str[0]) 482 - History (0, str); 482 + History(0, str); 483 483 #endif 484 484 str[charcount++] = _T('\n'); 485 485 str[charcount] = _T('\0'); ··· 503 503 #ifdef FEATURE_HISTORY 504 504 /* get previous command from buffer */ 505 505 ClearCommandLine (str, maxlen, orgx, orgy); 506 - History (-1, str); 506 + History(-1, str); 507 507 current = charcount = _tcslen (str); 508 508 if (((charcount + orgx) / maxx) + orgy > maxy - 1) 509 509 orgy += maxy - ((charcount + orgx) / maxx + orgy + 1); ··· 516 516 #ifdef FEATURE_HISTORY 517 517 /* get next command from buffer */ 518 518 ClearCommandLine (str, maxlen, orgx, orgy); 519 - History (1, str); 519 + History(1, str); 520 520 current = charcount = _tcslen (str); 521 521 if (((charcount + orgx) / maxx) + orgy > maxy - 1) 522 522 orgy += maxy - ((charcount + orgx) / maxx + orgy + 1);
+4 -4
base/shell/cmd/dir.c
··· 1390 1390 ptrStartNode = cmd_alloc(sizeof(DIRFINDLISTNODE)); 1391 1391 if (ptrStartNode == NULL) 1392 1392 { 1393 - WARN("DEBUG: Cannot allocate memory for ptrStartNode!\n"); 1393 + WARN("Cannot allocate memory for ptrStartNode!\n"); 1394 1394 return 1; /* Error cannot allocate memory for 1st object */ 1395 1395 } 1396 1396 ptrStartNode->stInfo.ptrHead = NULL; ··· 1408 1408 ptrNextNode->ptrNext = cmd_alloc(sizeof(DIRFINDLISTNODE)); 1409 1409 if (ptrNextNode->ptrNext == NULL) 1410 1410 { 1411 - WARN("DEBUG: Cannot allocate memory for ptrNextNode->ptrNext!\n"); 1411 + WARN("Cannot allocate memory for ptrNextNode->ptrNext!\n"); 1412 1412 DirNodeCleanup(ptrStartNode, &dwCount); 1413 1413 FindClose(hSearch); 1414 1414 return 1; ··· 1458 1458 *ptrCurNode = cmd_alloc(sizeof(DIRFINDSTREAMNODE)); 1459 1459 if (*ptrCurNode == NULL) 1460 1460 { 1461 - WARN("DEBUG: Cannot allocate memory for *ptrCurNode!\n"); 1461 + WARN("Cannot allocate memory for *ptrCurNode!\n"); 1462 1462 DirNodeCleanup(ptrStartNode, &dwCount); 1463 1463 FindClose(hStreams); 1464 1464 FindClose(hSearch); ··· 1512 1512 ptrFileArray = cmd_alloc(sizeof(PDIRFINDINFO) * dwCount); 1513 1513 if (ptrFileArray == NULL) 1514 1514 { 1515 - WARN("DEBUG: Cannot allocate memory for ptrFileArray!\n"); 1515 + WARN("Cannot allocate memory for ptrFileArray!\n"); 1516 1516 DirNodeCleanup(ptrStartNode, &dwCount); 1517 1517 return 1; 1518 1518 }
+2 -1
base/shell/cmd/dirstack.c
··· 37 37 LPDIRENTRY lpDir = cmd_alloc(FIELD_OFFSET(DIRENTRY, szPath[_tcslen(pszPath) + 1])); 38 38 if (!lpDir) 39 39 { 40 - error_out_of_memory (); 40 + WARN("Cannot allocate memory for lpDir\n"); 41 + error_out_of_memory(); 41 42 return -1; 42 43 } 43 44
+7 -1
base/shell/cmd/for.c
··· 96 96 { 97 97 SIZE_T Len = 0; 98 98 SIZE_T AllocLen = 1000; 99 + 99 100 LPTSTR Contents = cmd_alloc(AllocLen * sizeof(TCHAR)); 100 101 if (!Contents) 102 + { 103 + WARN("Cannot allocate memory for Contents!\n"); 101 104 return NULL; 105 + } 102 106 103 107 while (_fgetts(Buffer, CMDLINE_LENGTH, InputFile)) 104 108 { ··· 109 113 Contents = cmd_realloc(Contents, (AllocLen *= 2) * sizeof(TCHAR)); 110 114 if (!Contents) 111 115 { 116 + WARN("Cannot reallocate memory for Contents!\n"); 112 117 cmd_free(OldContents); 113 118 return NULL; 114 119 } ··· 454 459 return Ret; 455 460 } 456 461 457 - BOOL 462 + INT 458 463 ExecuteFor(PARSED_COMMAND *Cmd) 459 464 { 460 465 TCHAR Buffer[CMDLINE_LENGTH]; /* Buffer to hold the variable value */ ··· 470 475 lpNew = cmd_alloc(sizeof(FOR_CONTEXT)); 471 476 if (!lpNew) 472 477 { 478 + WARN("Cannot allocate memory for lpNew!\n"); 473 479 cmd_free(List); 474 480 return 1; 475 481 }
+72 -30
base/shell/cmd/history.c
··· 48 48 LPTSTR string; 49 49 } HIST_ENTRY, * LPHIST_ENTRY; 50 50 51 - static INT size, max_size=100; 51 + static INT size, max_size = 100; 52 52 53 - static LPHIST_ENTRY Top; 54 - static LPHIST_ENTRY Bottom; 53 + static LPHIST_ENTRY Top = NULL; 54 + static LPHIST_ENTRY Bottom = NULL; 55 55 56 - static LPHIST_ENTRY curr_ptr=0; 56 + static LPHIST_ENTRY curr_ptr = NULL; 57 57 58 58 VOID InitHistory(VOID); 59 59 VOID History_move_to_bottom(VOID); 60 - VOID History (INT dir, LPTSTR commandline); 60 + VOID History(INT dir, LPTSTR commandline); 61 61 VOID CleanHistory(VOID); 62 62 VOID History_del_current_entry(LPTSTR str); 63 63 ··· 68 68 VOID set_size(INT new_size); 69 69 70 70 71 - INT CommandHistory (LPTSTR param) 71 + INT CommandHistory(LPTSTR param) 72 72 { 73 73 LPTSTR tmp; 74 74 INT tmp_int; ··· 120 120 121 121 VOID set_size(INT new_size) 122 122 { 123 + ASSERT(Top && Bottom); 124 + 123 125 while (new_size<size) 124 126 del(Top->prev); 125 127 ··· 129 131 130 132 VOID InitHistory(VOID) 131 133 { 132 - size=0; 134 + size = 0; 133 135 134 136 Top = cmd_alloc(sizeof(HIST_ENTRY)); 137 + if (!Top) 138 + { 139 + WARN("Cannot allocate memory for Top!\n"); 140 + return; 141 + } 135 142 Bottom = cmd_alloc(sizeof(HIST_ENTRY)); 143 + if (!Bottom) 144 + { 145 + WARN("Cannot allocate memory for Bottom!\n"); 146 + cmd_free(Top); 147 + Top = NULL; 148 + return; 149 + } 136 150 137 151 Top->prev = Bottom; 138 152 Top->next = NULL; ··· 142 156 Bottom->next = Top; 143 157 Bottom->string = NULL; 144 158 145 - curr_ptr=Bottom; 159 + curr_ptr = Bottom; 146 160 } 147 161 148 162 149 163 VOID CleanHistory(VOID) 150 164 { 151 - while (Bottom->next!=Top) 165 + ASSERT(Top && Bottom); 166 + 167 + while (Bottom->next != Top) 152 168 del(Bottom->next); 153 169 154 170 cmd_free(Top); ··· 160 176 { 161 177 LPHIST_ENTRY tmp; 162 178 179 + ASSERT(Top && Bottom); 180 + 163 181 if (size == 0) 164 182 return; 165 183 166 184 if (curr_ptr == Bottom) 167 - curr_ptr=Bottom->next; 185 + curr_ptr = Bottom->next; 168 186 169 187 if (curr_ptr == Top) 170 - curr_ptr=Top->prev; 188 + curr_ptr = Top->prev; 171 189 172 190 173 191 tmp = curr_ptr; ··· 180 198 static 181 199 VOID del(LPHIST_ENTRY item) 182 200 { 201 + ASSERT(Top && Bottom); 202 + 183 203 if (item==NULL || item==Top || item==Bottom) 184 204 { 185 205 TRACE ("del in " __FILE__ ": returning\n" ··· 206 226 { 207 227 LPHIST_ENTRY tmp; 208 228 229 + ASSERT(Top && Bottom); 230 + 209 231 /*delete first entry if maximum number of entries is reached*/ 210 - while(size>=max_size) 232 + while (size>=max_size) 211 233 del(Top->prev); 212 234 213 235 while (_istspace(*string)) ··· 218 240 219 241 /*if new entry is the same than the last do not add it*/ 220 242 if (size) 243 + { 221 244 if (_tcscmp(string,Bottom->next->string)==0) 222 245 return; 246 + } 223 247 224 - /*fill bottom with string, it will become Bottom->next*/ 225 - Bottom->string=cmd_alloc((_tcslen(string)+1)*sizeof(TCHAR)); 248 + /*create new empty Bottom*/ 249 + tmp = cmd_alloc(sizeof(HIST_ENTRY)); 250 + if (!tmp) 251 + { 252 + WARN("Cannot allocate memory for new Bottom!\n"); 253 + return; 254 + } 255 + 256 + /*fill old bottom with string, it will become new Bottom->next*/ 257 + Bottom->string = cmd_alloc((_tcslen(string)+1)*sizeof(TCHAR)); 258 + if (!Bottom->string) 259 + { 260 + WARN("Cannot allocate memory for Bottom->string!\n"); 261 + cmd_free(tmp); 262 + return; 263 + } 226 264 _tcscpy(Bottom->string,string); 227 265 228 - /*save Bottom value*/ 229 - tmp=Bottom; 266 + tmp->next = Bottom; 267 + tmp->prev = NULL; 268 + tmp->string = NULL; 230 269 231 - /*create new void Bottom*/ 232 - Bottom=cmd_alloc(sizeof(HIST_ENTRY)); 233 - Bottom->next=tmp; 234 - Bottom->prev=NULL; 235 - Bottom->string=NULL; 270 + Bottom->prev = tmp; 236 271 237 - tmp->prev=Bottom; 272 + /*save the new Bottom value*/ 273 + Bottom = tmp; 238 274 239 275 /*set new size*/ 240 276 size++; ··· 243 279 244 280 VOID History_move_to_bottom(VOID) 245 281 { 246 - curr_ptr=Bottom; 282 + ASSERT(Top && Bottom); 283 + 284 + curr_ptr = Bottom; 247 285 } 248 286 249 287 LPCTSTR PeekHistory(INT dir) 250 288 { 251 289 LPHIST_ENTRY entry = curr_ptr; 290 + 291 + ASSERT(Top && Bottom); 252 292 253 293 if (dir == 0) 254 294 return NULL; ··· 283 323 return entry->string; 284 324 } 285 325 286 - VOID History (INT dir, LPTSTR commandline) 326 + VOID History(INT dir, LPTSTR commandline) 287 327 { 328 + ASSERT(Top && Bottom); 329 + 288 330 if (dir==0) 289 331 { 290 332 add_at_bottom(commandline); 291 - curr_ptr=Bottom; 333 + curr_ptr = Bottom; 292 334 return; 293 335 } 294 336 ··· 303 345 if (curr_ptr->next==Top || curr_ptr==Top) 304 346 { 305 347 #ifdef WRAP_HISTORY 306 - curr_ptr=Bottom; 348 + curr_ptr = Bottom; 307 349 #else 308 - curr_ptr=Top; 350 + curr_ptr = Top; 309 351 commandline[0]=_T('\0'); 310 352 return; 311 353 #endif ··· 321 363 if (curr_ptr->prev==Bottom || curr_ptr==Bottom) 322 364 { 323 365 #ifdef WRAP_HISTORY 324 - curr_ptr=Top; 366 + curr_ptr = Top; 325 367 #else 326 - curr_ptr=Bottom; 368 + curr_ptr = Bottom; 327 369 commandline[0]=_T('\0'); 328 370 return; 329 371 #endif 330 372 } 331 373 332 - curr_ptr=curr_ptr->prev; 374 + curr_ptr = curr_ptr->prev; 333 375 if (curr_ptr->string) 334 376 _tcscpy(commandline,curr_ptr->string); 335 377 }
+17 -5
base/shell/cmd/misc.c
··· 188 188 LPTSTR *oldarg; 189 189 190 190 q = cmd_alloc ((_tcslen(entry) + 1) * sizeof (TCHAR)); 191 - if (NULL == q) 191 + if (!q) 192 192 { 193 + WARN("Cannot allocate memory for q!\n"); 193 194 return FALSE; 194 195 } 195 196 196 197 _tcscpy (q, entry); 197 198 oldarg = *arg; 198 199 *arg = cmd_realloc (oldarg, (*ac + 2) * sizeof (LPTSTR)); 199 - if (NULL == *arg) 200 + if (!*arg) 200 201 { 201 - cmd_free (q); 202 + WARN("Cannot reallocate memory for arg!\n"); 202 203 *arg = oldarg; 204 + cmd_free (q); 203 205 return FALSE; 204 206 } 205 207 ··· 222 224 if (NULL != pathend) 223 225 { 224 226 dirpart = cmd_alloc((pathend - pattern + 2) * sizeof(TCHAR)); 225 - if (NULL == dirpart) 227 + if (!dirpart) 226 228 { 229 + WARN("Cannot allocate memory for dirpart!\n"); 227 230 return FALSE; 228 231 } 229 232 memcpy(dirpart, pattern, pathend - pattern + 1); ··· 241 244 if (NULL != dirpart) 242 245 { 243 246 fullname = cmd_alloc((_tcslen(dirpart) + _tcslen(FindData.cFileName) + 1) * sizeof(TCHAR)); 244 - if (NULL == fullname) 247 + if (!fullname) 245 248 { 249 + WARN("Cannot allocate memory for fullname!\n"); 246 250 ok = FALSE; 247 251 } 248 252 else ··· 286 290 287 291 arg = cmd_alloc (sizeof (LPTSTR)); 288 292 if (!arg) 293 + { 294 + WARN("Cannot allocate memory for arg!\n"); 289 295 return NULL; 296 + } 290 297 *arg = NULL; 291 298 292 299 ac = 0; ··· 333 340 q = cmd_alloc (((len = s - start) + 1) * sizeof (TCHAR)); 334 341 if (!q) 335 342 { 343 + WARN("Cannot allocate memory for q!\n"); 336 344 return NULL; 337 345 } 338 346 memcpy (q, start, len * sizeof (TCHAR)); ··· 381 389 382 390 arg = cmd_alloc (sizeof (LPTSTR)); 383 391 if (!arg) 392 + { 393 + WARN("Cannot allocate memory for arg!\n"); 384 394 return NULL; 395 + } 385 396 *arg = NULL; 386 397 387 398 ac = 0; ··· 409 420 q = cmd_alloc (((len = s - start) + 1) * sizeof (TCHAR)); 410 421 if (!q) 411 422 { 423 + WARN("Cannot allocate memory for q!\n"); 412 424 return NULL; 413 425 } 414 426 memcpy (q, start, len * sizeof (TCHAR));
+102 -42
base/shell/cmd/parser.c
··· 60 60 TCHAR Char; 61 61 62 62 if (bParseError) 63 - return CurChar = 0; 63 + return (CurChar = 0); 64 64 65 65 restart: 66 66 /* ··· 90 90 } 91 91 } 92 92 } 93 - return CurChar = Char; 93 + return (CurChar = Char); 94 94 } 95 95 96 96 static void ParseError(void) ··· 272 272 } 273 273 274 274 Redir = cmd_alloc(FIELD_OFFSET(REDIRECTION, Filename[_tcslen(Tok) + 1])); 275 + if (!Redir) 276 + { 277 + WARN("Cannot allocate memory for Redir!\n"); 278 + goto fail; 279 + } 275 280 Redir->Next = NULL; 276 281 Redir->OldHandle = INVALID_HANDLE_VALUE; 277 282 Redir->Number = Number; ··· 293 298 static PARSED_COMMAND *ParseBlock(REDIRECTION *RedirList) 294 299 { 295 300 PARSED_COMMAND *Cmd, *Sub, **NextPtr; 301 + 296 302 Cmd = cmd_alloc(sizeof(PARSED_COMMAND)); 303 + if (!Cmd) 304 + { 305 + WARN("Cannot allocate memory for Cmd!\n"); 306 + ParseError(); 307 + FreeRedirection(RedirList); 308 + return NULL; 309 + } 297 310 Cmd->Type = C_BLOCK; 298 311 Cmd->Next = NULL; 299 312 Cmd->Subcommands = NULL; ··· 340 353 /* Parse an IF statement */ 341 354 static PARSED_COMMAND *ParseIf(void) 342 355 { 343 - PARSED_COMMAND *Cmd = cmd_alloc(sizeof(PARSED_COMMAND)); 344 356 int Type; 357 + PARSED_COMMAND *Cmd; 358 + 359 + Cmd = cmd_alloc(sizeof(PARSED_COMMAND)); 360 + if (!Cmd) 361 + { 362 + WARN("Cannot allocate memory for Cmd!\n"); 363 + ParseError(); 364 + return NULL; 365 + } 345 366 memset(Cmd, 0, sizeof(PARSED_COMMAND)); 346 367 Cmd->Type = C_IF; 347 368 ··· 432 453 */ 433 454 static PARSED_COMMAND *ParseFor(void) 434 455 { 435 - PARSED_COMMAND *Cmd = cmd_alloc(sizeof(PARSED_COMMAND)); 456 + PARSED_COMMAND *Cmd; 436 457 TCHAR List[CMDLINE_LENGTH]; 437 458 TCHAR *Pos = List; 438 459 460 + Cmd = cmd_alloc(sizeof(PARSED_COMMAND)); 461 + if (!Cmd) 462 + { 463 + WARN("Cannot allocate memory for Cmd!\n"); 464 + ParseError(); 465 + return NULL; 466 + } 439 467 memset(Cmd, 0, sizeof(PARSED_COMMAND)); 440 468 Cmd->Type = C_FOR; 441 469 ··· 609 637 *Pos++ = _T('\0'); 610 638 611 639 Cmd = cmd_alloc(FIELD_OFFSET(PARSED_COMMAND, Command.First[Pos - ParsedLine])); 640 + if (!Cmd) 641 + { 642 + WARN("Cannot allocate memory for Cmd!\n"); 643 + ParseError(); 644 + FreeRedirection(RedirList); 645 + return NULL; 646 + } 612 647 Cmd->Type = C_COMMAND; 613 648 Cmd->Next = NULL; 614 649 Cmd->Subcommands = NULL; ··· 647 682 PARSED_COMMAND *Cmd; 648 683 ParseChar(); 649 684 Cmd = cmd_alloc(sizeof(PARSED_COMMAND)); 685 + if (!Cmd) 686 + { 687 + WARN("Cannot allocate memory for Cmd!\n"); 688 + ParseError(); 689 + return NULL; 690 + } 650 691 Cmd->Type = C_QUIET; 651 692 Cmd->Next = NULL; 652 693 /* @ acts like a unary operator with low precedence, ··· 702 743 } 703 744 704 745 Cmd = cmd_alloc(sizeof(PARSED_COMMAND)); 746 + if (!Cmd) 747 + { 748 + WARN("Cannot allocate memory for Cmd!\n"); 749 + ParseError(); 750 + FreeCommand(Left); 751 + FreeCommand(Right); 752 + return NULL; 753 + } 705 754 Cmd->Type = OpType; 706 755 Cmd->Next = NULL; 707 756 Cmd->Redirections = NULL; ··· 840 889 for (Redir = Cmd->Redirections; Redir; Redir = Redir->Next) 841 890 { 842 891 if (SubstituteForVars(Redir->Filename, Buf)) 892 + { 843 893 ConOutPrintf(_T(" %c%s%s"), _T('0') + Redir->Number, 844 - RedirString[Redir->Mode], Buf); 894 + RedirString[Redir->Mode], Buf); 895 + } 845 896 } 846 897 } 847 898 ··· 862 913 * overflowing the supplied buffer, define some helper macros to make 863 914 * this less painful. 864 915 */ 865 - #define CHAR(Char) { \ 916 + #define CHAR(Char) \ 917 + do { \ 866 918 if (Out == OutEnd) return NULL; \ 867 - *Out++ = Char; } 868 - #define STRING(String) { \ 919 + *Out++ = Char; \ 920 + } while (0) 921 + #define STRING(String) \ 922 + do { \ 869 923 if (Out + _tcslen(String) > OutEnd) return NULL; \ 870 - Out = _stpcpy(Out, String); } 871 - #define PRINTF(Format, ...) { \ 924 + Out = _stpcpy(Out, String); \ 925 + } while (0) 926 + #define PRINTF(Format, ...) \ 927 + do { \ 872 928 UINT Len = _sntprintf(Out, OutEnd - Out, Format, __VA_ARGS__); \ 873 929 if (Len > (UINT)(OutEnd - Out)) return NULL; \ 874 - Out += Len; } 875 - #define RECURSE(Subcommand) { \ 930 + Out += Len; \ 931 + } while (0) 932 + #define RECURSE(Subcommand) \ 933 + do { \ 876 934 Out = Unparse(Subcommand, Out, OutEnd); \ 877 - if (!Out) return NULL; } 935 + if (!Out) return NULL; \ 936 + } while (0) 878 937 879 938 switch (Cmd->Type) 880 939 { ··· 883 942 * Windows doesn't bother escaping them, so for compatibility 884 943 * we probably shouldn't do it either */ 885 944 if (!SubstituteForVars(Cmd->Command.First, Buf)) return NULL; 886 - STRING(Buf) 945 + STRING(Buf); 887 946 if (!SubstituteForVars(Cmd->Command.Rest, Buf)) return NULL; 888 - STRING(Buf) 947 + STRING(Buf); 889 948 break; 890 949 case C_QUIET: 891 - CHAR(_T('@')) 892 - RECURSE(Cmd->Subcommands) 950 + CHAR(_T('@')); 951 + RECURSE(Cmd->Subcommands); 893 952 break; 894 953 case C_BLOCK: 895 - CHAR(_T('(')) 954 + CHAR(_T('(')); 896 955 for (Sub = Cmd->Subcommands; Sub; Sub = Sub->Next) 897 956 { 898 - RECURSE(Sub) 957 + RECURSE(Sub); 899 958 if (Sub->Next) 900 - CHAR(_T('&')) 959 + CHAR(_T('&')); 901 960 } 902 - CHAR(_T(')')) 961 + CHAR(_T(')')); 903 962 break; 904 963 case C_MULTI: 905 964 case C_IFFAILURE: 906 965 case C_IFSUCCESS: 907 966 case C_PIPE: 908 967 Sub = Cmd->Subcommands; 909 - RECURSE(Sub) 910 - PRINTF(_T(" %s "), OpString[Cmd->Type - C_OP_LOWEST]) 911 - RECURSE(Sub->Next) 968 + RECURSE(Sub); 969 + PRINTF(_T(" %s "), OpString[Cmd->Type - C_OP_LOWEST]); 970 + RECURSE(Sub->Next); 912 971 break; 913 972 case C_IF: 914 - STRING(_T("if")) 973 + STRING(_T("if")); 915 974 if (Cmd->If.Flags & IFFLAG_IGNORECASE) 916 - STRING(_T(" /I")) 975 + STRING(_T(" /I")); 917 976 if (Cmd->If.Flags & IFFLAG_NEGATE) 918 - STRING(_T(" not")) 977 + STRING(_T(" not")); 919 978 if (Cmd->If.LeftArg && SubstituteForVars(Cmd->If.LeftArg, Buf)) 920 - PRINTF(_T(" %s"), Buf) 979 + PRINTF(_T(" %s"), Buf); 921 980 PRINTF(_T(" %s"), IfOperatorString[Cmd->If.Operator]); 922 981 if (!SubstituteForVars(Cmd->If.RightArg, Buf)) return NULL; 923 - PRINTF(_T(" %s "), Buf) 982 + PRINTF(_T(" %s "), Buf); 924 983 Sub = Cmd->Subcommands; 925 - RECURSE(Sub) 984 + RECURSE(Sub); 926 985 if (Sub->Next) 927 986 { 928 - STRING(_T(" else ")) 929 - RECURSE(Sub->Next) 987 + STRING(_T(" else ")); 988 + RECURSE(Sub->Next); 930 989 } 931 990 break; 932 991 case C_FOR: 933 - STRING(_T("for")) 934 - if (Cmd->For.Switches & FOR_DIRS) STRING(_T(" /D")) 935 - if (Cmd->For.Switches & FOR_F) STRING(_T(" /F")) 936 - if (Cmd->For.Switches & FOR_LOOP) STRING(_T(" /L")) 937 - if (Cmd->For.Switches & FOR_RECURSIVE) STRING(_T(" /R")) 992 + STRING(_T("for")); 993 + if (Cmd->For.Switches & FOR_DIRS) STRING(_T(" /D")); 994 + if (Cmd->For.Switches & FOR_F) STRING(_T(" /F")); 995 + if (Cmd->For.Switches & FOR_LOOP) STRING(_T(" /L")); 996 + if (Cmd->For.Switches & FOR_RECURSIVE) STRING(_T(" /R")); 938 997 if (Cmd->For.Params) 939 - PRINTF(_T(" %s"), Cmd->For.Params) 940 - PRINTF(_T(" %%%c in (%s) do "), Cmd->For.Variable, Cmd->For.List) 941 - RECURSE(Cmd->Subcommands) 998 + PRINTF(_T(" %s"), Cmd->For.Params); 999 + PRINTF(_T(" %%%c in (%s) do "), Cmd->For.Variable, Cmd->For.List); 1000 + RECURSE(Cmd->Subcommands); 942 1001 break; 943 1002 } 944 1003 945 1004 for (Redir = Cmd->Redirections; Redir; Redir = Redir->Next) 946 1005 { 947 - if (!SubstituteForVars(Redir->Filename, Buf)) return NULL; 1006 + if (!SubstituteForVars(Redir->Filename, Buf)) 1007 + return NULL; 948 1008 PRINTF(_T(" %c%s%s"), _T('0') + Redir->Number, 949 - RedirString[Redir->Mode], Buf) 1009 + RedirString[Redir->Mode], Buf); 950 1010 } 951 1011 return Out; 952 1012 }
+8 -1
base/shell/cmd/path.c
··· 50 50 LPTSTR pszBuffer; 51 51 52 52 pszBuffer = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR)); 53 + if (!pszBuffer) 54 + { 55 + WARN("Cannot allocate memory for pszBuffer!\n"); 56 + return 1; 57 + } 58 + 53 59 dwBuffer = GetEnvironmentVariable (_T("PATH"), pszBuffer, ENV_BUFFER_SIZE); 54 60 if (dwBuffer == 0) 55 61 { ··· 61 67 { 62 68 LPTSTR pszOldBuffer = pszBuffer; 63 69 pszBuffer = (LPTSTR)cmd_realloc (pszBuffer, dwBuffer * sizeof (TCHAR)); 64 - if (pszBuffer == NULL) 70 + if (!pszBuffer) 65 71 { 72 + WARN("Cannot reallocate memory for pszBuffer!\n"); 66 73 cmd_free(pszOldBuffer); 67 74 return 1; 68 75 }
+1
base/shell/cmd/setlocal.c
··· 49 49 Saved = cmd_alloc(sizeof(SETLOCAL)); 50 50 if (!Saved) 51 51 { 52 + WARN("Cannot allocate memory for Saved!\n"); 52 53 error_out_of_memory(); 53 54 return 1; 54 55 }
+2 -1
base/shell/cmd/start.c
··· 178 178 179 179 /* get comspec */ 180 180 comspec = cmd_alloc ( MAX_PATH * sizeof(TCHAR)); 181 - if (comspec == NULL) 181 + if (!comspec) 182 182 { 183 + WARN("Cannot allocate memory for start comspec!\n"); 183 184 error_out_of_memory(); 184 185 return 1; 185 186 }
+16 -2
base/shell/cmd/where.c
··· 149 149 150 150 /* load environment variable PATHEXT */ 151 151 pszPathExt = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR)); 152 + if (!pszPathExt) 153 + { 154 + WARN("Cannot allocate memory for pszPathExt!\n"); 155 + return FALSE; 156 + } 157 + 152 158 dwBuffer = GetEnvironmentVariable (_T("PATHEXT"), pszPathExt, ENV_BUFFER_SIZE); 153 159 if (dwBuffer > ENV_BUFFER_SIZE) 154 160 { 155 161 LPTSTR pszOldPathExt = pszPathExt; 156 162 pszPathExt = (LPTSTR)cmd_realloc (pszPathExt, dwBuffer * sizeof (TCHAR)); 157 - if (pszPathExt == NULL) 163 + if (!pszPathExt) 158 164 { 165 + WARN("Cannot reallocate memory for pszPathExt!\n"); 159 166 cmd_free(pszOldPathExt); 160 167 return FALSE; 161 168 } ··· 187 194 188 195 /* load environment variable PATH into buffer */ 189 196 pszPath = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR)); 197 + if (!pszPath) 198 + { 199 + WARN("Cannot allocate memory for pszPath!\n"); 200 + return FALSE; 201 + } 202 + 190 203 dwBuffer = GetEnvironmentVariable (_T("PATH"), pszPath, ENV_BUFFER_SIZE); 191 204 if (dwBuffer > ENV_BUFFER_SIZE) 192 205 { 193 206 LPTSTR pszOldPath = pszPath; 194 207 pszPath = (LPTSTR)cmd_realloc (pszPath, dwBuffer * sizeof (TCHAR)); 195 - if (pszPath == NULL) 208 + if (!pszPath) 196 209 { 210 + WARN("Cannot reallocate memory for pszPath!\n"); 197 211 cmd_free(pszOldPath); 198 212 cmd_free(pszPathExt); 199 213 return FALSE;