Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-05-22
10:36 • Closed ticket [3837178c25]: Tcl_DStringSetObjLength(): TIP 660 out-of-bounds wri... artifact: 993e08e70a user: apnadkarni
2023-07-08
12:00 • Ticket [3837178c25]: 3 changes artifact: 99e4db7497 user: chrstphrchvz
2023-05-17
14:17 • Ticket [3837178c25]: 3 changes artifact: e255baf9fb user: apnadkarni
2023-05-07
20:41 • Ticket [3837178c25]: 4 changes artifact: 558b262cb0 user: jan.nijtmans
20:34
Merge 8.7. Restore Tcl_DStringSetLength() behavior, as suggested in [3837178c25] check-in: ca456c3719 user: jan.nijtmans tags: trunk, main
20:06
New testcase, contributed by @chrstphrchvz. Many thanks! See [3837178c25] check-in: 3de6304e70 user: jan.nijtmans tags: core-8-6-branch
02:39 • New ticket [3837178c25] Tcl_DStringSetObjLength(): TIP 660 out-of-bounds write</... artifact: 01c37693a7 user: chrstphrchvz

Tcl_DStringSetObjLength(): TIP 660 out-of-bounds write

Closed / Fixed3837178c2548d802c3946cc0918fedfcf6699e57Created 4 years agoUpdated 195 days ago
Descriptionwiki2023-05-07 02:39:51
Tcl_DStringSetLength() previously treated negative length as 0 (an undocumented behavior some found concerning: [2613599]). The check for negative length was removed when TIP 494 made length unsigned (originally in [6d05288cf774], partially restored in [c0829cbacc86], and removed again in [84a5f246b739]). TIP 660 changed length back to signed without restoring the check for negative length, which at a minimum allows writing '\0' out-of-bounds.

Please consider adding the following (or similar) to tests/dstring.test in all maintained branches, regardless of what is done about this ticket:

test dstring-4.3 {truncation} -constraints testdstring -setup {
    testdstring free
} -body {
    testdstring append "xwvut" -1
    # Pass a negative length to Tcl_DStringSetLength();
    # if not caught, causing '\0' to be written out-of-bounds,
    # try corrupting dsPtr->length which begins
    # 2*sizeof(Tcl_Size) bytes before dsPtr->staticSpace[],
    # so that the result is -256 (on little endian systems)
    # rather than e.g. -8 or -16.
    # (sizeof(Tcl_Size) does not seem to be available via Tcl,
    # so assume sizeof(Tcl_Size) == sizeof(void*) for Tcl 9.)
    testdstring trunc [expr {-2*([package vsatisfies $tcl_version 9.0-]
	? $tcl_platform(pointerSize) : 4)}]
    list [testdstring get] [testdstring length]
} -cleanup {
    testdstring free
} -result {{} 0}
Referenced by2
  • ca456c3719Merge 8.7. Restore Tcl_DStringSetLength() behavior, as suggested in [3837178c25] check-in2023-05-07 20:34:18
  • 3de6304e70New testcase, contributed by @chrstphrchvz. Many thanks! See [3837178c25] check-in2023-05-07 20:06:50
jan.nijtmanswiki2023-05-07 20:41:20

All branches have this test-case now, and `Tcl_DStringSetLength()` is now restored in 9.0. Thanks! Let's think about it, how it should be

apnadkarniplain2023-05-17 14:17:28
I think this particular check is already on the [bug-c9663296fd] branch along with other missing overflow checks in the DString code.
chrstphrchvzwiki2023-07-08 12:00:03

The check appeared on the bug-c9663296fd branch as part of a merge from main: [b4827791dced]

I am fine with this ticket being closed if there is nothing else to do for it. I have not encountered nor looked for similar issues.

apnadkarniplain2026-05-22 10:36:31
Both the check and the test are present in current versions so closing.
Log in (anonymous is fine) to add a comment.