--- parser3/src/main/pa_request.C 2026/09/25 12:55:42 1.443 +++ parser3/src/main/pa_request.C 2026/09/28 02:09:17 1.451 @@ -7,7 +7,6 @@ #include "pa_sapi.h" #include "pa_common.h" -#include "pa_dir.h" #include "pa_os.h" #include "pa_request.h" #include "pa_wwrapper.h" @@ -36,7 +35,7 @@ #include "pa_vdate.h" #include "pa_varray.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.443 2026/09/25 12:55:42 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; +volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.451 2026/09/28 02:09:17 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -45,10 +44,19 @@ volatile const char * IDENT_PA_REQUEST_C /// content type of response when no $MAIN:defaults.content-type defined const char* DEFAULT_CONTENT_TYPE="text/html"; -const uint LOOP_LIMIT=100000; -const uint ARRAY_LIMIT=1000000; -const uint EXECUTE_RECURSION_LIMIT=1000; -const uint HTTPD_TIMEOUT=4; +// One recursion level uses ~0.85KB of stack under linux/gcc, ~1KB under win32 and ~2KB under win64. +// MSVC win64 exception handling requires an extra ~10KB !!! for each throw, and execute() catches and +// rethrows on every level to provide a stack trace. +// So 1000 levels need ~1MB under linux (default 8MB stack is enough) and ~2+10=12MB under win64. +// Worse, win64 keeps the unwound frames until the catch block returns, so a handler running away +// in its turn starts on top of them and needs one more such budget. The stack is set to 40MB in +// the MSVC project settings to allow nested three runaways. +const int EXECUTE_RECURSION_LIMIT=1000; +const int EXECUTE_RECURSION_RESERVE=100; + +const int LOOP_LIMIT=100000; +const int ARRAY_LIMIT=1000000; +const int HTTPD_TIMEOUT=4; const size_t FILE_SIZE_LIMIT=512*1024*1024; // defines for globals @@ -130,9 +138,18 @@ const String content_disposition_filenam VStateless_class& VClassMAIN_create(); // +void Request::recursion_limit_reached() { + if(execute_recursion_limit>pa_execute_recursion_limit) + throw Recursion_limit_exception(); + + execute_recursion_limit+=EXECUTE_RECURSION_RESERVE; + throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected"); +} + Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, String::Language adefault_lang): // private anti_endless_execute_recursion(0), + execute_recursion_limit(pa_execute_recursion_limit), // public allow_class_replace(false), @@ -343,14 +360,16 @@ void Request::configure_admin(VStateless pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT; CONF_OPTION(limits, recursion_limit_name, { pa_execute_recursion_limit=option->as_int(); - if(pa_execute_recursion_limit==0) pa_execute_recursion_limit=INT_MAX; + if(pa_execute_recursion_limit<=0) pa_execute_recursion_limit=INT_MAX; + pa_execute_recursion_limit=min(pa_execute_recursion_limit, INT_MAX-EXECUTE_RECURSION_RESERVE); }, "LIMITS.%s must be int"); + execute_recursion_limit=pa_execute_recursion_limit; pa_file_size_limit=FILE_SIZE_LIMIT; CONF_OPTION(limits, file_size_limit_name, { double limit=option->as_double(); if(limit >= (double)SSIZE_MAX) - throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %.15g", file_size_limit_name.cstr(), (double)SSIZE_MAX); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %s", file_size_limit_name.cstr(), pa_itoa(SSIZE_MAX)); pa_file_size_limit=(size_t)limit; if(pa_file_size_limit==0) pa_file_size_limit=SSIZE_MAX; @@ -422,7 +441,7 @@ const char* Request::get_exception_cstr( return result; } -Table &Request::Exception_trace::table(Request &r, const String* problem_source){ +Table &Request::Exception_trace::table(Request &r, const String* problem_source, size_t bottom, size_t top){ // $stack[^table::create{name file lineno colno}] Table::columns_type stack_trace_columns(new ArrayString); *stack_trace_columns+=new String("name"); @@ -431,8 +450,8 @@ Table &Request::Exception_trace::table(R *stack_trace_columns+=new String("colno"); Table& stack_trace=*new Table(stack_trace_columns); - if(!is_empty()/*signed!*/) - for(size_t i=bottom_index()+(bottom_value().name()==problem_source); ias_vfile(flang, &charsets), header_only, as_attachment); } catch(const Exception& e) { // request handling problem + // OK, as native execution stack has unwound + anti_endless_execute_recursion=0; + // we're returning not result, but error explanation VException& details=*new VException(*this, e); const char* exception_cstr=get_exception_cstr(e, details); @@ -536,6 +558,7 @@ void Request::core(const char* config_fi // preparing parameters to @unhandled_exception[exception;stack] Table& stack_trace=exception_trace.table(*this, details.problem_source); + details.expire_trace(); exception_trace.clear(); // forget all about previous life, in case there would be error inside of this method, error handled would not be mislead by old stack contents (see extract_origin) Value *params[]={&details, new VTable(&stack_trace)}; @@ -713,13 +736,13 @@ const String& Request::relative(const ch } const String& Request::full_disk_path(const String& relative_name) { - if(is_os_absolute_path(relative_name)) - return relative_name; if(relative_name.first_char()=='/') { String& result=*new String(pa_strdup(request_info.document_root)); result << relative_name; return result; } + if(is_os_absolute_path(relative_name)) + return relative_name; if(relative_name.starts_with("http://") || relative_name.starts_with("parser://")) return relative_name; @@ -882,7 +905,7 @@ static void output_pieces(Request& r, bo part_length = rg.end-rg.start+1; char buf[MAX_STRING]; - snprintf(buf, MAX_STRING, "bytes %.15g-%.15g/%.15g", (double)rg.start, (double)rg.end, (double)content_length); + snprintf(buf, MAX_STRING, "bytes %s-%s/%s", pa_uitoa(rg.start), pa_uitoa(rg.end), pa_uitoa(content_length)); SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206"); SAPI::add_header_attribute(r.sapi_info, "content-range", buf); } else { @@ -1018,6 +1041,7 @@ const String& Request::transcode(const x VException::VException(Request& r, const Exception& e): fexception(e), fhandled(&VBool::get(false)), + ftrace_bottom(r.exception_trace.bottom_index()), ftrace_top(r.exception_trace.top_index()), origin(Operation::Origin::create(0, 0, 0)), problem_source(e.problem_source()) { Request::Exception_trace& exception_trace=r.exception_trace; @@ -1035,6 +1059,12 @@ Value* VException::handled() { return fhash ? fhash->get(Symbols::HANDLED_SYMBOL) : fhandled; } +Table& VException::stack(Request& r) { + if(ftrace_top==size_t(-1)) + throw Exception(PARSER_RUNTIME, 0, "exception stack used outside of context"); + return r.exception_trace.table(r, problem_source, ftrace_bottom, ftrace_top); +} + Value* VException::get_element(const String& name) { if(!fhash) { if(SYMBOLS_EQ(name,HANDLED_SYMBOL))