--- parser3/src/main/pa_exec.C 2016/11/28 20:31:15 1.88 +++ parser3/src/main/pa_exec.C 2024/11/25 23:40:40 1.102 @@ -1,8 +1,8 @@ /** @file Parser: program executing for different OS-es. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian @todo setrlimit */ @@ -13,7 +13,7 @@ #include "pa_exception.h" #include "pa_common.h" -volatile const char * IDENT_PA_EXEC_C="$Id: pa_exec.C,v 1.88 2016/11/28 20:31:15 moko Exp $" IDENT_PA_EXEC_H; +volatile const char * IDENT_PA_EXEC_C="$Id: pa_exec.C,v 1.102 2024/11/25 23:40:40 moko Exp $" IDENT_PA_EXEC_H; #ifdef _MSC_VER @@ -79,7 +79,7 @@ static DWORD CreateHiddenConsoleProcess( // calculating script's directory char dir[MAX_STRING]; - strncpy(dir, szScriptFileSpec, MAX_STRING-1); dir[MAX_STRING-1]=0; + pa_strncpy(dir, szScriptFileSpec, MAX_STRING); lsplit(dir,' '); // trim arguments rsplit(dir,'/'); rsplit(dir,'\\'); // trim filename @@ -165,9 +165,40 @@ static void read_pipe(File_read_result& } } +static const char* arg_quote(const char *arg) { + size_t length = strlen(arg); + if (length >= 2 && arg[0] == '"' && arg[length - 1] == '"') + return arg; // allready qouted + + size_t extra_length = 2; // opening and closing quotes + for(const char *src = arg; *src; src++){ + if(*src == '"' || *src == '\\') + extra_length++; + } + + char *result = (char *)pa_malloc(length + extra_length + 1); + char *dest = result; + + *dest++ = '"'; + + for(const char *src=arg; *src;){ + char c = *src++; + if(c == '"'){ + *dest++ = '"'; // required for .cmd, .exe also supports \" + } else if(c == '\\'){ + *dest++ = '\\'; + } + *dest++ = c; + } + + *dest++ = '"'; + *dest = '\0'; + return result; +} + static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) { const char* result=file_spec_cstr; - if(FILE *f=fopen(file_spec_cstr, "r")) { + if(FILE *f=pa_fopen(file_spec_cstr, "r")) { try { char buf[MAX_STRING]; size_t size=fread(buf, 1, MAX_STRING-1, f); @@ -190,13 +221,13 @@ static const char* buildCommand(const ch } fclose(f); } + { // appending argv String string(result); for(size_t i=0; icstr()); } - result=string.cstr(); } @@ -283,7 +314,7 @@ static pid_t execve_piped(const char* fi // chdir to script's directory char dir[MAX_STRING]; - strncpy(dir, file_spec_cstr, MAX_STRING-1); dir[MAX_STRING-1]=0; + pa_strncpy(dir, file_spec_cstr, MAX_STRING); rsplit(dir,'/'); // trim filename chdir(dir); @@ -313,7 +344,7 @@ static pid_t execve_piped(const char* fi } static int get_exit_status(int pid) { - int status; + int status=0; pid_t cid; while ((cid=waitpid(pid, &status, WUNTRACED)) == -1 && errno == EINTR); if(!cid) @@ -335,7 +366,7 @@ static void read_pipe(String& result, in static void read_pipe(File_read_result& result, int file){ char *buf=(char*)pa_malloc(MAX_STRING+1); - ssize_t bufsize = MAX_STRING; + size_t bufsize = MAX_STRING; result.headers = 0; result.length = 0; @@ -368,7 +399,6 @@ struct Append_env_pair_info { }; #endif -///@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 _MSC_VER @@ -382,12 +412,7 @@ static void append_env_pair(HashStringSt #endif } -PA_exec_result pa_exec( - bool forced_allow, - const String& file_spec, - const HashStringString* env, - const ArrayString& argv, - String& in) { +PA_exec_result pa_exec(bool forced_allow, const String& file_spec, const HashStringString* env, const ArrayString& argv, String::C in) { PA_exec_result result; #ifdef NO_PA_EXECS @@ -423,9 +448,9 @@ PA_exec_result pa_exec( throw Exception("file.execute", &file_spec, "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", error_size ? szErrorDesc : "", error); } else { - const char* in_cstr=in.cstr(); DWORD written_size; - WriteFile(hInWrite, in_cstr, in.length(), &written_size, NULL); + if(in.length>0) + WriteFile(hInWrite, in.str, in.length, &written_size, NULL); CloseHandle(hInWrite); read_pipe(result.out, hOutRead); CloseHandle(hOutRead); @@ -447,7 +472,7 @@ PA_exec_result pa_exec( if(!forced_allow) { struct stat finfo; - if(stat(file_spec_cstr, &finfo)!=0) + if(pa_stat(file_spec_cstr, &finfo)!=0) throw Exception("file.missing", &file_spec, "stat failed: %s (%d), actual filename '%s'", strerror(errno), errno, file_spec_cstr); check_safe_mode(finfo, file_spec, file_spec_cstr); @@ -477,10 +502,8 @@ PA_exec_result pa_exec( &pipe_write, &pipe_read, &pipe_err); if(pid>0) { // in child - if(!in.is_empty()) {// there is some in data - const char* in_cstr=in.cstr(); - write(pipe_write, in_cstr, in.length()); - } + if(in.length>0) // there is some in data + write(pipe_write, in.str, in.length); close(pipe_write); read_pipe(result.out, pipe_read); close(pipe_read);