Query about MAX_BITS

Posted by Nick Gammon on Wed 18 Jul 2007 05:51 AM — 7 posts, 27,777 views.

Australia Forum Administrator #0
I am working my way through the SMAUG FUSS code to adapt it to the Lua interface, and I have struck one strange thing. Perhaps someone can clear it up for me?

Look at the define for MAX_BITS:


/*
 * Defines for extended bitvectors
 */
#ifndef INTBITS
#define INTBITS	32
#endif
#define XBM		31 /* extended bitmask   ( INTBITS - 1 )  */
#define RSV		5  /* right-shift value  ( sqrt(XBM+1) )  */
#define XBI		4  /* integers in an extended bitvector   */
#define MAX_BITS	XBI * INTBITS


OK, we can see from the above that MAX_BITS is 128 (4 * 32).

However, the first bit you can set is bit zero, right? So the highest bit you can set is MAX_BITS - 1 (127) not MAX_BITS. Agreed?

There seem to be heaps of places in the code where you are allowed to set up to, and including MAX_BITS. For example, in act_wiz.c:


         if( value < 0 || value > MAX_BITS )
            ch_printf( ch, "Unknown flag: %s\r\n", arg2 );
         else
            xTOGGLE_BIT( Class->affected, value );


Isn't that wrong? Shouldn't it be:


         if( value < 0 || value >= MAX_BITS )
            ch_printf( ch, "Unknown flag: %s\r\n", arg2 );
         else
            xTOGGLE_BIT( Class->affected, value );


Ditto for about 30 other places.
USA #1
It appears to me that you have found a bug. :-)

Looks like somebody either:
1) didn't understand bit math
2) was off by one by mistake and then copy-pasted
3) some combination

Regardless it's clearly incorrect to run xTOGGLE_BIT with a value of 128.

mud.h:#define xTOGGLE_BIT(var, bit)     ((var).bits[(bit) >> RSV] ^= 1 << ((bit) & XBM))


128 right-shift 5 is 4. The bits array is of length XBI i.e. 4.

Therefore dereferencing slot 4 is going out of bounds. Oops.

I'll report this on the FUSS forums...
Australia Forum Administrator #2
Maybe make:


#define MAX_BITS	((XBI * INTBITS) - 1)


Not sure if that helps or hinders.
Australia Forum Administrator #3
Some places seem to test for >= MAX_BITS. Probably someone need to work out whether MAX_BITS is the highest bit, or the highest bit + 1, and then check each place it is used and make it conform.
USA #4
It seems that a lot of the code uses "MAX" as a length, not as a maximum allowed element. For instance, MAX_TRADE, MAX_FIX, MAX_IFS, MAX_AFFECTED_BY, MAX_ATTACK_TYPE, MAX_DEFENSE_TYPE, and so on.

So for consistency's sake, it seems that MAX_BITS should remain 128. But I would suggest renaming it to NUM_BITS, because that removes the ambiguity of "max".

Regardless, the value of max bit (128) is not the highest allowed bit, because it is out of the bit array bounds (it's allocated with length 4, and using bit 128 brings you to index 4).
USA #5
Irony of ironies (note the date stamp on the post):

http://www.fussproject.org/index.php?a=topic&t=591

Hope that helps someone to track all these down.

USA #6
Yeah, I don't know how that one got by but it's been corrected now.