summaryrefslogtreecommitdiffstats
path: root/generic/tclStringObj.c
diff options
context:
space:
mode:
authordgp <dgp@users.sourceforge.net>2020-04-09 22:27:37 (GMT)
committerdgp <dgp@users.sourceforge.net>2020-04-09 22:27:37 (GMT)
commitab39084d894caaf7e9a5e362169fd1ddcdb0460c (patch)
tree72af6f4617899720e199be1e83d43b8d6a4112d4 /generic/tclStringObj.c
parent18e7960200db745bdfb8936ed33e4cc7cafad557 (diff)
downloadtcl-ab39084d894caaf7e9a5e362169fd1ddcdb0460c.zip
tcl-ab39084d894caaf7e9a5e362169fd1ddcdb0460c.tar.gz
tcl-ab39084d894caaf7e9a5e362169fd1ddcdb0460c.tar.bz2
Bulletproof the calls to Tcl_UtfPrev in Tcl_AppendLimitedToObj.
Diffstat (limited to 'generic/tclStringObj.c')
-rw-r--r--generic/tclStringObj.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c
index aeb4285..c3c85dc 100644
--- a/generic/tclStringObj.c
+++ b/generic/tclStringObj.c
@@ -1119,12 +1119,7 @@ Tcl_AppendLimitedToObj(
{
String *stringPtr;
int toCopy = 0;
-
- if (Tcl_IsShared(objPtr)) {
- Tcl_Panic("%s called with shared object", "Tcl_AppendLimitedToObj");
- }
-
- SetStringFromAny(NULL, objPtr);
+ int eLen = 0;
if (length < 0) {
length = (bytes ? strlen(bytes) : 0);
@@ -1132,6 +1127,9 @@ Tcl_AppendLimitedToObj(
if (length == 0) {
return;
}
+ if (limit <= 0) {
+ return;
+ }
if (length <= limit) {
toCopy = length;
@@ -1139,8 +1137,12 @@ Tcl_AppendLimitedToObj(
if (ellipsis == NULL) {
ellipsis = "...";
}
- toCopy = (bytes == NULL) ? limit
- : Tcl_UtfPrev(bytes+limit+1-strlen(ellipsis), bytes) - bytes;
+ eLen = strlen(ellipsis);
+ while (eLen > limit) {
+ eLen = Tcl_UtfPrev(ellipsis+eLen, ellipsis) - ellipsis;
+ }
+
+ toCopy = Tcl_UtfPrev(bytes+limit+1-eLen, bytes) - bytes;
}
/*
@@ -1149,6 +1151,11 @@ Tcl_AppendLimitedToObj(
* objPtr's string rep.
*/
+ if (Tcl_IsShared(objPtr)) {
+ Tcl_Panic("%s called with shared object", "Tcl_AppendLimitedToObj");
+ }
+
+ SetStringFromAny(NULL, objPtr);
stringPtr = GET_STRING(objPtr);
if (stringPtr->hasUnicode != 0) {
AppendUtfToUnicodeRep(objPtr, bytes, toCopy);
@@ -1162,9 +1169,9 @@ Tcl_AppendLimitedToObj(
stringPtr = GET_STRING(objPtr);
if (stringPtr->hasUnicode != 0) {
- AppendUtfToUnicodeRep(objPtr, ellipsis, -1);
+ AppendUtfToUnicodeRep(objPtr, ellipsis, eLen);
} else {
- AppendUtfToUtfRep(objPtr, ellipsis, -1);
+ AppendUtfToUtfRep(objPtr, ellipsis, eLen);
}
}