Tcl Source Code

View Ticket
Login
Ticket UUID: f894a85b9a4412d77d4f3234e5a244b7b4d476a2
Title: Free-form clock scan silently wraps numbers that do not fit into an int (regression in 9.0)
Type: Bug Created on: 2026-11-14 08:05:35
Submitter: serhiy.storchaka Assigned to: nobody
Subsystem: 06. Time Measurement Severity: Important
Priority: 5 Medium Last modified: 2026-11-24 17:44:31
Status: Closed Closed by: serhiy.storchaka
Resolution: Fixed Closed on: 2026-11-24 17:44:31
Version: 9.0.5, 9.1.1
Description:

Since the clock rewrite in Tcl 9.0 (TIP 688), the free-form parser of clock scan reads numbers as 64-bit integers, but stores them into the int fields of the date (hour, minutes, day, month, year, day ordinal). A number that does not fit into an int is truncated, and the result is often a valid value, so a wrong date is returned without error:

Input 9.0.5 and 9.1.1 8.6.18
clock scan 12:4294967301:00 12:05:00 syntax error
clock scan 4294967308:00:00 12:00:00 syntax error
clock scan 4294967297/02/2026 2026-01-02 syntax error
clock scan 02/4294967297/2026 2026-02-01 syntax error
clock scan 02/03/4294969322 2026-02-03 syntax error
clock scan "-4294967297 Monday" 1969-12-29 (a Monday) syntax error

With Jan's fix for [8afd93d3d3], which makes secondOfMin an int, the seconds field would be affected too: 12:00:4294967301 would give 12:00:05.

The year is the only field whose valid range (-min-year/-max-year) is the whole int range by default, so clamping alone cannot reject it.

Relative months and years are wrapped too (with -base 0 -gmt 1):

Input 9.0.5 and 9.1.1
clock scan "4294967297 months" 1970-02-01 (1 month)
clock scan "4294967297 years" 1971-01-01 (1 year)
clock scan "next 4294967297 Feb" 1970-02-01

Relative days, weeks, fortnights and seconds are 64-bit and give the right result. 8.6.18 gives wrong results for all relative units with this number (the year 429502 for each of them) and a syntax error for next 4294967297 Feb.

User Comments:
serhiy.storchaka added on 2026-11-14 09:29:03:

Proposed fix in branch clock-freescan-int-range. Numbers that do not fit into an int are rejected instead of truncated, and relative counts are calculated in 64 bits. The branch also fixes the following bugs.

Relative months and years before the common era go in the wrong direction (base 101-03-15 B.C.E., -gmt 1):

Input 8.6.18 and 9.0 Branch
clock scan "1 year" 102-03-15 B.C.E. 100-03-15 B.C.E.
clock scan "13 months" 102-04-15 B.C.E. 100-04-15 B.C.E.
clock scan "next Feb" 102-02-15 B.C.E. 100-02-15 B.C.E.
clock add $base 1 year 102-03-15 B.C.E. 100-03-15 B.C.E.

Signed integer overflows with -valid 0 (-base 0 -gmt 1). The overflow is undefined behaviour; the 9.0 values are what gcc gives:

Input 9.0 Branch
Dec 31 2147483647 (time 21474836:47) 31450692 77340862020
02/2147483647/2026 -185540816332800 185544356918400
14/02/2147483647 67675204942704000 67767976236297600

Overflows in clock add (-gmt 1):

Input 9.0 Branch
clock add 0 67553994410557440 hours 3386706919782612992 integer value too large to represent
clock add 0 2147483647 weekdays -111325552329600 259759622044800
clock add 0 2147483647 years -31536000 integer value too large to represent

clock add now raises "integer value too large to represent" for a result outside the range of clock seconds, as it already does for such a base. Before, clock add 0 67553994410557440 seconds 67553994410557440 seconds returned 135107988821114880, which clock format does not accept.


jan.nijtmans added on 2026-11-14 10:22:35:

+1, just go ahead!


serhiy.storchaka added on 2026-11-14 13:45:15:

For 8.6: proposed fix in branch clock-range-8-6. 8.6 has the B.C.E. bug in relative months and years, and wraps dates in clock add and the julian day helpers (clock add 0 2147483647 years gave 4455773-12-22 instead of the year 2147485617). Julian days beyond about ±5.8 million years now raise "integer value too large to represent". Numbers that do not fit into an int are already a syntax error in the 8.6 parser, and clock format of such seconds is unchanged.


serhiy.storchaka added on 2026-11-24 17:44:31:

Merged into core-8-6-branch by Jan in [a0288fe887]. With [21bc2b48f7] (main) and [df2b525fbd] (core-9-0-branch), the fix is in all three branches.