Overview
| Artifact ID: | eedfdc67cb03ea51a8daf6d09522d0382b03b72c4410ce0885794cde1a958578 |
|---|---|
| Ticket: | 31e302fcf74ff38e31a1ddb5b0a0e2c1506766b3
signed integer overflow in generic/tclHash.c |
| User & Date: | chrstphrchvz on 2021-12-16 00:45:59 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-fossil-wiki"
- comment changed to:
(Not sure which category to pick for hash tables.) The <tt>RANDOM_INDEX()</tt> macro in generic/tclHash.c multiplies an input, which is not necessarily unsigned, by <tt>1103515245L</tt>. On platforms where <tt>sizeof(long) == 4</tt>, this often leads to signed integer overflow for signed inputs. UBSan (<tt>-fsanitize=signed-integer-overflow</tt>) complains on tclsh startup (somewhere during TclOO initialization): <pre> <span style="font-weight:bold">tcl/generic/tclHash.c:442:10: <span style="color:red">runtime error:</span> signed integer overflow: -1322139600 * 1103515245 cannot be represented in type 'long int'</span> <span style="font-weight:bold">tcl/generic/tclHash.c:1075:11: <span style="color:red">runtime error:</span> signed integer overflow: -1341125938 * 1103515245 cannot be represented in type 'long int'</span> % </pre> For systems with <tt>sizeof(long) == 8</tt> but <tt>sizeof(int) == 4</tt>, a similar error can be reproduced by changing <tt>1103515245L</tt> to <tt>1103515245</tt>: <pre> <span style="font-weight:bold">tcl/generic/tclHash.c:442:10: <span style="color:red">runtime error:</span> signed integer overflow: 113680 * 1103515245 cannot be represented in type 'int'</span> <span style="font-weight:bold">tcl/generic/tclHash.c:1075:11: <span style="color:red">runtime error:</span> signed integer overflow: 14286 * 1103515245 cannot be represented in type 'int'</span> % </pre> Signed integer overflow can be avoided by instead using <tt>1103515245UL</tt> (causing signed inputs to be sign-extended and then cast to <tt>unsigned long</tt>): <pre>diff --git generic/tclHash.c generic/tclHash.c index bcf6eee00..5de816829 100644 --- generic/tclHash.c +++ generic/tclHash.c @@ -35,7 +35,7 @@ */ <span> </span> #define RANDOM_INDEX(tablePtr, i) \ <span style="color:red">- ((((i)*1103515245L) >> (tablePtr)->downShift) & (tablePtr)->mask)</span> <span style="color:green">+ ((((i)*1103515245UL) >> (tablePtr)->downShift) & (tablePtr)->mask)</span> <span> </span> /* * Prototypes for the array hash key methods. </pre>
- foundin changed to: "8.6.12"
- is_private changed to: "0"
- login: "chrstphrchvz"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- severity changed to: "Minor"
- status changed to: "Open"
- submitter changed to: "chrstphrchvz"
- subsystem changed to: "69. Other"
- title changed to: "signed integer overflow in generic/tclHash.c"
- type changed to: "Patch"