From d1a1538c301096e5be710310329fa391a61a3b33 Mon Sep 17 00:00:00 2001 From: dgp Date: Mon, 13 Apr 2020 02:28:24 +0000 Subject: [c61818e4c9] [string trimright] robustly handle backing up over incomplete or malformed byte sequences. --- generic/tclUtil.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/generic/tclUtil.c b/generic/tclUtil.c index b3742f7..d40cbeb 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -1570,8 +1570,12 @@ static inline int TrimRight( const char *bytes, /* String to be trimmed... */ int numBytes, /* ...and its length in bytes */ + /* Calls to TclUtfToUniChar() in this routine + * rely on (bytes[numBytes] == '\0'). */ const char *trim, /* String of trim characters... */ int numTrim) /* ...and its length in bytes */ + /* Calls to TclUtfToUniChar() in this routine + * rely on (trim[numTrim] == '\0'). */ { const char *pp, *p = bytes + numBytes; @@ -1579,10 +1583,13 @@ TrimRight( do { Tcl_UniChar ch1; const char *q = trim; - int bytesLeft = numTrim; + int pInc = 0, bytesLeft = numTrim; pp = Tcl_UtfPrev(p, bytes); - (void)TclUtfToUniChar(pp, &ch1); + do { + pp += pInc; + pInc = TclUtfToUniChar(pp, &ch1); + } while (pp + pInc < p); /* Inner loop: scan trim string for match to current character */ do { -- cgit v0.12