pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Michael Paquier <michael.paquier@gmail.com>
Cc: PostgreSQL mailing lists <pgsql-odbc@postgresql.org>
Subject: Re: Elimination of (more or less) all compilation warnings on OSX
Date: Thu, 06 Mar 2014 20:57:29 +0200
Message-ID: <5318C519.7020209@vmware.com> (raw)
In-Reply-To: <27047.1394124651@sss.pgh.pa.us>
References: <CAB7nPqSSt31_LDfQDTYXCysFMdB1NgLtjR6XUF5CL6f_O4OGaQ@mail.gmail.com>
	<53187E62.4080907@vmware.com>
	<53189C96.1050609@vmware.com>
	<27047.1394124651@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>

On 03/06/2014 06:50 PM, Tom Lane wrote:
> Heikki Linnakangas <hlinnakangas@vmware.com> writes:
>> It's worth noting that isspace, isdigit etc. take an "unsigned char"
>> argument. The compiler won't warn if you pass a "char", so you have to
>> be careful with that. In practice, I don't think it will lead to actual
>> bugs on any real platform and locale, but still..
>
> It definitely used to lead to bugs on some old platforms.  It may be
> that everybody is more careful nowadays, but the POSIX spec is still
> perfectly clear about it:
>
> 	  The c argument is an int, the value of which the application
> 	  shall ensure is a character representable as an unsigned char or
> 	  equal to the value of the macro EOF. If the argument has any
> 	  other value, the behavior is undefined.
>
> I had my old HPUX box hacked up so that gcc would produce "subscript
> is a char" warnings for unsafe usage.  It seems a bit more difficult to
> get modern platforms to do that, though; people stopped exposing the
> underlying flag array in the system headers ...

A-ha, clever. I bet we could do something similar with modern compilers 
too. Searching around, I found this trick (from 
https://stackoverflow.com/questions/4712720/typechecking-macro-arguments-in-c):

> #define CHECK_TYPE(var, type)										\
> 	{																\
> 		__typeof(var) *__tmp;										\
> 		__tmp = (type *) NULL;										\
> 		(void) __tmp; /* to avoid "set but not used" warnings */	\
> 	}

The idea is to create a pointer variable that has the same type as the 
argument, and assign an unsigned char value to it. With -Wpointer-sign, 
gcc will complain. That's not portable, but that's OK. If we can get 
some compilers to complain, that's enough.

With that, we can do:

#ifdef isalnum
#undef isalnum
#endif
#define isalnum(x) ((CHECK_TYPE(x, unsigned char)), isalnum(x))

I did that for all the is* routines, and unsurprisingly there were a 
bunch of places were we got that wrong in psqlodbc. I'll fix those after 
we get the minor release out.

I'll also try that on the PostgreSQL sources.

- Heikki

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

warn-on-signed-arg-to-isalnum-and-friends-1.patch (text/x-diff, 1.8K) download diff --git a/psqlodbc.h b/psqlodbc.h index 0468e0a..785b6eb 100644 --- a/psqlodbc.h +++ b/psqlodbc.h @@ -9,6 +9,68 @@ #ifndef __PSQLODBC_H__ #define __PSQLODBC_H__ +#include <ctype.h> + +#define CHECK_TYPE(var, type) \ + { \ + __typeof(var) *__tmp; \ + __tmp = (type *) NULL; \ + (void) __tmp; /* to avoid "set but not used" warnings */ \ + } + +#ifdef isalnum +#undef isalnum +#endif +#define isalnum(x) ((CHECK_TYPE(x, unsigned char)), isalnum(x)) +#ifdef isalpha +#undef isalpha +#endif +#define isalpha(x) ((CHECK_TYPE(x, unsigned char)), isalpha(x)) +#ifdef isascii +#undef isascii +#endif +#define isascii(x) ((CHECK_TYPE(x, unsigned char)), isascii(x)) +#ifdef isblank +#undef isblank +#endif +#define isblank(x) ((CHECK_TYPE(x, unsigned char)), isblank(x)) +#ifdef iscntrl +#undef iscntrl +#endif +#define iscntrl(x) ((CHECK_TYPE(x, unsigned char)), iscntrl(x)) +#ifdef isdigit +#undef isdigit +#endif +#define isdigit(x) ((CHECK_TYPE(x, unsigned char)), isdigit(x)) +#ifdef isgraph +#undef isgraph +#endif +#define isgraph(x) ((CHECK_TYPE(x, unsigned char)), isgraph(x)) +#ifdef islower +#undef islower +#endif +#define islower(x) ((CHECK_TYPE(x, unsigned char)), islower(x)) +#ifdef isprint +#undef isprint +#endif +#define isprint(x) ((CHECK_TYPE(x, unsigned char)), isprint(x)) +#ifdef ispunct +#undef ispunct +#endif +#define ispunct(x) ((CHECK_TYPE(x, unsigned char)), ispunct(x)) +#ifdef isspace +#undef isspace +#endif +#define isspace(x) ((CHECK_TYPE(x, unsigned char)), isspace(x)) +#ifdef isupper +#undef isupper +#endif +#define isupper(x) ((CHECK_TYPE(x, unsigned char)), isupper(x)) +#ifdef isxdigit +#undef isxdigit +#endif +#define isxdigit(x) ((CHECK_TYPE(x, unsigned char)), isxdigit(x)) + /* #define __MS_REPORTS_ANSI_CHAR__ */ #ifndef WIN32

view thread (5+ messages)

Message-ID: <5318C519.7020209@vmware.com>
Permalink:  ../5318C519.7020209@vmware.com/
Also on:    postgresql.org/message-id/5318C519.7020209@vmware.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: hlinnakangas@vmware.com, tgl@sss.pgh.pa.us, michael.paquier@gmail.com
  Subject: Re: Elimination of (more or less) all compilation warnings on OSX
  In-Reply-To: <5318C519.7020209@vmware.com>
  References: <CAB7nPqSSt31_LDfQDTYXCysFMdB1NgLtjR6XUF5CL6f_O4OGaQ@mail.gmail.com> <53187E62.4080907@vmware.com> <53189C96.1050609@vmware.com> <27047.1394124651@sss.pgh.pa.us> <5318C519.7020209@vmware.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox