[comp.sys.mac.programmer] Think C enum bug in Events.h

rnpantos@puff.uwaterloo.ca (Roger Pantos) (06/15/91)

I recently switched to using the System 7 header and library files (from 
the 7b4 CD-ROM) with Think C v4.0.5. It seems that a few constants in 
Events.h which were formerly #define'd are now enum's instead. This, I 
believe, has led to a problem.

Consider adbAddrMask: it contains the definition
    enum {
         adbAddrMask = 0x00FF0000
    };

Now consider the following code:

#include <Events.h>
void    main( void)
{
long message, test;

    message = 0x00330000;
    test = message & adbAddrMask;
}

If you step through this with the debugger, at the end of the program 
you get message == 0x00330000, but test == 0x00000000.

Is this wrong? Well, yes and no. It's not really the result I'd like to 
get, but according K&R, 2nd Ed., enum's are to be treated as type "int".

adbAddrMask (0x00FF0000) becomes 0 when cast to a (16-bit) int in Think 
C. This problem does not show up under MPW because ints there are 32 
bits.

There is an even better example in keyCodeMask, 0x0000FF00. Because an 
enum is a _signed_ int, keyCodeMask when used in an operation with a 
long becomes sign-extended to be 0xFFFFFF00.

Thus in the above program, setting message to be, say, 0x00023300 means 
that
    message & keyCodeMask == 0x00023300
is not masked at all.

There are 4 constants in Events.h which should be #define'd as they were 
previously: charCodeMask, keyCodeMask, adbAddrMask and osEvtMessageMask 
(0xFF000000L). This will fix the problem.

Doing a quick grep through the include files, I only found one
other constant with the same problem - MaxSmallFract in Picker.h 
(formerly in ColorToolbox.h). I may have missed a few, however.

Other than that, my projects converted to the new stuff without any 
problems. Symantec, Pete Helme and DTS did a great job, considering the 
time constraints they were under. Thanks, guys!


cheers,

Roger