--- parser3/src/main/pa_exec.C 2023/09/26 20:49:10 1.97 +++ parser3/src/main/pa_exec.C 2026/10/03 14:30:03 1.107 @@ -1,7 +1,7 @@ /** @file Parser: program executing for different OS-es. - Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://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.97 2023/09/26 20:49:10 moko Exp $" IDENT_PA_EXEC_H; +volatile const char * IDENT_PA_EXEC_C="$Id: pa_exec.C,v 1.107 2026/10/03 14:30:03 moko Exp $" IDENT_PA_EXEC_H; #ifdef _MSC_VER @@ -81,7 +81,7 @@ static DWORD CreateHiddenConsoleProcess( char dir[MAX_STRING]; pa_strncpy(dir, szScriptFileSpec, MAX_STRING); lsplit(dir,' '); // trim arguments - rsplit(dir,'/'); rsplit(dir,'\\'); // trim filename + rsplit(dir,"/\\"); // trim filename // Create a child process (suspended) fCreated=CreateProcess(NULL, @@ -131,7 +131,7 @@ static int get_exit_status(HANDLE hProce return dwExitCode; } -static void read_pipe(String& result, HANDLE hOutRead, String::Language lang){ +static void read_pipe(String& result, HANDLE hOutRead, String::Language lang) { while(true) { char *buf=new(PointerFreeGC) char[MAX_STRING+1]; DWORD size=0; @@ -142,7 +142,7 @@ static void read_pipe(String& result, HA } } -static void read_pipe(File_read_result& result, HANDLE hOutRead){ +static void read_pipe(File_read_result& result, HANDLE hOutRead) { char *buf=(char*)pa_malloc(MAX_STRING+1); DWORD bufsize = MAX_STRING; @@ -165,6 +165,36 @@ static void read_pipe(File_read_result& } } +static const char* arg_quote(const char *arg) { + size_t length = strlen(arg); + if (length >= 1 && arg[0] == '-') + return arg; // avoid options qouting + 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 == '"') + 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++ = '"'; + *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=pa_fopen(file_spec_cstr, "r")) { @@ -190,13 +220,13 @@ static const char* buildCommand(const ch } fclose(f); } + { // appending argv String string(result); for(size_t i=0; icstr()); } - result=string.cstr(); } @@ -285,7 +315,7 @@ static pid_t execve_piped(const char* fi char dir[MAX_STRING]; pa_strncpy(dir, file_spec_cstr, MAX_STRING); rsplit(dir,'/'); // trim filename - chdir(dir); + PA_UNUSED int ignore_result=chdir(dir); // execute execve(file_spec_cstr, argv, env); @@ -368,7 +398,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 @@ -443,7 +472,7 @@ PA_exec_result pa_exec(bool forced_allow if(!forced_allow) { struct stat finfo; 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); + throw Exception("file.missing", &file_spec, "stat failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, file_spec_cstr)); check_safe_mode(finfo, file_spec, file_spec_cstr); } @@ -473,7 +502,7 @@ PA_exec_result pa_exec(bool forced_allow if(pid>0) { // in child if(in.length>0) // there is some in data - write(pipe_write, in.str, in.length); + PA_UNUSED int ignore_result=write(pipe_write, in.str, in.length); close(pipe_write); read_pipe(result.out, pipe_read); close(pipe_read);