Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2003:
[Freeciv-Dev] Re: (PR#4788) range checks in BV macros
Home

[Freeciv-Dev] Re: (PR#4788) range checks in BV macros

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#4788) range checks in BV macros
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 8 Aug 2003 09:50:34 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Gregory Berkolaiko wrote:
> On Thu, 7 Aug 2003, Per I. Mathisen wrote:
> 
> 
>>On Thu, 7 Aug 2003, Jason Short wrote:
>>
>>>This patch introduces assertions on the range of the input in the BV_***
>>>macros. Probably a good thing IMO.
>>
>>I think this should be #ifdef DEBUG.
> 
> 
> I agree.

Patch attached.

jason

? rc
Index: common/shared.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/shared.h,v
retrieving revision 1.114
diff -u -r1.114 shared.h
--- common/shared.h     2003/07/19 04:05:59     1.114
+++ common/shared.h     2003/08/08 16:49:43
@@ -120,12 +120,21 @@
 #define _BV_BYTES(bits)                ((((bits) - 1) / 8) + 1)
 #define _BV_BYTE_INDEX(bits)   ((bits) / 8)
 #define _BV_BITMASK(bit)       (1u << ((bit) & 0x7))
+#ifdef DEBUG
+#  define _BV_ASSERT(bv, bit) assert((bit) >= 0 \
+                                     && (bit) < sizeof((bv).vec) * 8)
+#else
+#  define _BV_ASSERT(bv, bit) (void)0
+#endif
 #define BV_ISSET(bv, bit) \
-  (((bv).vec[_BV_BYTE_INDEX(bit)] & _BV_BITMASK(bit)) != 0)
+  (_BV_ASSERT(bv, bit), \
+   ((bv).vec[_BV_BYTE_INDEX(bit)] & _BV_BITMASK(bit)) != 0)
 #define BV_SET(bv, bit) \
-  do { (bv).vec[_BV_BYTE_INDEX(bit)] |= _BV_BITMASK(bit); } while(FALSE)
+  do { _BV_ASSERT(bv, bit); \
+       (bv).vec[_BV_BYTE_INDEX(bit)] |= _BV_BITMASK(bit); } while(FALSE)
 #define BV_CLR(bv, bit) \
-  do { (bv).vec[_BV_BYTE_INDEX(bit)] &= ~_BV_BITMASK(bit); } while(FALSE)
+  do { _BV_ASSERT(bv, bit); \
+       (bv).vec[_BV_BYTE_INDEX(bit)] &= ~_BV_BITMASK(bit); } while(FALSE)
 #define BV_CLR_ALL(bv) \
   do { memset((bv).vec, 0, sizeof((bv).vec)); } while(FALSE)
 bool bv_check_mask(unsigned char *vec1, unsigned char *vec2, size_t size1,

[Prev in Thread] Current Thread [Next in Thread]