|
2021-12-18
| ||
| 12:20 | • Ticket [90612089d8] signed integer overflow in ExprRandFunc() status still C... artifact: adafff04a3 user: chrstphrchvz | |
|
2021-12-16
| ||
| 08:29 | • Closed ticket [90612089d8]. artifact: b1fd4b5708 user: jan.nijtmans | |
| 08:29 | • Ticket [90612089d8]: 3 changes artifact: 9e1e37de8a user: jan.nijtmans | |
| 08:20 | Fix [90612089d8]: signed integer overflow in ExprRandFunc() check-in: 2d6da16ccc user: jan.nijtmans tags: core-8-6-branch | |
|
2021-12-15
| ||
| 22:59 | • Ticket [90612089d8] signed integer overflow in ExprRandFunc() status still P... artifact: 302d9d264c user: chrstphrchvz | |
| 17:06 | • Pending ticket [90612089d8]. artifact: 132796b134 user: jan.nijtmans | |
| 17:03 | Fix [90612089d8]: signed integer overflow in ExprRandFunc() closed check-in: b0fd5cb70e user: jan.nijtmans tags: bug-90612089d8 | |
| 09:46 | • New ticket [90612089d8] signed integer overflow in ExprRandFunc(). artifact: 81c1a49514 user: chrstphrchvz | |
signed integer overflow in ExprRandFunc()
% puts [expr {rand()}]
tcl/generic/tclBasic.c:7784:69: runtime error: left shift of 404780544 by 12 places cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclBasic.c:7784:69 in
0.6984060833688853
The signed integer overflow can be avoided by instead using PTR2UINT():
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. */ - iPtr->randSeed = TclpGetClicks() + (PTR2INT(Tcl_GetCurrentThread())<<12); + iPtr->randSeed = TclpGetClicks() + (PTR2UINT(Tcl_GetCurrentThread())<<12); /* * Make sure 1 <= randSeed <= (2^31) - 2. See below.
Though I wonder if it is intentional to have Tcl_GetCurrentThread() contribute at most (32-12)-1=19 bits (assuming sizeof(int)==4) 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 pthread_t, and/or spacing between pthread_t 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 Tcl_GetCurrentThread(), can the behavior of this statement be changed in 8.6?
2d6da16cccFix [90612089d8]: signed integer overflow in ExprRandFunc() check-in2021-12-16 08:20:53b0fd5cb70eFix [90612089d8]: signed integer overflow in ExprRandFunc() check-in2021-12-15 17:03:03
Suggested fix [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!
Multiplying by 4093 does preserve more useful bits, but it is done after the result of Tcl_GetCurrentThread() is temporarily narrowed to an unsigned int. Would it be useful to introduce a PTR2ULONG() macro, so that the result of Tcl_GetCurrentThread isn’t narrowed when sizeof(Tcl_ThreadId) == sizeof(unsigned long) > sizeof(unsigned int)?
> 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:
iPtr->randSeed &= 0x7FFFFFFFL
so any bits higher than 30 will be lost anyway.
A few lines after that:
iPtr->randSeed &= 0x7FFFFFFFL
so any bits higher than 30 will be lost anyway.
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.