Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-18
16:44 • Ticket [69b4f7eb37] FilterInputBytes(): signed integer overflow, poor worst-case... artifact: dc326a73b4 user: serhiy.storchaka
16:43
Fix [69b4f7eb37]: [gets] of a line longer than 2 GB panicked in Tcl_SetObjLength, returned a negativ... check-in: 34cbbe86a5 user: serhiy.storchaka tags: gets-long-lines
2023-05-05
13:33 • Ticket [c9663296fd] GrowStringBuffer and GrowUnicodeBuffer do not check for over... artifact: 2f0f091002 user: chrstphrchvz
13:07 • Ticket [c9663296fd]: 3 changes artifact: 6294d03579 user: chrstphrchvz
2022-04-13
18:34 • Ticket [69b4f7eb37] FilterInputBytes(): signed integer overflow, poor worst-case... artifact: 6636cf6393 user: chrstphrchvz
2022-03-06
20:28 • New ticket [69b4f7eb37]. artifact: 5e4cad2b3e user: chrstphrchvz

FilterInputBytes(): signed integer overflow, poor worst-case performance lengthening objPtr

Open69b4f7eb379e7b806c0c144a36059ca55f569634Created 5 years agoUpdated 45 days ago
Descriptionwiki2022-03-06 20:28:47
Running this example leads to signed integer overflow in FilterInputBytes() (as reported by UBSan -fsanitize=signed-integer-overflow):
$ dd bs=4096 count=250000 if=/dev/zero | tclsh8.6
tcl/generic/tclIO.c:5330:22: runtime error: signed integer overflow: 1912594432 + 1912594432 cannot be represented in type 'int'

But something else I notice is that once FilterInputBytes() stops growing objPtr exponentially, it falls back to growing linearly by a very small amount (58 bytes in this example), with each growth requiring realloc()—which even if made much faster by using Linux’s mremap() will still cause throughput to drop dramatically. While the overflow can be avoiding by capping length to INT_MAX, low-memory systems—or systems with plenty of memory if FilterInputBytes() is rewritten to use size_t for Tcl 9—still have the slow linear growth performance issue, so it may still be desirable to improve the objPtr growth algorithm to minimize the number of realloc() calls. One approach I think of is something that approximates logistic growth: if length = offset + offset fails, try again with length = offset + (offset >> 1); otherwise try length = offset + (offset >> 2); and so on until whatever reasonable minimum.

Referenced by3
  • 34cbbe86a5Fix [69b4f7eb37]: [gets] of a line longer than 2 GB panicked in Tcl_SetObjLength, returned a negative length with -translation binary, or crashed when a \r ended a buffer. Use Tcl_Size for the offsets and counts in Tcl_GetsObj, TclGetsObjBinary and FilterInputBytes, and do not overflow when doubling the buffer. check-in2026-10-18 16:43:07
  • 2f0f091002Ticket [c9663296fd] GrowStringBuffer and GrowUnicodeBuffer do not check for overflows status still Open with 3 other changes ticket2023-05-05 13:33:10
  • 6294d03579Ticket [c9663296fd] GrowStringBuffer and GrowUnicodeBuffer do not check for overflows status still Open with 3 other changes ticket2023-05-05 13:07:04
chrstphrchvzwiki2022-04-13 18:34:58

Apparently there are other places in Tcl where objects are first grown exponentially, and then linearly once doubling is not possible. I would think those are affected by performance issues similar to the one seen in FilterInputBytes(), but maybe that is avoided by their use of TCL_MIN_GROWTH.

Without a more rigorous analysis, the alternative growth algorithm I suggest (i.e. to approximate logistic growth) may increase the likelihood that a program terminates (due to wasted memory starving other needed allocations) rather than eventually finishes.

serhiy.storchakawiki2026-10-18 16:44:59

Still present in 9.1 (9.0 has the same code): Tcl_GetsObj(), TclGetsObjBinary() and FilterInputBytes() keep offsets and counts in int, so [gets] of a line over 2 GB panics in Tcl_SetObjLength ("length requested is negative"), returns a negative length with -translation binary, or crashes when a \r ends a buffer.

Proposed fix in branch gets-long-lines: Tcl_Size there, and the doubling of the buffer no longer overflows. Tests gets-bigdata-1 to 3 (constraint bigdata, about 6 GB of memory). The growth strategy when doubling fails is unchanged.

Log in (anonymous is fine) to add a comment.