From c6d24089dbc284f0fef5e1ce37b75ac04c6ebd94 Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Thu, 15 Nov 2012 12:32:21 +0100 Subject: [PATCH] cmd: Make allocation unfailable. --- programs/cmd/batch.c | 5 +- programs/cmd/builtins.c | 43 ++++++--------- programs/cmd/directory.c | 31 ++++------- programs/cmd/wcmd.h | 18 +++++- programs/cmd/wcmdmain.c | 116 +++++++++++++++++---------------------- 5 files changed, 96 insertions(+), 117 deletions(-) diff --git a/programs/cmd/batch.c b/programs/cmd/batch.c index 5bf7d1bf994..d02caa5e2e0 100644 --- a/programs/cmd/batch.c +++ b/programs/cmd/batch.c @@ -68,7 +68,7 @@ void WCMD_batch (WCHAR *file, WCHAR *command, BOOL called, WCHAR *startLabel, HA prev_context = context; context = LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT)); context -> h = h; - context->batchfileW = WCMD_strdupW(file); + context->batchfileW = heap_strdupW(file); context -> command = command; memset(context -> shift_count, 0x00, sizeof(context -> shift_count)); context -> prev_context = prev_context; @@ -257,8 +257,7 @@ WCHAR *WCMD_fgets(WCHAR *buf, DWORD noChars, HANDLE h) const char *p; cp = GetConsoleCP(); - bufA = HeapAlloc(GetProcessHeap(), 0, noChars); - if (!bufA) return NULL; + bufA = heap_alloc(noChars); /* Save current file position */ filepos.QuadPart = 0; diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c index bcf19a33f95..7a39f129986 100644 --- a/programs/cmd/builtins.c +++ b/programs/cmd/builtins.c @@ -230,9 +230,7 @@ void WCMD_choice (const WCHAR * args) { have_console = GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldmode); errorlevel = 0; - my_command = WCMD_strdupW(WCMD_skip_leading_spaces((WCHAR*) args)); - if (!my_command) - return; + my_command = heap_strdupW(WCMD_skip_leading_spaces((WCHAR*) args)); ptr = WCMD_skip_leading_spaces(my_command); while (*ptr == '/') { @@ -632,9 +630,7 @@ void WCMD_copy(WCHAR * args) { } /* We have found something to process - build a COPY_FILE block to store it */ - thiscopy = HeapAlloc(GetProcessHeap(),0,sizeof(COPY_FILES)); - if (thiscopy == NULL) goto exitreturn; - + thiscopy = heap_alloc(sizeof(COPY_FILES)); WINE_TRACE("Not a switch, but probably a filename/list %s\n", wine_dbgstr_w(thisparam)); thiscopy->concatenate = concatnextfilename; @@ -645,7 +641,7 @@ void WCMD_copy(WCHAR * args) { leave space to append \* to the end) , then copy in character by character. Strip off quotes if we find them. */ len = strlenW(thisparam) + (sizeof(WCHAR) * 5); /* 5 spare characters, null + \*.* */ - thiscopy->name = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR)); + thiscopy->name = heap_alloc(len*sizeof(WCHAR)); memset(thiscopy->name, 0x00, len); pos1 = thisparam; @@ -724,7 +720,7 @@ void WCMD_copy(WCHAR * args) { strcpyW(destname, dotW); strcatW(destname, slashW); - destination = HeapAlloc(GetProcessHeap(),0,sizeof(COPY_FILES)); + destination = heap_alloc(sizeof(COPY_FILES)); if (destination == NULL) goto exitreturn; destination->concatenate = FALSE; /* Not used for destination */ destination->binarycopy = binarymode; @@ -785,7 +781,7 @@ void WCMD_copy(WCHAR * args) { /* Save away the destination name*/ HeapFree(GetProcessHeap(), 0, destination->name); - destination->name = WCMD_strdupW(destname); + destination->name = heap_strdupW(destname); WINE_TRACE("Resolved destination is '%s' (calc later %d)\n", wine_dbgstr_w(destname), appendfirstsource); @@ -886,7 +882,7 @@ void WCMD_copy(WCHAR * args) { /* If we needed tyo save away the first filename, do it */ if (appendfirstsource && overwrite) { HeapFree(GetProcessHeap(), 0, destination->name); - destination->name = WCMD_strdupW(outname); + destination->name = heap_strdupW(outname); WINE_TRACE("Final resolved destination name : '%s'\n", wine_dbgstr_w(outname)); appendfirstsource = FALSE; destisdirectory = FALSE; @@ -1272,14 +1268,12 @@ static BOOL WCMD_delete_one (const WCHAR *thisArg) { WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(subParm)); /* Allocate memory, add to list */ - nextDir = HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK)); + nextDir = heap_alloc(sizeof(DIRECTORY_STACK)); if (allDirs == NULL) allDirs = nextDir; if (lastEntry != NULL) lastEntry->next = nextDir; lastEntry = nextDir; nextDir->next = NULL; - nextDir->dirName = HeapAlloc(GetProcessHeap(),0, - (strlenW(subParm)+1) * sizeof(WCHAR)); - strcpyW(nextDir->dirName, subParm); + nextDir->dirName = heap_strdupW(subParm); } } while (FindNextFileW(hff, &fd) != 0); FindClose (hff); @@ -1362,8 +1356,7 @@ static WCHAR *WCMD_strtrim(const WCHAR *s) const WCHAR *start = s; WCHAR* result; - if (!(result = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR)))) - return NULL; + result = heap_alloc((len + 1) * sizeof(WCHAR)); while (isspaceW(*start)) start++; if (*start) { @@ -1440,7 +1433,7 @@ static void WCMD_part_execute(CMD_LIST **cmdList, const WCHAR *firstcmd, /* Process the first command, if there is one */ if (executecmds && firstcmd && *firstcmd) { - WCHAR *command = WCMD_strdupW(firstcmd); + WCHAR *command = heap_strdupW(firstcmd); WCMD_execute (firstcmd, (*cmdList)->redirects, cmdList, FALSE); HeapFree(GetProcessHeap(), 0, command); } @@ -1658,14 +1651,12 @@ static void WCMD_add_dirstowalk(DIRECTORY_STACK *dirsToWalk) { (strcmpW(fd.cFileName, dotW) != 0)) { /* Allocate memory, add to list */ - DIRECTORY_STACK *toWalk = HeapAlloc(GetProcessHeap(), 0, sizeof(DIRECTORY_STACK)); + DIRECTORY_STACK *toWalk = heap_alloc(sizeof(DIRECTORY_STACK)); WINE_TRACE("(%p->%p)\n", remainingDirs, remainingDirs->next); toWalk->next = remainingDirs->next; remainingDirs->next = toWalk; remainingDirs = toWalk; - toWalk->dirName = HeapAlloc(GetProcessHeap(), 0, - sizeof(WCHAR) * - (strlenW(dirsToWalk->dirName) + 2 + strlenW(fd.cFileName))); + toWalk->dirName = heap_alloc(sizeof(WCHAR) * (strlenW(dirsToWalk->dirName) + 2 + strlenW(fd.cFileName))); strcpyW(toWalk->dirName, dirsToWalk->dirName); strcatW(toWalk->dirName, slashW); strcatW(toWalk->dirName, fd.cFileName); @@ -1728,7 +1719,7 @@ static void WCMD_parse_line(CMD_LIST *cmdStart, /* FIXME: Use tokens= line to populate forloopcontext */ varidx = FOR_VAR_IDX(variable); - if (varidx >=0) forloopcontext.variable[varidx] = WCMD_strdupW(parm); + if (varidx >=0) forloopcontext.variable[varidx] = heap_strdupW(parm); if (where && where[0] != forf_eol) { CMD_LIST *thisCmdStart = cmdStart; @@ -1907,11 +1898,9 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) { /* Set up the list of directories to recurse if we are going to */ } else if (doRecurse) { /* Allocate memory, add to list */ - dirsToWalk = HeapAlloc(GetProcessHeap(), 0, sizeof(DIRECTORY_STACK)); + dirsToWalk = heap_alloc(sizeof(DIRECTORY_STACK)); dirsToWalk->next = NULL; - dirsToWalk->dirName = HeapAlloc(GetProcessHeap(),0, - (strlenW(optionsRoot) + 1) * sizeof(WCHAR)); - strcpyW(dirsToWalk->dirName, optionsRoot); + dirsToWalk->dirName = heap_strdupW(optionsRoot); WINE_TRACE("Starting with root directory %s\n", wine_dbgstr_w(dirsToWalk->dirName)); } @@ -3435,7 +3424,7 @@ void WCMD_start(const WCHAR *args) GetWindowsDirectoryW( file, MAX_PATH ); strcatW( file, exeW ); - cmdline = HeapAlloc( GetProcessHeap(), 0, (strlenW(file) + strlenW(args) + 2) * sizeof(WCHAR) ); + cmdline = heap_alloc( (strlenW(file) + strlenW(args) + 2) * sizeof(WCHAR) ); strcpyW( cmdline, file ); strcatW( cmdline, spaceW ); strcatW( cmdline, args ); diff --git a/programs/cmd/directory.c b/programs/cmd/directory.c index 28e02314a9c..f8d0ffd4f8c 100644 --- a/programs/cmd/directory.c +++ b/programs/cmd/directory.c @@ -203,8 +203,7 @@ static void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen) { ULONG domainLen = MAXSTRING; SID_NAME_USE nameuse; - secBuffer = HeapAlloc(GetProcessHeap(),0,sizeNeeded * sizeof(BYTE)); - if(!secBuffer) return; + secBuffer = heap_alloc(sizeNeeded * sizeof(BYTE)); /* Get the owners security descriptor */ if(!GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, secBuffer, @@ -274,7 +273,7 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le same directory. Note issuing a directory header with no contents mirrors what windows does */ parms = inputparms; - fd = HeapAlloc(GetProcessHeap(),0,sizeof(WIN32_FIND_DATAW)); + fd = heap_alloc(sizeof(WIN32_FIND_DATAW)); while (parms && strcmpW(inputparms->dirName, parms->dirName) == 0) { concurrentDirs++; @@ -513,17 +512,13 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string)); /* Allocate memory, add to list */ - thisDir = HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK)); + thisDir = heap_alloc(sizeof(DIRECTORY_STACK)); if (dirStack == NULL) dirStack = thisDir; if (lastEntry != NULL) lastEntry->next = thisDir; lastEntry = thisDir; thisDir->next = NULL; - thisDir->dirName = HeapAlloc(GetProcessHeap(),0, - sizeof(WCHAR) * (strlenW(string)+1)); - strcpyW(thisDir->dirName, string); - thisDir->fileName = HeapAlloc(GetProcessHeap(),0, - sizeof(WCHAR) * (strlenW(parms->fileName)+1)); - strcpyW(thisDir->fileName, parms->fileName); + thisDir->dirName = heap_strdupW(string); + thisDir->fileName = heap_strdupW(parms->fileName); parms = parms->next; } } @@ -853,7 +848,7 @@ void WCMD_directory (WCHAR *args) } WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path)); - thisEntry = HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK)); + thisEntry = heap_alloc(sizeof(DIRECTORY_STACK)); if (fullParms == NULL) fullParms = thisEntry; if (prevEntry != NULL) prevEntry->next = thisEntry; prevEntry = thisEntry; @@ -865,13 +860,11 @@ void WCMD_directory (WCHAR *args) wine_dbgstr_w(drive), wine_dbgstr_w(dir), wine_dbgstr_w(fname), wine_dbgstr_w(ext)); - thisEntry->dirName = HeapAlloc(GetProcessHeap(),0, - sizeof(WCHAR) * (strlenW(drive)+strlenW(dir)+1)); + thisEntry->dirName = heap_alloc(sizeof(WCHAR) * (strlenW(drive)+strlenW(dir)+1)); strcpyW(thisEntry->dirName, drive); strcatW(thisEntry->dirName, dir); - thisEntry->fileName = HeapAlloc(GetProcessHeap(),0, - sizeof(WCHAR) * (strlenW(fname)+strlenW(ext)+1)); + thisEntry->fileName = heap_alloc(sizeof(WCHAR) * (strlenW(fname)+strlenW(ext)+1)); strcpyW(thisEntry->fileName, fname); strcatW(thisEntry->fileName, ext); @@ -881,12 +874,10 @@ void WCMD_directory (WCHAR *args) /* If just 'dir' entered, a '*' parameter is assumed */ if (fullParms == NULL) { WINE_TRACE("Inserting default '*'\n"); - fullParms = HeapAlloc(GetProcessHeap(),0, sizeof(DIRECTORY_STACK)); + fullParms = heap_alloc(sizeof(DIRECTORY_STACK)); fullParms->next = NULL; - fullParms->dirName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * (strlenW(cwd)+1)); - strcpyW(fullParms->dirName, cwd); - fullParms->fileName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * 2); - strcpyW(fullParms->fileName, starW); + fullParms->dirName = heap_strdupW(cwd); + fullParms->fileName = heap_strdupW(starW); } lastDrive = '?'; diff --git a/programs/cmd/wcmd.h b/programs/cmd/wcmd.h index fb1712231a1..33098610a48 100644 --- a/programs/cmd/wcmd.h +++ b/programs/cmd/wcmd.h @@ -117,7 +117,6 @@ void WCMD_HandleTildaModifiers(WCHAR **start, BOOL justFors); void WCMD_splitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext); void WCMD_strip_quotes(WCHAR *cmd); WCHAR *WCMD_LoadMessage(UINT id); -WCHAR *WCMD_strdupW(const WCHAR *input); void WCMD_strsubstW(WCHAR *start, const WCHAR* next, const WCHAR* insert, int len); BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars, LPDWORD charsRead); @@ -127,6 +126,23 @@ void WCMD_free_commands(CMD_LIST *cmds); void WCMD_execute (const WCHAR *orig_command, const WCHAR *redirects, CMD_LIST **cmdList, BOOL retrycall); +void *heap_alloc(size_t); + +static inline WCHAR *heap_strdupW(const WCHAR *str) +{ + WCHAR *ret = NULL; + + if(str) { + size_t size; + + size = (strlenW(str)+1)*sizeof(WCHAR); + ret = heap_alloc(size); + memcpy(ret, str, size); + } + + return ret; +} + /* Data structure to hold context when executing batch files */ typedef struct _BATCH_CONTEXT { diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index c59f686e185..83f66619d73 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -73,11 +73,8 @@ static int numChars; static char *get_file_buffer(void) { static char *output_bufA = NULL; - if (!output_bufA) { - output_bufA = HeapAlloc(GetProcessHeap(), 0, MAX_WRITECONSOLE_SIZE); - if (!output_bufA) - WINE_FIXME("Out of memory - could not allocate ansi 64K buffer\n"); - } + if (!output_bufA) + output_bufA = heap_alloc(MAX_WRITECONSOLE_SIZE); return output_bufA; } @@ -439,16 +436,17 @@ static void WCMD_show_prompt (void) { WCMD_output_asis (out_string); } +void *heap_alloc(size_t size) +{ + void *ret; -/************************************************************************* - * WCMD_strdupW - * A wide version of strdup as its missing from unicode.h - */ -WCHAR *WCMD_strdupW(const WCHAR *input) { - int len=strlenW(input)+1; - WCHAR *result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); - memcpy(result, input, len * sizeof(WCHAR)); - return result; + ret = HeapAlloc(GetProcessHeap(), 0, size); + if(!ret) { + ERR("Out of memory\n"); + ExitProcess(1); + } + + return ret; } /************************************************************************* @@ -732,16 +730,16 @@ static WCHAR *WCMD_expand_envvar(WCHAR *start) WCHAR *searchFor; if (equalspos == NULL) return start+1; - s = WCMD_strdupW(endOfVar + 1); + s = heap_strdupW(endOfVar + 1); /* Null terminate both strings */ thisVar[strlenW(thisVar)-1] = 0x00; *equalspos = 0x00; /* Since we need to be case insensitive, copy the 2 buffers */ - searchIn = WCMD_strdupW(thisVarContents); + searchIn = heap_strdupW(thisVarContents); CharUpperBuffW(searchIn, strlenW(thisVarContents)); - searchFor = WCMD_strdupW(colonpos+1); + searchFor = heap_strdupW(colonpos+1); CharUpperBuffW(searchFor, strlenW(colonpos+1)); /* Handle wildcard case */ @@ -938,40 +936,42 @@ static void init_msvcrt_io_block(STARTUPINFOW* st) st->lpReserved2 = st_p.lpReserved2; if (st_p.cbReserved2 && st_p.lpReserved2) { + unsigned num = *(unsigned*)st_p.lpReserved2; + char* flags; + HANDLE* handles; + BYTE *ptr; + size_t sz; + /* Override the entries for fd 0,1,2 if we happened * to change those std handles (this depends on the way cmd sets * its new input & output handles) */ - size_t sz = max(sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * 3, st_p.cbReserved2); - BYTE* ptr = HeapAlloc(GetProcessHeap(), 0, sz); - if (ptr) - { - unsigned num = *(unsigned*)st_p.lpReserved2; - char* flags = (char*)(ptr + sizeof(unsigned)); - HANDLE* handles = (HANDLE*)(flags + num * sizeof(char)); + sz = max(sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * 3, st_p.cbReserved2); + ptr = heap_alloc(sz); + flags = (char*)(ptr + sizeof(unsigned)); + handles = (HANDLE*)(flags + num * sizeof(char)); - memcpy(ptr, st_p.lpReserved2, st_p.cbReserved2); - st->cbReserved2 = sz; - st->lpReserved2 = ptr; + memcpy(ptr, st_p.lpReserved2, st_p.cbReserved2); + st->cbReserved2 = sz; + st->lpReserved2 = ptr; #define WX_OPEN 0x01 /* see dlls/msvcrt/file.c */ - if (num <= 0 || (flags[0] & WX_OPEN)) - { - handles[0] = GetStdHandle(STD_INPUT_HANDLE); - flags[0] |= WX_OPEN; - } - if (num <= 1 || (flags[1] & WX_OPEN)) - { - handles[1] = GetStdHandle(STD_OUTPUT_HANDLE); - flags[1] |= WX_OPEN; - } - if (num <= 2 || (flags[2] & WX_OPEN)) - { - handles[2] = GetStdHandle(STD_ERROR_HANDLE); - flags[2] |= WX_OPEN; - } -#undef WX_OPEN + if (num <= 0 || (flags[0] & WX_OPEN)) + { + handles[0] = GetStdHandle(STD_INPUT_HANDLE); + flags[0] |= WX_OPEN; } + if (num <= 1 || (flags[1] & WX_OPEN)) + { + handles[1] = GetStdHandle(STD_OUTPUT_HANDLE); + flags[1] |= WX_OPEN; + } + if (num <= 2 || (flags[2] & WX_OPEN)) + { + handles[2] = GetStdHandle(STD_ERROR_HANDLE); + flags[2] |= WX_OPEN; + } +#undef WX_OPEN } } @@ -1288,22 +1288,11 @@ void WCMD_execute (const WCHAR *command, const WCHAR *redirects, } /* Move copy of the command onto the heap so it can be expanded */ - new_cmd = HeapAlloc( GetProcessHeap(), 0, MAXSTRING * sizeof(WCHAR)); - if (!new_cmd) - { - WINE_ERR("Could not allocate memory for new_cmd\n"); - return; - } + new_cmd = heap_alloc(MAXSTRING * sizeof(WCHAR)); strcpyW(new_cmd, command); /* Move copy of the redirects onto the heap so it can be expanded */ - new_redir = HeapAlloc( GetProcessHeap(), 0, MAXSTRING * sizeof(WCHAR)); - if (!new_redir) - { - WINE_ERR("Could not allocate memory for new_redir\n"); - HeapFree( GetProcessHeap(), 0, new_cmd ); - return; - } + new_redir = heap_alloc(MAXSTRING * sizeof(WCHAR)); /* If piped output, send stdout to the pipe by appending >filename to redirects */ if (piped) { @@ -1673,18 +1662,16 @@ static void WCMD_addCommand(WCHAR *command, int *commandLen, CMD_LIST *thisEntry = NULL; /* Allocate storage for command */ - thisEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(CMD_LIST)); + thisEntry = heap_alloc(sizeof(CMD_LIST)); /* Copy in the command */ if (command) { - thisEntry->command = HeapAlloc(GetProcessHeap(), 0, - (*commandLen+1) * sizeof(WCHAR)); + thisEntry->command = heap_alloc((*commandLen+1) * sizeof(WCHAR)); memcpy(thisEntry->command, command, *commandLen * sizeof(WCHAR)); thisEntry->command[*commandLen] = 0x00; /* Copy in the redirects */ - thisEntry->redirects = HeapAlloc(GetProcessHeap(), 0, - (*redirLen+1) * sizeof(WCHAR)); + thisEntry->redirects = heap_alloc((*redirLen+1) * sizeof(WCHAR)); memcpy(thisEntry->redirects, redirs, *redirLen * sizeof(WCHAR)); thisEntry->redirects[*redirLen] = 0x00; thisEntry->pipeFile[0] = 0x00; @@ -1814,7 +1801,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE /* Allocate working space for a command read from keyboard, file etc */ if (!extraSpace) - extraSpace = HeapAlloc(GetProcessHeap(), 0, (MAXSTRING+1) * sizeof(WCHAR)); + extraSpace = heap_alloc((MAXSTRING+1) * sizeof(WCHAR)); if (!extraSpace) { WINE_ERR("Could not allocate memory for extraSpace\n"); @@ -2413,10 +2400,7 @@ int wmain (int argc, WCHAR *argvW[]) len = strlenW(argPos); /* Take a copy */ - cmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); - if (!cmd) - exit(1); - strcpyW(cmd, argPos); + cmd = heap_strdupW(argPos); /* opt_s left unflagged if the command starts with and contains exactly * one quoted string (exactly two quote characters). The quoted string