Missing parentheses in #define LABEL 1<<1
Closed / Fixed957dbe231ccd7f311b583ebe47da716db43c0641Created 11 years agoUpdated 11 years ago
Descriptionplain2016-06-03 09:32:58
In tcl 8.6 there seems to be a bug of missing parentheses in the thread package.
At the top of the file tcl8.6.1/pkgs/thread2.7.0/generic/threadCmd.c
there is a definition
#define THREAD_SEND_WAIT 1<<1
#define THREAD_SEND_HEAD 1<<2
It seems to me that this is intended to define which specific one bit to use in
bitwise operations. These are used for example in ThreadSendObjCmd() in a
line like:
if (OPT_CMP(arg, "-async")) {
flags &= ~THREAD_SEND_WAIT;
}
The problem is that the operator precedence of the bitwise not ("~") is
higher than the the precedence of the shift left ("<<"). While the
intended result seems to be to compute
flags &= ~(1<<1);
what is actually computed is
flags &= (~1)<<1;
As long as this operation is only done on the least significant used
bit out of all bits in the bit mask, this "bug" probably has no practical
effect, except invoking undefined behavior of shifting into and past the sign
bit of a signed value. However, if it is indeed a bug, it has the
potential to become a hard-to-notice more serious problem with later
code changes.
Please verify whether the intention is indeed to clear out one single bit,
or that it clears also all bits that are less significant at the same time.
In case the intention is to use 1<<1 and 1<<2 to specify a single bit in
a bit-mask, my recommendation would be to include parentheses:
#define THREAD_SEND_WAIT (1<<1)
#define THREAD_SEND_HEAD (1<<2)
The same problem and suggested fix applies to these three lines in the
newer version in tcl8.6.5/pkgs/thread2.7.3/generic/threadCmd.c:
#define THREAD_SEND_WAIT 1<<1
#define THREAD_SEND_HEAD 1<<2
#define THREAD_SEND_CLBK 1<<3
Best regards,
Frans van der Have,
developer at Scientific Volume Imaging.
gahrwiki2016-06-03 13:21:57
Fixed in http://core.tcl.tk/thread/info/b52389dd2464bdb1. Thanks!
Log in (anonymous is fine) to add a comment.