Browse Source

gcc 4.1 version of the code also works with other compilers, so make it

default and get rid of #ifdef hell
master
Felix von Leitner 16 years ago
parent
commit
c25fdbefb5
  1. 13
      rangecheck.h

13
rangecheck.h

@ -16,12 +16,9 @@
__static inline int range_ptrinbuf(const void* buf,size_t len,const void* ptr) {
register const char* c=(const char*)buf; /* no pointer arithmetic on void* */
return (c && /* is buf non-NULL? */
#if (__GNUC__ == 4) && (__GNUC_MINOR__ == 1)
((uintptr_t)c)+len>(uintptr_t)c && /* gcc 4.1 miscompiles this test */
#else
c+len>c && /* catch integer overflows and fail if buffer is 0 bytes long */
((uintptr_t)c)+len>(uintptr_t)c && /* gcc 4.1 miscompiles without (uintptr_t) */
/* catch integer overflows and fail if buffer is 0 bytes long */
/* because then ptr can't point _in_ the buffer */
#endif
(uintptr_t)((const char*)ptr-c)<len); /* this one is a little tricky.
"ptr-c" checks the offset of ptr in the buffer is inside the buffer size.
Now, ptr-c can underflow; say it is -1. When we cast it to uintptr_t, it becomes
@ -32,11 +29,7 @@ __static inline int range_ptrinbuf(const void* buf,size_t len,const void* ptr) {
* Check whether buf is NULL, and whether buf+len overflows.
* Does NOT check whether buf has a non-zero length! */
__static inline int range_validbuf(const void* buf,size_t len) {
#if (__GNUC__ == 4) && (__GNUC_MINOR__ == 1)
return (buf && (uintptr_t)buf+len>=(uintptr_t)buf); /* gcc 4.1 miscompiles this test */
#else
return (buf && (const char*)buf+len>=(const char*)buf);
#endif
return (buf && (uintptr_t)buf+len>=(uintptr_t)buf);
}
/* is buf2[0..len2-1] inside buf1[0..len-1]? */

Loading…
Cancel
Save