Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Query about MAX_BITS

Query about MAX_BITS

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Wed 18 Jul 2007 05:51 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Wed 18 Jul 2007 06:10 AM (UTC)
Message
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...

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 18 Jul 2007 06:20 AM (UTC)
Message
Maybe make:


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


Not sure if that helps or hinders.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #3 on Wed 18 Jul 2007 06:21 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #4 on Wed 18 Jul 2007 06:35 AM (UTC)
Message
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).

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Gatewaysysop2   USA  (146 posts)  Bio
Date Reply #5 on Thu 19 Jul 2007 04:34 AM (UTC)
Message
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.


"The world of men is dreaming, it has gone mad in its sleep, and a snake is strangling it, but it can't wake up." -D.H. Lawrence
Top

Posted by Samson   USA  (683 posts)  Bio
Date Reply #6 on Tue 24 Jul 2007 06:10 PM (UTC)
Message
Yeah, I don't know how that one got by but it's been corrected now.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,547 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.