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."

    extern int ctype_identity[257]; /* [n] = n-1 */
    #define isupper(c) ((isupper)((ctype_identity + 1)[c]))

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

Thoughts, objections?

                        regards, tom lane

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

Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:

Reply via email to