Portable check for unportable macro usage - Mailing list pgsql-hackers

From Tom Lane
Subject Portable check for unportable macro usage
Date
Msg-id 11911.1476893690@sss.pgh.pa.us
Whole thread Raw
Responses Re: Portable check for unportable macro usage  (Andres Freund <andres@anarazel.de>)
List pgsql-hackers
A portability hazard that we fight constantly is that the <ctype.h>
macros such as isalpha() can't safely be called on "char" values.
Per POSIX, you have to make sure the input is cast to "unsigned char".
That's far too easy to forget, but on most machines you won't get any
warning about it.

We have a couple of buildfarm machines that will generate "char value
used as array subscript" warnings for this, because their implementations
of these macros are indeed just array lookups.  But I think gaur is
the only one that's still active, and it's not going to last forever.

Ralph Corderoy, one of the nmh hackers, was looking at this problem
and came up with this cute solution [1]:
   #ifndef NDEBUG   #if EOF != -1   #error "Please report this to nmh's authors."   #endif
   extern int ctype_identity[257]; /* [n] = n-1 */   #define isupper(c) ((isupper)((ctype_identity + 1)[c]))   ...
#endif

This essentially converts the macros to always have an array lookup
as the first step, and thus you'll get the char-as-array-subscript
warning if your compiler has one.  (AFAIK, all versions of gcc will
emit that with -Wall.)

You wouldn't want this in production, of course, since it's pure overhead
and the only value is as a compile-time check.  For our purposes, rather
than "#ifndef NDEBUG" we could either use "#ifdef USE_ASSERT_CHECKING"
or invent a separate macro to control this replacement.  I'd be a bit
inclined to invent a separate macro and set up a buildfarm member to
#define that symbol and throw an error on char-as-array-subscript.
Individual developers could #define it for test purposes but I wouldn't
expect most of us to bother, as long as there's a backstop in the build
farm.

Thoughts, objections?
        regards, tom lane

[1] http://lists.nongnu.org/archive/html/nmh-workers/2016-10/msg00313.html



pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Question about behavior of snapshot too old feature
Next
From: Greg Stark
Date:
Subject: packing/alignment annotation for ItemPointerData redux