|
2022-02-18
| ||
| 03:43 | • New ticket [1c7f179710] undefined behavior for INST_LSHIFT in ExecuteExtendedBin... artifact: 4193cd7082 user: chrstphrchvz | |
|
2021-12-18
| ||
| 14:56 | • Closed ticket [b0f84119c8]: TEBCresume(): undefined behavior for INST_LSHIFT... artifact: 977a353248 user: jan.nijtmans | |
| 14:46 | Fix [b0f84119c8]: TEBCresume(): undefined behavior for INST_LSHIFT check-in: 66f17f6fe9 user: jan.nijtmans tags: core-8-6-branch | |
| 12:11 | • Ticket [d1434179b5] avoid signed integer overflow in AppendUtfToUtfRep() sta... artifact: 66948b3d71 user: chrstphrchvz | |
|
2021-12-16
| ||
| 21:03 | • Pending ticket [b0f84119c8]: TEBCresume(): undefined behavior for INST_LSHIFT</i... artifact: bea84d6133 user: jan.nijtmans | |
| 21:02 | Suggested fix for [b0f84119c8]: TEBCresume(): undefined behavior for INST_LSHIFT closed check-in: c8fd4fa714 user: jan.nijtmans tags: bug-b0f84119c8 | |
| 17:22 | • New ticket [b0f84119c8] TEBCresume(): undefined behavior for INST_LSHIFT. artifact: 97f7294489 user: chrstphrchvz | |
TEBCresume(): undefined behavior for INST_LSHIFT
Example to trigger complaint from -fsanitize=shift-base (left shift by CHAR_BIT*sizeof(long)-1 causes signed integer overflow) and -fsanitize=signed-integer-overflow (2’s complement negation of LONG_MIN causes signed integer overflow) for sizeof(long)==4:
% expr {1<<0}
tcl/generic/tclExecute.c:6384:9: runtime error: left shift of 1 by 31 places cannot be represented in type 'long int'
tcl/generic/tclExecute.c:6384:5: runtime error: negation of -2147483648 cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself
1
Likewise for sizeof(long)==8:
% expr {1<<0}
tcl/generic/tclExecute.c:6384:9: runtime error: left shift of 1 by 63 places cannot be represented in type 'long'
tcl/generic/tclExecute.c:6384:5: runtime error: negation of -9223372036854775808 cannot be represented in type 'long'; cast to an unsigned type to negate this value to itself
1
Another example to trigger complaint from -fsanitize=shift-base (left-shifting negative signed integer is undefined behavior):
% expr {-1<<1}
tcl/generic/tclExecute.c:6385:17: runtime error: left shift of negative value -1
-2
Suggested fix for both of these is to use unsigned long literals and casts in a few places (this should avoid undefined behavior without changing actual behavior):
diff --git generic/tclExecute.c generic/tclExecute.c index 8963472e5..19b0565 100644 --- generic/tclExecute.c +++ generic/tclExecute.c @@ -6381,8 +6381,8 @@ TEBCresume( if ((size_t) shift < CHAR_BIT*sizeof(long) && (l1 != 0) && !((l1>0 ? l1 : ~l1) & - -(1L<<(CHAR_BIT*sizeof(long) - 1 - shift)))) { - lResult = l1 << shift; + -(1UL<<(CHAR_BIT*sizeof(long) - 1 - shift)))) { + lResult = (unsigned long) l1 << shift; goto longResultOfArithmetic; } }
(I might also point out that l1 != 0 in this condition is always true because of the l1 == 0 check on line 6353, though I’m not sure whether removing it makes more sense than keeping it.)
4193cd7082New ticket [1c7f179710] undefined behavior for INST_LSHIFT in ExecuteExtendedBinaryMathOp(). ticket2022-02-18 03:43:3066f17f6fe9Fix [b0f84119c8]: TEBCresume(): undefined behavior for INST_LSHIFT check-in2021-12-18 14:46:4566948b3d71Ticket [d1434179b5] avoid signed integer overflow in AppendUtfToUtfRep() status still Closed with 5 other changes ticket2021-12-18 12:11:46c8fd4fa714Suggested fix for [b0f84119c8]: TEBCresume(): undefined behavior for INST_LSHIFT check-in2021-12-16 21:02:15
Suggested fix committed [here]
Fix now merged to 8.6 and up. Thanks very much for the report and for the fix!