Artifacts Associated With Ticket b0f84119c8
Ticket change [97f7294489] (rid 196851) by chrstphrchvz on 2021-12-16 17:22:45:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- 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.) - foundin initialized to: "8.6.12"
- is_private initialized to: "0"
- login: "chrstphrchvz"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "chrstphrchvz"
- subsystem initialized to: "16. Commands A-H"
- title initialized to: "TEBCresume(): undefined behavior for INST_LSHIFT"
- type initialized to: "Patch"
Ticket change [bea84d6133] (rid 196855) by jan.nijtmans on 2021-12-16 21:03:18:
- assignee changed to: "jan.nijtmans"
- icomment: "Suggested fix committed [c8fd4fa71448bcf6|here]"
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Pending"
Ticket change [977a353248] (rid 196867) by jan.nijtmans on 2021-12-18 14:56:19:
- closedate changed to: "2459567.12244462"
- closer changed to: "jan.nijtmans"
- icomment:
Fix now merged to 8.6 and up. Thanks very much for the report and for the fix!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- status changed to: "Closed"