Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 3e0f6aaaad97c97edfe94e082a8bfe208079594650932a315bdb76c3125cbfcb
Ticket: c9f0520f7e783fa926bf935295d204895f890499
ckalloc argument overflow in Tcl_SplitList with a string about 1
User & Date: elio_du on 2025-07-09 15:45:39
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-plain"
  4. comment changed to:
    The ckalloc tries to allocate size * sizeof(char*) + length + 1, if since sizeof(char*) in 64bit machine is 8 bytes, for the extreme case string is "0 1 2 3 4 5 ...", size can be so size is half size of string itself, so it takes much smaller string to cause size * sizeof(char*) + length + 1 to hit the 2^32 limit.
    
    This issue is not presenting in Tcl 9 as datatype is now 64bit, still would rather Tcl_panic with some error message instead of SEGV directly.
    
    To reproduce:
    ---------------------------------------------
    
    static int Crash_Cmd(ClientData cdata, Tcl_Interp *interp, int objc,
                         Tcl_Obj *const objv[]) {
      if (objc < 2) {
        return TCL_ERROR;
      }
      const char * str = Tcl_GetString(objv[1]);
      char **list_elements = NULL;
    
    #if TCL_MAJOR_VERSION == 9
      long int list_sz = 0;
      (void)Tcl_SplitList(interp, str, &list_sz, (CONST86 char ***)&list_elements);
    #else
      int list_sz = 0;
      (void)Tcl_SplitList(interp, str, &list_sz, (CONST84 char ***)&list_elements);
    #endif
    
      printf("SPLITTED INTO %d elements\n", list_sz);
    
      for (long int i = 0; i < list_sz; i ++) {
        if (i % 1024 == 0) {
          printf("%ld:%s\n", i, list_elements[i]);
        }
      }
    
      Tcl_SetObjResult(interp, Tcl_NewStringObj("Crash", -1));
      return TCL_OK;
    }
    
    /*
      * Crash_Init -- Called when Tcl loads your extension.
      */
     int DLLEXPORT
     Crash_Init(Tcl_Interp *interp)
     {
       if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
         return TCL_ERROR;
       }
       /* changed this to check for an error - GPS */
       if (Tcl_PkgProvide(interp, "Crash", "1.0") == TCL_ERROR) {
         return TCL_ERROR;
       }
       Tcl_CreateObjCommand(interp, "crash", Crash_Cmd, NULL, NULL);
       return TCL_OK;
     }
    -----------------------------------------
    
    Test.tcl:
    ----------------------------------------
    load ./libcrash.so
    
    # create a huge string
    # crash by 26 in 8.6.11
    set limit 26
    set str "0 1 2 3 4 5 6 7 8 9 "
    for { set i 0 } { $i < $limit } { incr i } {
      set str "$str $str"
      puts "doubled $i: [expr [string length $str] / 1024.0 / 1024] MB"
    }
    
    crash "$str"
    exit
    -------------------------------------------
    
  5. foundin changed to: "8.6.11"
  6. is_private changed to: "0"
  7. login: "elio_du"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "36ae4f4121e0beea41dc8655c6d0658cff1db08d"
  10. resolution changed to: "None"
  11. severity changed to: "Critical"
  12. status changed to: "Open"
  13. submitter changed to: "elio_du"
  14. subsystem changed to: "11. Conversions from String"
  15. title changed to:
    ckalloc argument overflow in Tcl_SplitList with a string about 1
    
  16. type changed to: "Bug"