--- parser3/src/main/pa_common.C 2020/12/08 21:30:46 1.321 +++ parser3/src/main/pa_common.C 2025/07/02 23:42:10 1.337 @@ -1,8 +1,8 @@ /** @file Parser: commonly functions. - Copyright (c) 2000-2017 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2000-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "pa_common.h" @@ -12,7 +12,6 @@ #include "pa_charsets.h" #include "pa_http.h" #include "pa_request_charsets.h" -#include "pcre.h" #include "pa_request.h" #include "pa_idna.h" @@ -29,7 +28,7 @@ #define pa_mkdir(path, mode) mkdir(path, mode) #endif -volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.321 2020/12/08 21:30:46 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; +volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.337 2025/07/02 23:42:10 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; // some maybe-undefined constants @@ -89,6 +88,15 @@ FILE *pa_fopen(const char *pathname, con return _wfopen((const wchar_t *)utf16name, (const wchar_t *)utf16mode); } +int pa_unlink(const char *pathname){ + const UTF16* utf16name=pa_utf16_encode(pathname, pa_thread_request().charsets.source()); + return _wunlink((const wchar_t *)utf16name); +} + +#else + +#define pa_unlink unlink + #endif /// these options were handled but not checked elsewhere, now check them @@ -116,7 +124,7 @@ struct File_read_action_info { static void file_read_action(struct stat& finfo, int f, const String& file_spec, void *context) { File_read_action_info& info = *static_cast(context); - size_t to_read_size = check_file_size(info.limit && info.limit < finfo.st_size ? info.limit : finfo.st_size, &file_spec); + size_t to_read_size = check_file_size(info.limit && info.limit < (size_t)finfo.st_size ? info.limit : (size_t)finfo.st_size, &file_spec); if(to_read_size) { if(info.offset) pa_lseek(f, info.offset, SEEK_SET); // seek never fails as POSIX allows the file offset to be set beyond the EOF @@ -238,12 +246,10 @@ void check_safe_mode(struct stat, const #endif +bool file_read_action_under_lock(const String& file_spec, const char* action_name, File_read_action action, void *context, + bool as_text, bool fail_on_read_problem) { -bool file_read_action_under_lock(const String& file_spec, - const char* action_name, File_read_action action, void *context, - bool as_text, - bool fail_on_read_problem) { - const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); + const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); int f; // first open, next stat: @@ -255,10 +261,11 @@ bool file_read_action_under_lock(const S // they delay update till open, so we would receive "!^test[" string // if would do stat, next open. // later: it seems, even this does not help sometimes - if((f=pa_open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) { + if((f=pa_open(fname, O_RDONLY | (as_text ? _O_TEXT : _O_BINARY) ))>=0) { try { - if(pa_lock_shared_blocking(f)!=0) - throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname); + int pa_errno=pa_lock_shared_blocking(f); + if(pa_errno!=0) + throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname); struct stat finfo; if(pa_fstat(f, &finfo)!=0) @@ -286,35 +293,29 @@ bool file_read_action_under_lock(const S } void create_dir_for_file(const String& file_spec) { - size_t pos_after=1; - size_t pos_before; - while((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) { - pa_mkdir(file_spec.mid(0, pos_before).taint_cstr(String::L_FILE_SPEC), 0775); - pos_after=pos_before+1; + const char *str=file_spec.taint_cstr(String::L_FILE_SPEC); + if(str[0]){ + const char *pos=str+1; + while((pos=strchr(pos,'/')) && pos[1]) { // to avoid trailing /, see #1166 + pa_mkdir(pa_strdup(str,pos-str), 0775); + pos++; + } } } -bool file_write_action_under_lock( - const String& file_spec, - const char* action_name, - File_write_action action, - void *context, - bool as_text, - bool do_append, - bool do_block, - bool fail_on_lock_problem) { +bool file_write_action_under_lock(const String& file_spec, const char* action_name, File_write_action action, void *context, + bool as_text, bool do_append, bool do_block, bool fail_on_lock_problem) { + const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); int f; if(access(fname, W_OK)!=0) // no create_dir_for_file(file_spec); - if((f=pa_open(fname, - O_CREAT|O_RDWR - |(as_text?_O_TEXT:_O_BINARY) - |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) { - if((do_block?pa_lock_exclusive_blocking(f):pa_lock_exclusive_nonblocking(f))!=0) { - Exception e("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname); - close(f); + if((f=pa_open(fname, O_CREAT | O_RDWR | (as_text ? _O_TEXT : _O_BINARY) | (do_append ? O_APPEND : PA_O_TRUNC), 0664))>=0) { + int pa_errno=do_block ? pa_lock_exclusive_blocking(f) : pa_lock_exclusive_nonblocking(f); + if(pa_errno!=0) { + Exception e("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname); + close(f); if(fail_on_lock_problem) throw e; return false; @@ -330,17 +331,17 @@ bool file_write_action_under_lock( } catch(...) { #ifdef HAVE_FTRUNCATE if(!do_append) - ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower + PA_UNUSED int ignore_result=ftruncate(f, lseek(f, 0, SEEK_CUR)); // one cannot use O_TRUNC, read lower #endif - pa_unlock(f);close(f); + pa_unlock(f);close(f); rethrow; } #ifdef HAVE_FTRUNCATE if(!do_append) - ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov for discovering] + PA_UNUSED int ignore_result=ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov for discovering] #endif - pa_unlock(f);close(f); + pa_unlock(f);close(f); return true; } else throw Exception(errno==EACCES ? "file.access" : 0, &file_spec, "%s failed: %s (%d), actual filename '%s'", action_name, strerror(errno), errno, fname); @@ -464,7 +465,7 @@ static void rmdir(const String& file_spe bool file_delete(const String& file_spec, bool fail_on_problem, bool keep_empty_dirs) { const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); - if(unlink(fname)!=0) { + if(pa_unlink(fname)!=0) { if(fail_on_problem) throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, &file_spec, "unlink failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname); @@ -564,15 +565,15 @@ const char* capitalize(const char* s){ return (const char*)result; } -char *str_lower(const char *s, size_t helper_length){ - char *result=pa_strdup(s, helper_length); +char *str_lower(const char *s, size_t length){ + char *result=pa_strdup(s, length); for(char* c=result; *c; c++) *c=(char)tolower((unsigned char)*c); return result; } -char *str_upper(const char *s, size_t helper_length){ - char *result=pa_strdup(s, helper_length); +char *str_upper(const char *s, size_t length){ + char *result=pa_strdup(s, length); for(char* c=result; *c; c++) *c=(char)toupper((unsigned char)*c); return result; @@ -652,6 +653,19 @@ char* rsplit(char* string, char delim) { return NULL; } +void pa_strncpy(char *dst, const char *src, size_t n){ + size_t left = n; + + if (left != 0 && src) { + while (--left != 0) { + if ((*dst++ = *src++) == '\0') + return; + } + } + if (n != 0) + *dst = '\0'; +} + #define STRCAT_STEP(str, len) if(str) { memcpy(ptr, str, len); ptr += len; } char *pa_strcat(const char *a, const char *b, const char *c) { @@ -741,26 +755,28 @@ FormatType format_type(const char* fmt){ } -const char* format(double value, const char* fmt) { +const char* format_double(double value, const char* fmt) { char local_buf[MAX_NUMBER]; int size=-1; if(fmt && strlen(fmt)){ switch(format_type(fmt)){ case FormatDouble: - size=snprintf(local_buf, sizeof(local_buf), fmt, value); - break; - case FormatInt: - size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value); + size=snprintf(local_buf, sizeof(local_buf), fmt, value); break; case FormatUInt: - size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value); + if(value >= 0){ // on Apple M1 (uint) is 0 + size=snprintf(local_buf, sizeof(local_buf), fmt, clip2uint(value)); + break; + } + case FormatInt: + size=snprintf(local_buf, sizeof(local_buf), fmt, clip2int(value)); break; case FormatInvalid: throw Exception(PARSER_RUNTIME, 0, "Incorrect format string '%s' was specified.", fmt); } } else - size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value); + return pa_itoa(clip2int(value)); if(size < 0 || size >= MAX_NUMBER-1){ // on win32 we manually reduce max size while printing throw Exception(PARSER_RUNTIME, 0, "Error occurred white executing snprintf with format string '%s'.", fmt); @@ -799,7 +815,7 @@ enum EscapeState { // @todo prescan for reduce required size (unescaped sting in 1 byte charset requires less memory usually) char* unescape_chars(const char* cp, int len, Charset* charset, bool js){ char* s=new(PointerFreeGC) char[len+1]; // must be enough (%uXXXX==6 bytes, max utf-8 char length==6 bytes) - char* dst=s; + XMLByte* dst=(XMLByte *)s; EscapeState escapeState=EscapeRest; uint escapedValue=0; int srcPos=0; @@ -845,7 +861,7 @@ char* unescape_chars(const char* cp, int escapedValue=(escapedValue << 4) + hex_value[c]; if(++jsCnt==4){ // transcode utf8 char to client charset (we can lost some chars here) - charset->store_Char((XMLByte*&)dst, (XMLCh)escapedValue, '?'); + charset->store_Char(dst, (XMLCh)escapedValue, '?'); escapeState=EscapeRest; } } else { @@ -897,7 +913,7 @@ size_t strpos(const char *str, const cha return (p==0)?STRING_NOT_FOUND:p-str; } -int remove_crlf(char* start, char* end) { +size_t remove_crlf(char* start, char* end) { char* from=start; char* to=start; bool skip=false;