Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-07-16
16:44
Merge 9.0 - Fix [c9f0520f7e] check-in: de054c46e8 user: apnadkarni tags: trunk, main
16:36 • Closed ticket [c9f0520f7e]: ckalloc argument overflow in Tcl_SplitList with a st... artifact: 76695072ea user: apnadkarni
16:26
Merge 8.6. Fix [c9f0520f7e] - Tcl_SplitList memory size computation overflow. No test because not pr... check-in: cbf058c20b user: apnadkarni tags: core-9-0-branch
15:26
Fix [c9f0520f7e] - Tcl_SplitList memory overflow overwrite check-in: 1eebbe69f5 user: apnadkarni tags: core-8-6-branch
12:03 • Ticket [c9f0520f7e] ckalloc argument overflow in Tcl_SplitList with a string abo... artifact: 6a860c4b9b user: apnadkarni
2025-07-15
03:32 • Ticket [c9f0520f7e]: 3 changes artifact: 3afb4f0bee user: apnadkarni
03:31
Proposed fix for [c9f0520f7e] Tcl_SplitList crash. check-in: 7fd8657f04 user: apnadkarni tags: bug-c9f0520f7e
2025-07-09
15:45 • New ticket [c9f0520f7e] ckalloc argument overflow in Tcl_SplitList with a string... artifact: 3e0f6aaaad user: elio_du

ckalloc argument overflow in Tcl_SplitList with a string about 1

Closed / Fixedc9f0520f7e783fa926bf935295d204895f890499Created 1 year agoUpdated 1 year ago
Descriptionplain2025-07-09 15:45:39
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
-------------------------------------------
Referenced by4
apnadkarniplain2025-07-15 03:32:58
Proposed fix is in [7fd8657f04]. Note Tcl_SplitList will return an error, not panic, so caller needs to check return status.
apnadkarniplain2025-07-16 12:03:12
Thanks for the report and analysis. To reproduce without an extension,

```
# 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 [string length $str]
}

proc p {} [list dict for $str {} {}]
p
```
apnadkarniplain2025-07-16 16:36:23
Fixed in [1eebbe69f5].
Log in (anonymous is fine) to add a comment.