--- parser3/src/classes/op.C 2026/09/24 02:40:10 1.281 +++ parser3/src/classes/op.C 2026/10/13 20:02:00 1.286 @@ -23,7 +23,7 @@ #include "syslog.h" #endif -volatile const char * IDENT_OP_C="$Id: op.C,v 1.281 2026/09/24 02:40:10 moko Exp $"; +volatile const char * IDENT_OP_C="$Id: op.C,v 1.286 2026/10/13 20:02:00 moko Exp $"; // defines @@ -57,40 +57,6 @@ static const String exception_var_name(E #define CACHE_EXCEPTION_HANDLED_CACHE_NAME "cache" -// helpers - -class Untaint_lang_name2enum: public HashString { -public: - Untaint_lang_name2enum() { - #define ULN(name, LANG) put(name, (value_type)(String::L_##LANG)); - ULN("clean", CLEAN); - ULN("as-is", AS_IS); - ULN("optimized-as-is", AS_IS|String::L_OPTIMIZE_BIT); - ULN("file-spec", FILE_SPEC); - ULN("http-header", HTTP_HEADER); - ULN("mail-header", MAIL_HEADER); - ULN("uri", URI); - ULN("sql", SQL); - ULN("js", JS); - ULN("xml", XML); - ULN("optimized-xml", XML|String::L_OPTIMIZE_BIT); - ULN("html", HTML); - ULN("optimized-html", HTML|String::L_OPTIMIZE_BIT); - ULN("regex", REGEX); - ULN("parser-code", PARSER_CODE); - ULN("json", JSON); - #undef ULN - } - - static Untaint_lang_name2enum &instance(){ - static Untaint_lang_name2enum *singleton=NULL; - if(!singleton) - singleton=new Untaint_lang_name2enum; - return *singleton; - } - -}; - // methods static void _if(Request& r, MethodParams& params) { @@ -109,19 +75,12 @@ static void _if(Request& r, MethodParams r.process_write(params[i]); } -String::Language get_untaint_lang(const String& lang_name){ - String::Language lang=Untaint_lang_name2enum::instance().get(lang_name); - if(!lang) - throw Exception(PARSER_RUNTIME, &lang_name, "invalid taint language"); - return lang; -} - static void _untaint(Request& r, MethodParams& params) { String::Language lang; if(params.count()==1) lang=String::L_AS_IS; // mark as simply 'as-is'. useful in html from sql else - lang=get_untaint_lang(params.as_string(0, "lang must be string")); + lang=String::name2lang(params.as_string(0, "lang must be string")); Value& vbody=params.as_junction(params.count()-1, "body must be code"); Value& result=r.process(vbody); @@ -139,7 +98,7 @@ static void _taint(Request& r, MethodPar if(params.count()==1) lang=String::L_TAINTED; // mark as simply 'tainted'. useful in table:create else - lang=get_untaint_lang(params.as_string(0, "lang must be string")); + lang=String::name2lang(params.as_string(0, "lang must be string")); { Value& vbody=params.as_no_junction(params.count()-1, "body must not be code"); @@ -150,7 +109,7 @@ static void _taint(Request& r, MethodPar } static void _apply_taint(Request& r, MethodParams& params) { - String::Language lang=params.count()==1 ? String::L_AS_IS : get_untaint_lang(params.as_string(0, "lang must be string")); + String::Language lang=params.count()==1 ? String::L_AS_IS : String::name2lang(params.as_string(0, "lang must be string")); const String &sbody=params.as_string(params.count()-1, "body must be string"); String::Body result_body=sbody.cstr_to_string_body_untaint(lang, r.connection(false), &r.charsets); r.write(*new String(result_body, String::L_AS_IS)); @@ -537,7 +496,6 @@ template static Try_catch_result try_catch(Request& r, Value& body_code(Request&, I), I info, Value* catch_code, bool could_be_handled_by_caller=false) { Try_catch_result result; - // minor bug: context not restored if only finally code is present, see #1062 if(!catch_code) { result.processed_code=body_code(r, info); return result; @@ -545,38 +503,54 @@ static Try_catch_result try_catch(Reques // taking snapshot of try-context Request_context_saver try_context(r); + size_t trace_start=r.exception_trace.top_index(); + try { result.processed_code=body_code(r, info); + } catch(const Recursion_limit_exception&) { + rethrow; } catch(const Exception& e) { - Request_context_saver throw_context(r); // remembering exception stack trace + size_t trace_bottom=r.exception_trace.bottom_index(); + size_t trace_top=r.exception_trace.top_index(); VException& details=*new VException(r, e); - try_context.restore(); // restoring try-context for code after try and catch-code + try_context.restore(); // restoring try-context for catch code + r.recursion_limit_try_restore(); // here to allow "endless" recursion in catch - { - Temp_value_element temp(r, *catch_code->get_junction()->method_frame, exception_var_name, &details); - Temp_skip temp_skip(r); - result.processed_code=r.process(*catch_code); - } - - // retriving $exception.handled - Value* vhandled=details.handled(); + r.exception_trace.set_bottom_index(trace_top); // keep the original trace occupied; errors in catch start after it bool bhandled=false; - if(vhandled) { - if(vhandled->is_string()) { // not simple $exception.handled(1/0)? - if(bhandled=could_be_handled_by_caller) { // and we can possibly handle it - result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover - } - } else - bhandled=vhandled->as_bool(); + try { + { + Temp_value_element temp(r, *catch_code->get_junction()->method_frame, exception_var_name, &details); + Temp_skip temp_skip(r); + result.processed_code=r.process(*catch_code); + } + + Value* vhandled=details.handled(); + if(vhandled) { + if(vhandled->is_string()) { // not simple $exception.handled(1/0)? + if(bhandled=could_be_handled_by_caller) { // and we can possibly handle it + result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover + } + } else + bhandled=vhandled->as_bool(); + } + } catch(...) { + try_context.restore(); // restore execution for finally, but keep new error trace. + r.recursion_limit_try_restore(); // here to allow "endless" recursion in finally + details.expire_trace(); + rethrow; } + details.expire_trace(); if(!bhandled){ - throw_context.restore(); // restoring exception stack trace creared by try_context.restore() + r.exception_trace.set_range(trace_bottom, trace_top); rethrow; } + + r.exception_trace.set_range(trace_start, trace_start); } return result; @@ -830,11 +804,17 @@ static void _try_operator(Request& r, Me try{ // process try and catch code result=try_catch(r, process_try_body_code, &body_code, &catch_code); + } catch(const Recursion_limit_exception&) { + rethrow; } catch(...){ // process finally code but ignore the result if(finally_code){ + size_t trace_bottom=r.exception_trace.bottom_index(); + size_t trace_top=r.exception_trace.top_index(); + r.exception_trace.set_bottom_index(trace_top); Temp_skip temp(r); /* Value &finally_result= */ r.process(*finally_code); + r.exception_trace.set_range(trace_bottom, trace_top); } rethrow; }