Artifacts Associated With Ticket 69b4f7eb37
Ticket change [5e4cad2b3e] (rid 198796) by chrstphrchvz on 2022-03-06 20:28:47:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- comment initialized to:
Running this example leads to signed integer overflow in <tt>FilterInputBytes()</tt> (as reported by UBSan <tt>-fsanitize=signed-integer-overflow</tt>): <pre> $ dd bs=4096 count=250000 if=/dev/zero | tclsh8.6 <span style="font-weight:bold">tcl/generic/tclIO.c:5330:22:<span style="color:firebrick"> runtime error: </span>signed integer overflow: 1912594432 + 1912594432 cannot be represented in type 'int'</span> </pre> But something else I notice is that once <tt>FilterInputBytes()</tt> stops growing <tt>objPtr</tt> exponentially, it falls back to growing linearly by a very small amount (58 bytes in this example), with each growth requiring <tt>realloc()</tt>—which even if [http://blog.httrack.com/blog/2014/04/05/a-story-of-realloc-and-laziness/|made much faster by using Linux’s <tt>mremap()</tt>] will still cause throughput to drop dramatically. While the overflow can be avoiding by capping <tt>length</tt> to <tt>INT_MAX</tt>, low-memory systems—or systems with plenty of memory if <tt>FilterInputBytes()</tt> is rewritten to use <tt>size_t</tt> for Tcl 9—still have the slow linear growth performance issue, so it may still be desirable to improve the <tt>objPtr</tt> growth algorithm to minimize the number of <tt>realloc()</tt> calls. One approach I think of is something that approximates logistic growth: if <tt>length = offset + offset</tt> fails, try again with <tt>length = offset + (offset >> 1)</tt>; otherwise try <tt>length = offset + (offset >> 2)</tt>; and so on until whatever reasonable minimum.
- foundin initialized to: "core-8-6-branch"
- is_private initialized to: "0"
- login: "chrstphrchvz"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "chrstphrchvz"
- subsystem initialized to: "24. Channel Commands"
- title initialized to:
FilterInputBytes(): signed integer overflow, poor worst-case performance lengthening objPtr
- type initialized to: "Bug"
Ticket change [6636cf6393] (rid 200107) by chrstphrchvz on 2022-04-13 18:34:58:
- icomment:
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 <tt>FilterInputBytes()</tt>, but maybe that is avoided by their use of <tt>TCL_MIN_GROWTH</tt>. 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.
- login: "chrstphrchvz"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [dc326a73b4] (rid 259054) by serhiy.storchaka on 2026-10-18 16:44:59:
- foundin changed to: "8.6.18, 9.1.0"
- icomment:
Still present in 9.1 (9.0 has the same code): <tt>Tcl_GetsObj()</tt>, <tt>TclGetsObjBinary()</tt> and <tt>FilterInputBytes()</tt> keep offsets and counts in <tt>int</tt>, so <nowiki>[gets]</nowiki> of a line over 2 GB panics in <tt>Tcl_SetObjLength</tt> ("length requested is negative"), returns a negative length with <tt>-translation binary</tt>, or crashes when a <tt>\r</tt> ends a buffer. Proposed fix in branch [/timeline?r=gets-long-lines|gets-long-lines]: <tt>Tcl_Size</tt> there, and the doubling of the buffer no longer overflows. Tests gets-bigdata-1 to 3 (constraint <tt>bigdata</tt>, about 6 GB of memory). The growth strategy when doubling fails is unchanged. - login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"