Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-11-04
15:44 • Ticket [824752f10e] Crash in Tcl_ListObjReplace status still Closed with 5 o... artifact: b8b898c896 user: jan.nijtmans
14:58 • Closed ticket [824752f10e]. artifact: fd7618eb45 user: gahr
14:56
[824752f10e] Use LIST_MAX instead of INT_MAX for unknown number of elements to the end of the list. check-in: 24d9077003 user: dgp tags: trunk
14:48
[824752f10e] More robust, portable check for integer overflow. check-in: cf3aa0440e user: dgp tags: trunk
14:40
[824752f10e] More robust, portable check for integer overflow. check-in: b2a4266498 user: dgp tags: core-8-6-branch
14:37
[824752f10e] More robust, portable check for integer overflow. check-in: 9b1cb4c030 user: dgp tags: core-8-5-branch
14:24
merge core-8-5-branch ([824752f10e] Avoid calling Tcl_SetObjResult if interp is NULL) check-in: 16efbc4dbf user: gahr tags: trunk
14:23
merge core-8-5-branch ([824752f10e] Avoid calling Tcl_SetObjResult if interp is NULL) check-in: e7a9d70cd2 user: gahr tags: core-8-6-branch
14:21
[824752f10e] Avoid calling Tcl_SetObjResult if interp is NULL check-in: b6601d604f user: gahr tags: core-8-5-branch
13:55 • Ticket [824752f10e] Crash in Tcl_ListObjReplace status still Open with 4 oth... artifact: 9eda8c8f07 user: gahr
13:47 • New ticket [824752f10e]. artifact: 3205b6581b user: gahr

Crash in Tcl_ListObjReplace

Closed / Fixed824752f10e0cd14625d2d476ee1ebbf8e7f5fd5cCreated 10 years agoUpdated 10 years ago
Descriptionplain2016-11-04 13:47:32
I am seeing a crash in Tcl_ListObjReplace, caused by a combination of invalid arguments and a NULL interp.

This is the stacktrace:

ore file '/home/gahr/tcl/unix/../../tclsh.core' (aarch64) was loaded.                                                                                            
(lldb) bt                                                                                                                                                         
* thread #1: tid = 101137, 0x0000000040547678 libtcl8.7.so`Tcl_SetObjResult + 12, name = 'tclsh', stop reason = signal SIGSEGV                                    
  * frame #0: 0x0000000040547678 libtcl8.7.so`Tcl_SetObjResult + 12                                                                                               
    frame #1: 0x000000004052ce1c libtcl8.7.so`Tcl_ListObjReplace + 252                                                                                            
    frame #2: 0x00000000404f8520 libtcl8.7.so`TclCompileEnsemble + 1312                                                                                           
    frame #3: 0x00000000404f93c0 libtcl8.7.so`TclAttemptCompileProc + 212                                                                                         
    frame #4: 0x00000000404e20ec libtcl8.7.so`TclCompileScript + 1588                                                                                             
    frame #5: 0x00000000404e155c libtcl8.7.so`TclSetByteCodeFromAny + 164                                                                                         
    frame #6: 0x00000000404fcb30 libtcl8.7.so`TclCompileObj + 500                                                                                                 
    frame #7: 0x000000004047e7f4 libtcl8.7.so`TclNREvalObjEx + 476                                                                                                
    frame #8: 0x0000000040529e84 libtcl8.7.so`TclNREvalFile + 896                                                                                                 
    frame #9: 0x0000000040492a14 libtcl8.7.so`TclNRSourceObjCmd + 188                                                                                             
    frame #10: 0x000000004047c75c libtcl8.7.so`Tcl_EvalObjv + 104                                                                                                 
    frame #11: 0x000000004047db90 libtcl8.7.so`TclEvalEx + 2032                                                                                                   
    frame #12: 0x00000000004009b4 tclsh`Tcl_AppInit + 20                                                                                                          
    frame #13: 0x0000000040530540 libtcl8.7.so`Tcl_MainEx + 440                                                                                                   
    frame #14: 0x0000000000400990 tclsh`main + 48                                                                                                                 
    frame #15: 0x00000000004008c0 tclsh`__start + 360                                                                                                             
    frame #16: 0x0000000040412658 ld-elf.so.1 at rtld_start.S:41


The fix is in two parts.

1) Make sure Tcl_SetObjResult is not called if interp is NULL

Index: generic/tclListObj.c
==================================================================
--- generic/tclListObj.c
+++ generic/tclListObj.c
@@ -905,12 +905,14 @@

        count = numElems - first;
     }

     if (objc > LIST_MAX - (numElems - count)) {
-       Tcl_SetObjResult(interp, Tcl_ObjPrintf(
-               "max length of a Tcl list (%d elements) exceeded", LIST_MAX));
+       if (interp != NULL) {
+           Tcl_SetObjResult(interp, Tcl_ObjPrintf(
+                   "max length of a Tcl list (%d elements) exceeded", LIST_MAX));
+       }
        return TCL_ERROR;
     }
     isShared = (listRepPtr->refCount > 1);
     numRequired = numElems - count + objc; /* Known <= LIST_MAX */
     needGrow = numRequired > listRepPtr->maxElemCount;


2) Avoid calling Tcl_ListObjReplace with an invalid combination of arguments

Index: generic/tclEnsemble.c
==================================================================
--- generic/tclEnsemble.c
+++ generic/tclEnsemble.c
@@ -3133,11 +3133,11 @@
        /*
         * The length of the "replaced" list must be depth-1.  Trim back
         * any extra elements that might have been appended by failing
         * pathways above.
         */
-       (void) Tcl_ListObjReplace(NULL, replaced, depth-1, INT_MAX, 0, NULL);
+       (void) Tcl_ListObjReplace(NULL, replaced, depth-1, LIST_MAX, 0, NULL);

        /*
         * TODO: Reconsider whether we ought to call CompileToInvokedCommand()
         * when depth==1.  In that case we are choosing to emit the
         * INST_INVOKE_REPLACE bytecode when there is in fact no replacing


I am happy to commit this myself, but I would rather seek opinion on part 2) first.
Referenced by7
  • 24d9077003[824752f10e] Use LIST_MAX instead of INT_MAX for unknown number of elements to the end of the list. check-in2016-11-04 14:56:19
  • cf3aa0440e[824752f10e] More robust, portable check for integer overflow. check-in2016-11-04 14:48:36
  • b2a4266498[824752f10e] More robust, portable check for integer overflow. check-in2016-11-04 14:40:54
  • 9b1cb4c030[824752f10e] More robust, portable check for integer overflow. check-in2016-11-04 14:37:50
  • 16efbc4dbfmerge core-8-5-branch ([824752f10e] Avoid calling Tcl_SetObjResult if interp is NULL) check-in2016-11-04 14:24:42
  • e7a9d70cd2merge core-8-5-branch ([824752f10e] Avoid calling Tcl_SetObjResult if interp is NULL) check-in2016-11-04 14:23:43
  • b6601d604f[824752f10e] Avoid calling Tcl_SetObjResult if interp is NULL check-in2016-11-04 14:21:04
gahr (claiming to be [email protected])plain2016-11-04 13:55:39
I should mention that this is happening only on FreeBSD 12-CURRENT, arm64 (ARMv8).
gahr Closed ticket [824752f10e]. [History diff]2016-11-04 14:58:48
jan.nijtmans Ticket [824752f10e]: 5 changes [History diff]2016-11-04 15:44:33
Log in (anonymous is fine) to add a comment.