From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | pgsql-hackers(at)postgreSQL(dot)org |
Subject: | Portable check for unportable <ctype.h> macro usage |
Date: | 2016-10-19 16:14:50 |
Message-ID: | 11911.1476893690@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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
From | Date | Subject | |
---|---|---|---|
Next Message | Greg Stark | 2016-10-19 16:14:52 | packing/alignment annotation for ItemPointerData redux |
Previous Message | Bruce Momjian | 2016-10-19 16:11:38 | Re: Question about behavior of snapshot too old feature |