Skip to content
Tcl Source Code History Of Ticket 90612089d8
Login

History Of Ticket 90612089d8

Artifacts Associated With Ticket 90612089d8

  1. Ticket change [81c1a49514] (rid 196811) by chrstphrchvz on 2021-12-15 09:46:14:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      There is signed integer overflow happening in <tt>ExprRandFunc()</tt> (generic/tclBasic.c). UBSan (<tt>-fsanitize=shift-base</tt>) complaint:
      
      <pre>
      % puts [expr {rand()}]
      <span style="font-weight:bold">tcl/generic/tclBasic.c:7784:69: <span style="color:red">runtime error:</span> left shift of 404780544 by 12 places cannot be represented in type 'int'</span>
      SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclBasic.c:7784:69 in 
      0.6984060833688853
      </pre>
      
      The signed integer overflow can be avoided by instead using <tt>PTR2UINT()</tt>:
      <pre>
      diff --git generic/tclBasic.c generic/tclBasic.c
      index fb8524180..4a2fc04b3 100644
      --- generic/tclBasic.c
      +++ generic/tclBasic.c
      @@ -7781,7 +7781,7 @@ ExprRandFunc(
       	 * take into consideration the thread this interp is running in.
       	 */
      <span> </span>
      <span style="color:red">-	iPtr->randSeed = TclpGetClicks() + (PTR2INT(Tcl_GetCurrentThread())<<12);</span>
      <span style="color:green">+	iPtr->randSeed = TclpGetClicks() + (PTR2UINT(Tcl_GetCurrentThread())<<12);</span>
      <span> </span>
       	/*
       	 * Make sure 1 <= randSeed <= (2^31) - 2. See below.
      </pre>
      
      Though I wonder if it is intentional to have <tt>Tcl_GetCurrentThread()</tt> contribute at most (32-12)-1=19 bits (assuming <tt>sizeof(int)==4</tt>) to the seed (even fewer in practice due to lower order bits tending to be identical between threads, depending on the alignment of the struct underlying <tt>pthread_t</tt>, and/or spacing between <tt>pthread_t</tt> instances being a multiple of not very small powers of 2: I observe that for glibc NPTL on Linux i386, it is 2**12—i.e. 4kib page size—so as few as ((32-12)-12)-1=7 useful bits are incorporated in the seed). If this statement should be changed to incorporate more useful bits from <tt>Tcl_GetCurrentThread()</tt>, can the behavior of this statement be changed in 8.6?
      
    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: "16. Commands A-H"
    14. title initialized to: "signed integer overflow in ExprRandFunc()"
    15. type initialized to: "Patch"
  2. Ticket change [132796b134] (rid 196818) by jan.nijtmans on 2021-12-15 17:06:01:

    1. assignee changed to: "jan.nijtmans"
    2. icomment:
      Suggested fix [b0fd5cb70eed5212|here]. In stead of multiplying by 2**12 (= 4096) we could also multiply with the first prime-number below that (= 4093). Then all bits of randomness in thread-id are kept.
      
      Thanks for the suggestion!
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
    5. resolution changed to: "Fixed"
    6. status changed to: "Pending"
  3. Ticket change [302d9d264c] (rid 196820) by chrstphrchvz on 2021-12-15 22:59:36:

    1. icomment:
      Multiplying by 4093 does preserve more useful bits, but it is done after the result of <tt>Tcl_GetCurrentThread()</tt> is temporarily narrowed to an <tt>unsigned int</tt>. Would it be useful to introduce a <tt>PTR2ULONG()</tt> macro, so that the result of <tt>Tcl_GetCurrentThread</tt> isn’t narrowed when <tt>sizeof(Tcl_ThreadId) == sizeof(unsigned long) &gt; sizeof(unsigned int)</tt>?
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  4. Ticket change [9e1e37de8a] (rid 196828) by jan.nijtmans on 2021-12-16 08:29:43:

    1. icomment:
      > Would it be useful to introduce a PTR2ULONG() macro ...
      
      In Tcl 8.7, PTR2UINT() no longer narrows to unsigned int. Since it's just an internal macro, it's not worth to change that in 8.6: 8.7 still has some more time for possible bugs arising from that change to be found and fixed (although I don't know of any)
      
      Further on, the (unsigned long) cast doesn't do anything if the value is already an unsigned type. A few lines after that:
      <pre>
          iPtr->randSeed &= 0x7FFFFFFFL
      </pre>
      so any bits higher than 30 will be lost anyway.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  5. Ticket change [b1fd4b5708] (rid 196829) by jan.nijtmans on 2021-12-16 08:29:54:

    1. closedate changed to: "2459564.85409833"
    2. closer changed to: "jan.nijtmans"
    3. login: "jan.nijtmans"
    4. mimetype: "text/plain"
    5. status changed to: "Closed"
  6. Ticket change [adafff04a3] (rid 196863) by chrstphrchvz on 2021-12-18 12:20:51:

    1. closedate changed to: "2459567.01448206"
    2. closer changed to: "chrstphrchvz"
    3. icomment:
      <blockquote>A few lines after that:
      <pre>
          iPtr->randSeed &= 0x7FFFFFFFL
      </pre>
      so any bits higher than 30 will be lost anyway.</blockquote>
      
      It seems that for a moment I forgot how (x * y) mod z = ((x mod z) * (y mod z)) mod z (in this case z = 0x7FFFFFFF+1); thanks for the clarification.
      
    4. login: "chrstphrchvz"
    5. mimetype: "text/x-fossil-wiki"