--- parser3/src/main/pa_exec.C 2008/09/04 09:37:48 1.72 +++ parser3/src/main/pa_exec.C 2009/10/02 01:18:27 1.78 @@ -1,13 +1,13 @@ /** @file Parser: program executing for different OS-es. - Copyright(c) 2000,2001-2005 ArtLebedev Group(http://www.artlebedev.com) + Copyright(c) 2000-2009 ArtLebedev Group(http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) @todo setrlimit */ -static const char * const IDENT_EXEC_C="$Date: 2008/09/04 09:37:48 $"; +static const char * const IDENT_EXEC_C="$Date: 2009/10/02 01:18:27 $"; #include "pa_config_includes.h" @@ -29,10 +29,10 @@ static const char * const IDENT_EXEC_C=" static DWORD CreateHiddenConsoleProcess(LPCTSTR szCmdLine, LPCTSTR szScriptFileSpec, char *szEnv, - PROCESS_INFORMATION* ppi, - LPHANDLE phInWrite, - LPHANDLE phOutRead, - LPHANDLE phErrRead) + PROCESS_INFORMATION* ppi, + LPHANDLE phInWrite, + LPHANDLE phOutRead, + LPHANDLE phErrRead) { DWORD result=0; BOOL fCreated; @@ -113,9 +113,9 @@ error: static void read_pipe(String& result, HANDLE hOutRead, String::Language lang){ while(true) { - char *buf=new(PointerFreeGC) char[MAX_STRING]; - unsigned long size; - if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size) + char *buf=new(PointerFreeGC) char[MAX_STRING+1]; + DWORD size=0; + if(!ReadFile(hOutRead, buf, MAX_STRING, &size, NULL) || !size) break; buf[size]=0; result.append_know_length(buf, size, lang); @@ -124,11 +124,8 @@ static void read_pipe(String& result, HA static void read_pipe(File_read_result& result, HANDLE hOutRead){ - char *buf=new(PointerFreeGC) char[MAX_STRING]; - - unsigned long size = 0; - unsigned long bufsize = 0; - unsigned long newsize = 0; + char *buf=(char*)pa_malloc(MAX_STRING+1); + DWORD bufsize = MAX_STRING; result.headers = 0; result.length = 0; @@ -136,20 +133,15 @@ static void read_pipe(File_read_result& result.success = false; while(true) { - if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size) + DWORD size=0; + if(!ReadFile(hOutRead, buf + result.length, bufsize - result.length, &size, NULL) || !size) break; - newsize = (size + result.length); - if(newsize > bufsize){ - bufsize = (size==MAX_STRING-1)?newsize*2:newsize; - char *tmp = new(PointerFreeGC) char[bufsize]; - if(result.str) - memcpy(tmp, result.str, result.length); - memcpy(tmp+result.length, buf, size); - result.str = tmp; - }else{ - memcpy(result.str+result.length, buf, size); + result.length += size; + if(result.length >= bufsize){ + bufsize *= 2; + buf=(char*)pa_realloc(buf, bufsize+1); } - result.length = newsize; + result.str=buf; } } @@ -312,8 +304,8 @@ static int get_exit_status(int pid) { static void read_pipe(String& result, int file, String::Language lang){ while(true) { - char *buf=new(PointerFreeGC) char[MAX_STRING]; - ssize_t length=read(file, buf, MAX_STRING-1); + char *buf=new(PointerFreeGC) char[MAX_STRING+1]; + ssize_t length=read(file, buf, MAX_STRING); if(length<=0) break; buf[length]=0; @@ -322,10 +314,8 @@ static void read_pipe(String& result, in } static void read_pipe(File_read_result& result, int file){ - char *buf=new(PointerFreeGC) char[MAX_STRING]; - - unsigned long bufsize = 0; - unsigned long newsize = 0; + char *buf=(char*)pa_malloc(MAX_STRING+1); + ssize_t bufsize = MAX_STRING; result.headers = 0; result.length = 0; @@ -333,21 +323,15 @@ static void read_pipe(File_read_result& result.success = false; while(true) { - ssize_t size=read(file, buf, MAX_STRING-1); + ssize_t size=read(file, buf + result.length, bufsize - result.length); if(size <= 0) break; - newsize = (size + result.length); - if(newsize > bufsize){ - bufsize = (size==MAX_STRING-1)?newsize*2:newsize; - char *tmp = new(PointerFreeGC) char[bufsize]; - if(result.str) - memcpy(tmp, result.str, result.length); - memcpy(tmp+result.length, buf, size); - result.str = tmp; - }else{ - memcpy(result.str+result.length, buf, size); + result.length += size; + if(result.length >= bufsize){ + bufsize *= 2; + buf=(char*)pa_realloc(buf, bufsize+1); } - result.length = newsize; + result.str=buf; } } @@ -363,7 +347,7 @@ struct Append_env_pair_info { #endif }; #endif -///@test maybe here and at argv construction --- cstr(String::L_UNSPECIFIED +///@test maybe here and at argv construction --- untaint_cstr(String::L_AS_IS static void append_env_pair(HashStringString::key_type key, HashStringString::value_type value, Append_env_pair_info *info) { #ifdef WIN32 @@ -400,7 +384,7 @@ PA_exec_result pa_exec( PROCESS_INFORMATION pi; HANDLE hInWrite, hOutRead, hErrRead; - const char* script_spec_cstr=file_spec.cstr(String::L_FILE_SPEC); + const char* script_spec_cstr=file_spec.taint_cstr(String::L_FILE_SPEC); const char* cmd=buildCommand(script_spec_cstr, argv); char* env_cstr=0; if(env) { @@ -421,7 +405,7 @@ PA_exec_result pa_exec( szErrorDesc, sizeof(szErrorDesc), (va_list *)¶m); if(error_size>3) // ".\r\n" szErrorDesc[error_size-3]=0; - + throw Exception("file.execute", &file_spec, "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", @@ -443,16 +427,16 @@ PA_exec_result pa_exec( from http://www.apache.org/websrc/cvsweb.cgi/apache-1.3/src/main/util_script.c?rev=1.151&content-type=text/vnd.viewcvs-markup * We must close the handles to the new process and its main thread - * to prevent handle and memory leaks. + * to prevent handle and memory leaks. */ CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); + CloseHandle(pi.hThread); } #else // execve needs non const - char* file_spec_cstr=file_spec.cstrm(String::L_FILE_SPEC); + char* file_spec_cstr=file_spec.taint_cstrm(String::L_FILE_SPEC); int pipe_write, pipe_read, pipe_err; @@ -469,7 +453,7 @@ from http://www.apache.org/websrc/cvsweb } #endif - char* argv_cstrs[1+50+1]={file_spec_cstr, 0}; + char* argv_cstrs[1+100+1]={file_spec_cstr, 0}; const int argv_size=argv.count(); const int argv_max=sizeof(argv_cstrs)/sizeof(argv_cstrs[0])-1-1; if(argv_size>argv_max) @@ -495,7 +479,7 @@ from http://www.apache.org/websrc/cvsweb &pipe_write, &pipe_read, &pipe_err); if(pid>0) { // in child - if(in.length()) {// there is some in data + if(!in.is_empty()) {// there is some in data const char* in_cstr=in.cstr(); write(pipe_write, in_cstr, in.length()); }