Skip to content
Tcl Source Code Artifact [5e4cad2b3e]
Login

Artifact [5e4cad2b3e]

Artifact 5e4cad2b3e2558cea9d678fc93b0bc4060a82a860b1a4819d8ee3717f42ebd48:

Ticket change [5e4cad2b3e] - New ticket [69b4f7eb379e7b80|69b4f7eb37] <i>FilterInputBytes(): signed integer overflow, poor worst-case performance lengthening objPtr</i>. by chrstphrchvz on 2022-03-06 20:28:47.
D 2022-03-06T20:28:47.140
J assignee nobody
J closer nobody
J cmimetype text/x-fossil-wiki
J comment Running\sthis\sexample\sleads\sto\ssigned\sinteger\soverflow\sin\s<tt>FilterInputBytes()</tt>\s(as\sreported\sby\sUBSan\s<tt>-fsanitize=signed-integer-overflow</tt>):\r\n\r\n<pre>\r\n$\sdd\sbs=4096\scount=250000\sif=/dev/zero\s|\stclsh8.6\r\n<span\sstyle="font-weight:bold">tcl/generic/tclIO.c:5330:22:<span\sstyle="color:firebrick">\sruntime\serror:\s</span>signed\sinteger\soverflow:\s1912594432\s+\s1912594432\scannot\sbe\srepresented\sin\stype\s'int'</span>\r\n</pre>\r\n\r\n\r\nBut\ssomething\selse\sI\snotice\sis\sthat\sonce\s<tt>FilterInputBytes()</tt>\sstops\sgrowing\s<tt>objPtr</tt>\sexponentially,\sit\sfalls\sback\sto\sgrowing\slinearly\sby\sa\svery\ssmall\samount\s(58\sbytes\sin\sthis\sexample),\swith\seach\sgrowth\srequiring\s<tt>realloc()</tt>—which\seven\sif\s[http://blog.httrack.com/blog/2014/04/05/a-story-of-realloc-and-laziness/|made\smuch\sfaster\sby\susing\sLinux’s\s<tt>mremap()</tt>]\swill\sstill\scause\sthroughput\sto\sdrop\sdramatically.\sWhile\sthe\soverflow\scan\sbe\savoiding\sby\scapping\s<tt>length</tt>\sto\s<tt>INT_MAX</tt>,\slow-memory\ssystems—or\ssystems\swith\splenty\sof\smemory\sif\s<tt>FilterInputBytes()</tt>\sis\srewritten\sto\suse\s<tt>size_t</tt>\sfor\sTcl\s9—still\shave\sthe\sslow\slinear\sgrowth\sperformance\sissue,\sso\sit\smay\sstill\sbe\sdesirable\sto\simprove\sthe\s<tt>objPtr</tt>\sgrowth\salgorithm\sto\sminimize\sthe\snumber\sof\s<tt>realloc()</tt>\scalls.\sOne\sapproach\sI\sthink\sof\sis\ssomething\sthat\sapproximates\slogistic\sgrowth:\sif\s<tt>length\s=\soffset\s+\soffset</tt>\sfails,\stry\sagain\swith\s<tt>length\s=\soffset\s+\s(offset\s>>\s1)</tt>;\sotherwise\stry\s<tt>length\s=\soffset\s+\s(offset\s>>\s2)</tt>;\sand\sso\son\suntil\swhatever\sreasonable\sminimum.
J foundin core-8-6-branch
J is_private 0
J login chrstphrchvz
J priority 5\sMedium
J resolution None
J severity Minor
J status Open
J submitter chrstphrchvz
J subsystem 24.\sChannel\sCommands
J title FilterInputBytes():\ssigned\sinteger\soverflow,\spoor\sworst-case\sperformance\slengthening\sobjPtr
J type Bug
K 69b4f7eb379e7b806c0c144a36059ca55f569634
U chrstphrchvz
Z e45bc9c39668162f99021f0ca2743364