Remember the question about testing for passing a pointer instead
of an int to recvfrom for the len arg, which could evaluate to
a negative length at runtime?
The NOPOINTERS macro to use a bitwise & and gcc -Wall compile-time
warning about applying bitwise operators to pointers won't
work in the form that I originally posted (shifting 1 to the left
does not 1-fill behind it, you end up with a mask against a value with
only the high bit set), as anyone who tried it has discovered by now.
Given enough idle time, I could probably work out a macro to make
a bitmask with all 1s to match the width of the integer type of
the arg, but here is the simple way, if anyone actually wants to
use it in their code:
#ifndef NDEBUG /* define NDEBUG to disable test in production code */
/* If you don't have 8-bit bytes, work out your local bitmasks.
Use whatever works for the HAVE_128 define if you have 128-bit long long
or some kind of int128 type. sizeof() normally does nothing at
preprocessor time, so "#if (sizeof(long long) > 8)" is probably an
error. */
#ifdef HAVE_128
#define MASK128 0xffffffffffffffffffffffffffffffff
#endif
#define MASK64 0xffffffffffffffff
#define MASK32 0xffffffff
#define MASK16 0xffff
#define MASK8 0xff
#define NOPOINTERS(a, type) \
(a & ( \
#ifdef MASK128
sizeof(type) == 16 ? MASK128 : \
#endif
sizeof(type) == 8 ? MASK64 : sizeof(type) == 4 ? MASK32 : \
sizeof(type) == 2 ? MASK16 : MASK8))
#else
#define NOPOINTERS(a, type) a
#endif
/* examples:
#define RECVFROM(s, buf, len, flags, from, fromlen) \
recvfrom(NOPOINTERS(s, int), buf, NOPOINTERS(len, size_t), \
NOPOINTERS(flags, int), from, fromlen)
void * sample(const char * buf, size_t len);
if (sample(buf, NOPOINTERS(len, size_t)) != NULL) {
...
*/
I still think lclint comments are better technology on average than this
sort of macrology, but that shouldn't be taken as a comment on the quality
of the betterC library, mentioned in the same thread. I have not used
betterC, which provides a library of sanity tests that you can use and
link into your user-space applications. Lots of applications could benefit
from the work of the betterC author.
Regards,
Clayton Weaver
<mailto:[EMAIL PROTECTED]>
(Seattle)
"Everybody's ignorant, just in different subjects." Will Rogers
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]