Skip to content
Tcl Source Code View Ticket
Login

View Ticket

lsort crash

Closed / Fixed1675116fffffffffffffffffffffffffffffffffCreated 20 years agoUpdated 20 years ago
Descriptionplain2007-03-06 17:23:35
Following script crashes:

set l {1 2 3}
proc compare args {
  string length $::l
}
lsort -command compare $l
afreddplain2007-03-08 00:54:44
Logged In: YES 
user_id=1386588
Originator: NO


Here's a fix. It simply does a Tcl_DuplicateObj
of the input list and sorts that (but only for
calls using -command since this is the only way to
hit the problem).
[PS try doing the given example using -indices.
No crash, but some odd looking indexes!]


--- tclCmdIL.c-orig2007-03-07 17:42:32.856819800 +0000
+++ tclCmdIL.c2007-03-07 17:47:05.580806000 +0000
@@ -4143,7 +4143,7 @@
     int i, index, unique, indices;
     Tcl_Obj *resultPtr;
     int length;
-    Tcl_Obj *cmdPtr, **listObjPtrs;
+    Tcl_Obj *cmdPtr, *listPtr, **listObjPtrs;
     SortElement *elementArray;
     SortElement *elementPtr;
     SortInfo sortInfo;/* Information about this sort that needs to
@@ -4302,9 +4302,14 @@
 }
 Tcl_ListObjAppendElement(interp, newCommandPtr, Tcl_NewObj());
 sortInfo.compareCmdPtr = newCommandPtr;
+/* Fixes [SW#1675116] */
+listPtr = Tcl_DuplicateObj(objv[objc-1]);
+Tcl_IncrRefCount(listPtr);
+    } else {
+listPtr = objv[objc-1];
     }
 
-    sortInfo.resultCode = Tcl_ListObjGetElements(interp, objv[objc-1],
+    sortInfo.resultCode = Tcl_ListObjGetElements(interp, listPtr,
     &length, &listObjPtrs);
     if (sortInfo.resultCode != TCL_OK || length <= 0) {
 goto done;
@@ -4353,6 +4358,7 @@
   done:
     if (sortInfo.sortMode == SORTMODE_COMMAND) {
 Tcl_DecrRefCount(sortInfo.compareCmdPtr);
+Tcl_DecrRefCount(listPtr);
 sortInfo.compareCmdPtr = NULL;
     }
     if (sortInfo.indexc > 1) {
dkf Ticket [1675116fff]: 1 change [History diff]2007-03-08 06:04:10
dkfplain2007-03-09 23:41:06
Logged In: YES 
user_id=79902
Originator: NO

Fixed in HEAD. Needs backport.
dkfplain2007-03-10 21:58:20
Logged In: YES 
user_id=79902
Originator: NO

Fixed in 8.4 branch.
Log in (anonymous is fine) to add a comment.