Memory corruption false positive with TCL_MEM_DEBUG
Tcl built with TCL_MEM_DEBUG triggers a panic when decrementing the reference count of 0x61616161. This is a perfectly valid reference count in Tcl 9.
Encountered while testing lrepeat
% llength [lrepeat 0x61616162 x]
file = d:\src\tcltk\trunk\tcl\win\..\generic\tclListObj.c, line = 440
decrementing refCount of previously disposed object
5495155636Fix [7e3f26c748]: with TCL_MEM_DEBUG, the "previously disposed object" check could no longer fire, because freed memory was filled with 0x61 while [f505b17748] changed the marker to 0xE8. Fill freed memory with 0xE8 and use the same marker everywhere (also in tclDisassemble.c and tclPreserve.c, which still compared with 0x61616161). check-in2026-10-18 09:45:18f505b17748Bug [7e3f26c748] - TCL_MEM_DEBUG false positive check-in2023-04-02 13:23:33
Note on 8.x and 32-bit 9.0 this particular test case does not succeed as lists of that size are not permitted ``` % llength [lrepeat 0x61616162 x] max length of a Tcl list (536870906 elements) exceeded ```
Fixed for 9.0 in [f505b17748]. Leaving open for time being to see if it can be reproduced in 8.x or 32-bit Tcl. If so, the 0x61616161 pattern may have to change one with high bit set.
In addition to [f505b17748] the codebase features 4 more occurrences of the same 0x61616161 special value, one in tclDisassemble and three in tclPreserve. Shouldn't these be considered to be renamed FREEDREFCOUNTFILLER?
You're right. Also, rethinking the 32-bit case, perhaps the issue could arise there as well for other object types. Maybe the 61 marker should be changed to something like E8 (high bit set, printable) to reduce the chances of this occurring in 32-bit systems (6161616161616161 is basically impossible in 64-bit systems (unless one artificially keeps incrementing for no reason) but 61616161 may be feasible on 32-bits with valid references. I'll wait in case someone else has comments/ideas to fix both these. Keeping ticket open for now.
[f505b17748] changed the expected refCount of freed objects to 0xE8..., but Tcl_DbCkfree still fills freed memory with 0x61, so the "decrementing refCount of previously disposed object" panic can no longer fire; a use-after-free decrement is caught only by the threaded check ("Tcl_Obj allocated in another thread", without file and line). The 0x61616161 checks in tclDisassemble.c and tclPreserve.c never matched on 64-bit either.
Proposed fix in branch memdebug-freed-marker: Tcl_DbCkfree fills freed memory with 0xE8, as suggested here, and all five checks use the same marker. A use-after-free decrement now panics with the file and line again.