summaryrefslogtreecommitdiffstats
path: root/generic/tclStringObj.c
diff options
context:
space:
mode:
authordgp <dgp@users.sourceforge.net>2009-03-30 17:47:08 (GMT)
committerdgp <dgp@users.sourceforge.net>2009-03-30 17:47:08 (GMT)
commit6e9a5f76b1bf9a5faaf35ff4687980407d2177da (patch)
treec6772d718863ab5f2fcd7ed6a98641fec120511b /generic/tclStringObj.c
parent7c990e6c9a8147fc8a2f658142d9179893a5f0c9 (diff)
downloadtcl-6e9a5f76b1bf9a5faaf35ff4687980407d2177da.zip
tcl-6e9a5f76b1bf9a5faaf35ff4687980407d2177da.tar.gz
tcl-6e9a5f76b1bf9a5faaf35ff4687980407d2177da.tar.bz2
* generic/tclStringObj.c: Added protections from invalid memory
* generic/tclTestObj.c: accesses when we append (some part of) * tests/stringObj.test: a Tcl_Obj to itself. Added the appendself and appendself2 subcommands to the [teststringobj] testing command and added tests to the test suite. [Bug 2603158]
Diffstat (limited to 'generic/tclStringObj.c')
-rw-r--r--generic/tclStringObj.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c
index 32197a7..6490bc6 100644
--- a/generic/tclStringObj.c
+++ b/generic/tclStringObj.c
@@ -33,7 +33,7 @@
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclStringObj.c,v 1.32.2.7 2009/03/21 02:53:18 dgp Exp $ */
+ * RCS: @(#) $Id: tclStringObj.c,v 1.32.2.8 2009/03/30 17:47:09 dgp Exp $ */
#include "tclInt.h"
@@ -1242,6 +1242,17 @@ AppendUnicodeToUnicodeRep(objPtr, unicode, appendNumChars)
numChars = stringPtr->numChars + appendNumChars;
if (STRING_UALLOC(numChars) >= stringPtr->uallocated) {
+ /*
+ * Protect against case where unicode points into the existing
+ * stringPtr->unicode array. Force it to follow any relocations
+ * due to the reallocs below.
+ */
+ int offset = -1;
+ if (unicode >= stringPtr->unicode && unicode <= stringPtr->unicode
+ + 1 + stringPtr->uallocated / sizeof(Tcl_UniChar)) {
+ offset = unicode - stringPtr->unicode;
+ }
+
stringPtr->uallocated = STRING_UALLOC(2 * numChars);
tmpString = (String *) attemptckrealloc((char *)stringPtr,
STRING_SIZE(stringPtr->uallocated));
@@ -1254,6 +1265,11 @@ AppendUnicodeToUnicodeRep(objPtr, unicode, appendNumChars)
}
stringPtr = tmpString;
SET_STRING(objPtr, stringPtr);
+
+ /* Relocate unicode if needed; see above. */
+ if (offset >= 0) {
+ unicode = stringPtr->unicode + offset;
+ }
}
/*
@@ -1399,6 +1415,16 @@ AppendUtfToUtfRep(objPtr, bytes, numBytes)
stringPtr = GET_STRING(objPtr);
if (newLength > (int) stringPtr->allocated) {
+ /*
+ * Protect against case where unicode points into the existing
+ * stringPtr->unicode array. Force it to follow any relocations
+ * due to the reallocs below.
+ */
+ int offset = -1;
+ if (bytes >= objPtr->bytes
+ && bytes <= objPtr->bytes + objPtr->length) {
+ offset = bytes - objPtr->bytes;
+ }
/*
* There isn't currently enough space in the string representation
@@ -1419,6 +1445,11 @@ AppendUtfToUtfRep(objPtr, bytes, numBytes)
Tcl_SetObjLength(objPtr, newLength + growth);
}
+
+ /* Relocate bytes if needed; see above. */
+ if (offset >=0) {
+ bytes = objPtr->bytes + offset;
+ }
}
/*