clock: use-after-free when a callback clears the clock caches, and memory errors on malformed locale data
Memory errors in the C clock engine: a use-after-free when a callback clears the clock caches, an out-of-bounds read and a double free in TclStrIdxTree, and a NULL dereference on malformed locale data. They need a script that installs its own callbacks or locale data; outside data does not reach them.
Affected: 9.0.4, 9.1.0 and trunk (as of 2026-11-13, 12c60e9c42). 8.6 is not affected, because its clock formatting and scanning are Tcl code. All PoCs below are for trunk. Fixes follow in a comment.
1. Use-after-free when a callback clears the clock caches during clock format or clock scan
ClockLocalizeFormat, TclClockMCDict and the scan and format token loops hold the locale catalogue (opts->mcDictObj), the normalised locale, the normalised time zone (opts->timezoneObj), the format storage and the client data by plain pointer while they run Tcl callbacks (LocalizeFormat, mcget, SetupTimeZone, TZData reads). A callback that runs ::tcl::unsupported::clock::configure -clear or ::tcl::clock::ClearCaches, or that deletes the clock commands, frees them:
clock format 0 -gmt 1
trace add variable ::tcl::clock::LocFmtMap read {apply {args {
::tcl::unsupported::clock::configure -clear
}}}
clock format 0 -format %x -locale fr -gmt 1
SUMMARY: AddressSanitizer: heap-use-after-free tclClockFmt.c:951 in ClockLocalizeFormat
The same pattern gives a NULL dereference in Tcl_DictObjPut from TclClockMCDict (tclClock.c:779) with a msgcat loadcmd for ::tcl::clock. With a TZData read trace during clock scan ... %Z, it gives a use-after-free of the token array (tclClockFmt.c:2421) or of the client data (tclClock.c:1324). The time zone object is freed the same way when only the caches own it, which is the case once an equal zone name has been used before; %Z in clock format and the conversion at the end of clock scan then read it in ClockGetTZData. Two smaller problems are close by: TclClockMCDict uses the mcget result after Tcl_ResetResult, and ClockLocalizeFormat leaks a smart reference.
2. TclStrIdxTree: out-of-bounds read with U+023A or U+023E in locale names, double free on allocation failure
TclStrIdxTreeBuildFromList lowers keys with Tcl_UtfToLower, which keeps U+023A and U+023E because their lowercase forms need more bytes. Keys that match case-insensitively then have prefixes of different byte lengths, and TclStrIdxTreeSearch applies an offset measured in one key to its siblings:
clock format 0 -gmt 1
namespace eval ::tcl::clock {
::msgcat::mcset xx MONTHS_FULL [list ⱥⱥy ȺȺz March April May June July \
August September October November December]
}
catch {clock scan "ⱥⱥq 2000" -format "%B %Y" -locale xx -gmt 1}
SUMMARY: AddressSanitizer: heap-buffer-overflow tclStrIdxTree.h:130 in TclUtfFindEqualNCInLwr
No shipped msgs/*.msg file contains these characters. Matching is wrong as well: input ⱥⱥz does not match the key ȺȺz.
On an allocation failure, TclStrIdxTreeBuildFromList frees the tree items but leaves firstPtr set. Its callers (ClockMCGetListIdxTree, ClockMCGetMultiListIdxTree) then free the object, and the internal-rep free proc frees the same items again.
3. NULL dereference on malformed LOCALE_ERAS or LOCALE_NUMERALS, and format errors that are dropped or have no message
ClockFmtToken_LocaleERAYear_Proc does not check the NULL that Tcl_ListObjIndex returns for a short era row or a LOCALE_NUMERALS list under 100 entries:
clock format 0 -gmt 1
namespace eval ::tcl::clock {
::msgcat::mcset xx LOCALE_ERAS {{-1000000000000}}
}
clock format 0 -format %EC -locale xx -gmt 1
tclClockFmt.c:3074:6: runtime error: member access within null pointer of type 'Tcl_Obj'
Related: TclClockFormat jumps to done instead of error when a CTOKT_INT format procedure fails, so clock format returns the output so far with TCL_OK. With LOCALE_ERAS {{-1000000000000 E notanumber}}, clock format 0 -format "<%Ey>" returns < (8.6 reports the error).
A list shorter than the format needs in the other locale lookups of TclClockFormat (MONTHS_* and DAYS_OF_WEEK_*, and LOCALE_NUMERALS for the %O conversions) makes clock format fail with an empty error message and error code NONE. With MONTHS_ABBREV {a b}, clock format 1700000000 -format %b -gmt 1 fails that way; 8.6 formats an empty name.
c11b12c9a3Fix [8911644dc2]: clock: check locale lists for missing entries, report format errors instead of truncating check-in2026-11-13 20:08:10ba967169caFix [8911644dc2]: TclStrIdxTree: reset the list after freeing it on allocation failure (double free) check-in2026-11-13 20:08:09537cedd17eFix [8911644dc2]: TclStrIdxTree: lower keys per character, so U+023A and U+023E keep prefix lengths equal check-in2026-11-13 20:08:0890df9d4d43Fix [8911644dc2]: clock: hold references on catalogue, locale, time zone, client data and format storage while callbacks run check-in2026-11-13 20:08:08
Fixes on branch bug-891164-clock-memory-errors.
Each fix is its own commit, so that it can be picked to other branches on its own; the commits do not depend on each other. The fixes are for 9.x only; 8.6 is not changed. Tests are in tests/clock.test (new groups clock-81 and clock-82, to be renumbered as you like).
Item 1 (commit 01). References are taken for the duration of each command on the catalogue, the locale, the time zone, the client data and the format storage, and released in a new ClockFreeFmtScnArgs. The time zone is set through a new ClockSetOptsTimezone, also where clock scan changes it (%Z, and a zone in free-form input). Writes to a shared catalogue now go through a temporary smart reference (TclClockMCDictPut). The commit also fixes the mcget result used after Tcl_ResetResult and the leaked smart reference. One case is left: a callback that edits ::tcl::clock::mcMergedCat in place can still change catalogue values that C holds by pointer. Tests clock-82.1 to clock-82.6, each in a child interpreter.
Item 2 (commits 02, 03). Commit 02 builds the keys lowercased character by character with Tcl_UniCharToLower. Keys without U+023A or U+023E come out byte for byte as before. Test clock-82.7 fails by result even without a sanitiser. Commit 03 resets firstPtr and lastPtr after freeing the items. No test, since the suite cannot make an allocation fail.
Item 3 (commit 04). NULL checks with the error "malformed locale data" (CLOCK malformedLocaleData, modelled on malformedTimeZoneData), and the goto done becomes goto error. The same error replaces the empty message for short MONTHS_*, DAYS_OF_WEEK_* and LOCALE_NUMERALS lists in TclClockFormat. The message and error code are only a suggestion. A quiet fallback (plain digits for a short LOCALE_NUMERALS list, an empty name as in 8.6) would also do. Tests clock-81.1 to clock-81.4, clock-81.8, clock-81.9.
Test runs
The four commits on the branch (from trunk 12c60e9c42) were tested with ASan and UBSan, with UBSan's pointer-overflow check on:
clock.test: 8783 tests, 0 failed, no sanitiser report.clock-ivm.test,msgcat.test,safe.testandsafe-stock.test: no failures and no sanitiser reports.
They were also run on a TCL_MEM_DEBUG build. clock.test passes there. Over 12000 rounds of clock format, scan and add in three locales, memory stays flat when the caches are cleared between rounds. Without clearing, it grows by exactly as much as unpatched trunk does over the same rounds, so the new references balance.