From 40effa0f8aaae738487ed2384800e626995d776e Mon Sep 17 00:00:00 2001 From: dgp Date: Mon, 13 Apr 2020 02:10:45 +0000 Subject: Cherrypick partial fix. --- generic/tclUtil.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/generic/tclUtil.c b/generic/tclUtil.c index be80610..b3742f7 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -1573,8 +1573,7 @@ TrimRight( const char *trim, /* String of trim characters... */ int numTrim) /* ...and its length in bytes */ { - const char *p = bytes + numBytes; - int pInc; + const char *pp, *p = bytes + numBytes; /* Outer loop: iterate over string to be trimmed */ do { @@ -1582,8 +1581,8 @@ TrimRight( const char *q = trim; int bytesLeft = numTrim; - p = Tcl_UtfPrev(p, bytes); - pInc = TclUtfToUniChar(p, &ch1); + pp = Tcl_UtfPrev(p, bytes); + (void)TclUtfToUniChar(pp, &ch1); /* Inner loop: scan trim string for match to current character */ do { @@ -1600,9 +1599,9 @@ TrimRight( if (bytesLeft == 0) { /* No match; trim task done; *p is last non-trimmed char */ - p += pInc; break; } + p = pp; } while (p > bytes); return numBytes - (p - bytes); -- cgit v0.12 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