--- parser3/src/main/pa_string.C 2026/11/03 12:25:52 1.289 +++ parser3/src/main/pa_string.C 2026/11/29 13:17:21 1.291 @@ -12,7 +12,7 @@ #include "pa_charset.h" #include "pa_vregex.h" -volatile const char * IDENT_PA_STRING_C="$Id: pa_string.C,v 1.289 2026/11/03 12:25:52 moko Exp $" IDENT_PA_STRING_H; +volatile const char * IDENT_PA_STRING_C="$Id: pa_string.C,v 1.291 2026/11/29 13:17:21 moko Exp $" IDENT_PA_STRING_H; const String String::Empty; @@ -449,11 +449,20 @@ const String& String::right(Charset& cha if(charset.isUTF8()){ const XMLByte* src_begin=(const XMLByte*)cstr(); - const XMLByte* ptr=src_begin+self_length; + const XMLByte* src_end=src_begin+self_length; + const XMLByte* ptr=src_end; - // step back by n characters, continuation bytes (10xxxxxx) belong to the character before them - while(n-- && ptr>src_begin) - do ptr--; while(ptr>src_begin && (*ptr & 0xC0)==0x80); + // step back by n characters: continuation bytes (80-BF) belong to the leading byte before them, + // but only if it expects exactly that many, otherwise the byte is a character of its own (as length() counts it); + // the only exception is the last character cut off by the end of the string, length() counts it as a whole + while(n-- && ptr>src_begin) { + const XMLByte* lead=ptr-1; + while(lead>src_begin && (*lead & 0xC0)==0x80 && ptr-lead<6) // 6 bytes is the longest character + lead--; + size_t expected=lengthUTF8Char(*lead); + size_t present=ptr-lead; + ptr=(expected==present || (expected>present && ptr==src_end)) ? lead : ptr-1; + } from=ptr-src_begin; } else from=self_length>n ? self_length-n : 0; @@ -827,10 +836,10 @@ bool String::deserialize(size_t prolog_s memcpy(&body_length, cur, sizeof(body_length)); cur+=sizeof(body_length); in_buf-=sizeof(body_length); - if(in_bufbody_length) + if(!fragment_length || fragment_length>body_length-total_length /* the sum can overflow */ || !lang /* CORD_chars(0) is shorter */) return false; // file curruption // uchar needed to prevent propagating 0x80 bit to upper bytes langs.append(total_length, (String::Language)(uchar)lang, fragment_length); @@ -871,7 +880,8 @@ bool String::deserialize(size_t prolog_s if(total_length!=body_length) // length(all language fragments) vs length(letters) return false; - } + } else if(body_length) // letters without languages + return false; if(in_buf!=0) // some strange extra bytes return false;