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