Skip to content
Tcl Source Code History Of Ticket b0f84119c8
Login

History Of Ticket b0f84119c8

Artifacts Associated With Ticket b0f84119c8

  1. Ticket change [97f7294489] (rid 196851) by chrstphrchvz on 2021-12-16 17:22:45:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      Yet another case where I do not observe Tcl behaving unexpectedly despite complaints from [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan].
      
      Example to trigger complaint from <tt>-fsanitize=shift-base</tt> (left shift by <tt>CHAR_BIT*sizeof(long)-1</tt> causes signed integer overflow) and <tt>-fsanitize=signed-integer-overflow</tt> (2’s complement negation of <tt>LONG_MIN</tt> causes signed integer overflow) for <tt>sizeof(long)==4</tt>:
      
      <pre>
      % expr {1<<0}
      <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:9: <span style="color:red">runtime error:</span> left shift of 1 by 31 places cannot be represented in type 'long int'</span>
      <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:5: <span style="color:red">runtime error:</span> negation of -2147483648 cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself</span>
      1
      </pre>
      
      Likewise for <tt>sizeof(long)==8</tt>:
      
      <pre>
      % expr {1<<0}
      <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:9: <span style="color:red">runtime error:</span> left shift of 1 by 63 places cannot be represented in type 'long'</span>
      <span style="font-weight:bold">tcl/generic/tclExecute.c:6384:5: <span style="color:red">runtime error:</span> negation of -9223372036854775808 cannot be represented in type 'long'; cast to an unsigned type to negate this value to itself</span>
      1
      </pre>
      
      Another example to trigger complaint from <tt>-fsanitize=shift-base</tt> (left-shifting negative signed integer is undefined behavior):
      
      <pre>
      % expr {-1<<1}
      <span style="font-weight:bold">tcl/generic/tclExecute.c:6385:17: <span style="color:red">runtime error:</span> left shift of negative value -1</span>
      -2
      </pre>
      
      Suggested fix for both of these is to use <tt>unsigned long</tt> literals and casts in a few places (this should avoid undefined behavior without changing actual behavior):
      
      <pre>
      diff --git generic/tclExecute.c generic/tclExecute.c
      index 8963472e5..19b0565 100644
      --- generic/tclExecute.c
      +++ generic/tclExecute.c
      @@ -6381,8 +6381,8 @@ TEBCresume(
      <span> </span>
       		    if ((size_t) shift < CHAR_BIT*sizeof(long) && (l1 != 0)
       			    && !((l1>0 ? l1 : ~l1) &
      <span style="color:red">-				-(1L<<(CHAR_BIT*sizeof(long) - 1 - shift)))) {
      -			lResult = l1 << shift;</span>
      <span style="color:green">+				-(1UL<<(CHAR_BIT*sizeof(long) - 1 - shift)))) {
      +			lResult = (unsigned long) l1 << shift;</span>
       			goto longResultOfArithmetic;
       		    }
       		}
      </pre>
      
      (I might also point out that <tt>l1 != 0</tt> in this condition is always true because of the <tt>l1 == 0</tt> check on line 6353, though I’m not sure whether removing it makes more sense than keeping it.)
      
    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: "TEBCresume(): undefined behavior for INST_LSHIFT"
    15. type initialized to: "Patch"
  2. Ticket change [bea84d6133] (rid 196855) by jan.nijtmans on 2021-12-16 21:03:18:

    1. assignee changed to: "jan.nijtmans"
    2. icomment: "Suggested fix committed [c8fd4fa71448bcf6|here]"
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
    5. resolution changed to: "Fixed"
    6. status changed to: "Pending"
  3. Ticket change [977a353248] (rid 196867) by jan.nijtmans on 2021-12-18 14:56:19:

    1. closedate changed to: "2459567.12244462"
    2. closer changed to: "jan.nijtmans"
    3. icomment:
      Fix now merged to 8.6 and up. Thanks very much for the report and for the fix!
      
    4. login: "jan.nijtmans"
    5. mimetype: "text/x-fossil-wiki"
    6. status changed to: "Closed"