Skip to content
Tcl Source Code History Of Ticket 31e302fcf7
Login

History Of Ticket 31e302fcf7

Artifacts Associated With Ticket 31e302fcf7

  1. Ticket change [eedfdc67cb] (rid 196821) by chrstphrchvz on 2021-12-16 00:45:59:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized 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>
      
    5. foundin initialized to: "8.6.12"
    6. is_private initialized to: "0"
    7. login: "chrstphrchvz"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "chrstphrchvz"
    13. subsystem initialized to: "69. Other"
    14. title initialized to: "signed integer overflow in generic/tclHash.c"
    15. type initialized to: "Patch"
  2. Ticket change [70e0018f09] (rid 196833) by jan.nijtmans on 2021-12-16 08:37:48:

    1. assignee changed to: "jan.nijtmans"
    2. closedate changed to: "2459564.85959052"
    3. closer changed to: "jan.nijtmans"
    4. icomment:
      Fixed [96d30a7f6b67115d|here]. Thanks for the report and patch!
      
    5. login: "jan.nijtmans"
    6. mimetype: "text/x-fossil-wiki"
    7. resolution changed to: "Fixed"
    8. status changed to: "Closed"