string obj unicode efficiency
Closed / Fixed635200ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 24 years ago
Descriptionplain2002-11-07 21:51:46
The following code:
int len;
Tcl_Obj *objPtr;
objPtr = Tcl_NewObj();
Tcl_AppendToObj(objPtr, "foobar\n", 7);
Tcl_GetUnicodeFromObj(objPtr, &len);
Tcl_AppendToObj(objPtr, "foobar\n", 7);
Tcl_GetUnicodeFromObj(objPtr, &len);
Tcl_SetObjLength(objPtr, 0);
Tcl_AppendToObj(objPtr, "foobar\n", 7);
results in a crash because objPtr->bytes == NULL and
Tcl_AppendToObj tries to copy into it. The crash
occurs in AppendUtfToUtfRep here:
memcpy((VOID *) (objPtr->bytes + oldLength), (VOID
*) bytes,
(size_t) numBytes);
I don't know enough to be able to attempt to reproduce
this in pure Tcl, but my implementation of tip113 ran
into this problem...
vincentdarley Ticket [635200ffff]: 1 change
[History diff]2002-11-08 05:11:29
vincentdarleyplain2002-11-11 20:04:00
Logged In: YES user_id=32170 Also, if one replaces the last two calls with: Tcl_SetObjLength(objPtr, 0); Tcl_GetStringFromObj(objPtr, NULL); Tcl_AppendToObj(objPtr, "foobar\n", 7); Tcl_AppendToObj(objPtr, "foobar\n", 7); Tcl_AppendToObj(objPtr, "foobar\n", 7); Tcl_AppendToObj(objPtr, "foobar\n", 7); Tcl_AppendToObj(objPtr, "foobar\n", 7); then Tcl tramples all over memory.
vincentdarleyplain2002-11-12 05:10:19
Logged In: YES user_id=32170 Had a closer look at the code. The basic problem is this: Objects with a Unicode internal representation can have their string representation invalidated (as when more Unicode bytes are appended to the object), so that 'objPtr->bytes == NULL'. If, at that point, either Tcl_SetObjLength or Tcl_AttemptSetObjLength is called, the object is in a bad state. Neither of those functions knows what they are doing in that case (in fact, the first thing they do is invalidate the Unicode representation which is terribly wasteful!), and they end of placing the object in a state from which is cannot recover (both internal and string rep are gone).. I don't have enough expertise in this area to go much further, so I'd appreciate any help.
vincentdarleyplain2002-11-14 05:12:30
Logged In: YES user_id=32170 See stringObj.test test number 14.1 for how to reproduce this using the tcl test suite. The test is currently marked as knownBug.
vincentdarleyplain2003-01-15 01:58:04
Logged In: YES
user_id=32170
Upping priority to suggest this crash should be fixed for
8.4.2. Here is a sample script:
teststringobj set 1 foo
teststringobj getunicode 1
teststringobj append 1 bar -1
teststringobj getunicode 1
teststringobj append 1 bar -1
teststringobj setlength 1 0
teststringobj append 1 bar -1
teststringobj get 1
*crash*
vincentdarleyplain2003-01-17 18:29:35
Logged In: YES user_id=32170 Studied the code a bit more closely, and the fix is actually quite simpe. Tcl_SetObjLength is completely unaware of the fact that a Unicode StringObj may not have a valid objPtr->bytes, so by zeroing out the Unicode rep, that function has destroyed all available information about the object! Will commit a fix asap.
hobbsplain2003-01-21 01:10:59
Logged In: YES user_id=72656 Fix invalid. Still needs analysis.
andreas_kupriesplain2003-01-21 01:13:38
Logged In: YES user_id=75003 See also Tcl Bug 671138.
vincentdarleyplain2003-01-24 19:05:14
Logged In: YES user_id=32170 Fixed the bug properly, using Daniel's patch, plus appropriate modifications to Tcl_AttemptSetObjLength. Assigning to Daniel for his efficiency improvements, as discussed in email. Changed bug title and priority accordingly.
dasplain2003-02-18 18:51:12
Logged In: YES user_id=90580 c.f. bug 686782: attached tclStringObj.c patch and associated discussion
Log in (anonymous is fine) to add a comment.