On Mon, 18 Nov 2013 22:16:54 EST Eitan Adler wrote:
>
> Hi all,
> 
> clang gives a warning when building fmt(1):
> 
> fmt.c:400:35: warning: passing 'char *' to parameter of
>         type 'const unsigned char *' converts between
>         pointers to integer types with different sign
>
> might_be_header takes a pointer to unsigned char.  However there is
> no reason for this to be the case.  The patch bellow fixes that.  In
> addition it adds 'const' to the type of 'line'.

If the only problem is the call to isupper(), then one easy and
useful solution is to define a macro called isupperch(), and use
that instead.  I've come across code in many different projects
where a programmer blindly passes a (char *) into the is*()
functions, and in some cases that mistake has caused real run-time
bugs.  It isn't just a dumb warning from an over-zealous compiler.
And the real run-time bugs may not show up on a given hardware
platform, depending on whether 'plain char' is the same as
'signed char' or 'unsigned char'.  So even programmers who have
exhaustively tested their code may not see any run-time bugs
depending on which platform they are testing on.

In any case, here are examples of what I use for macros:

/*
 * All the following take a parameter of 'int', but expect values in the
 * range of unsigned char.  Define wrappers which take values of type 'char',
 * whether signed or unsigned, and ensure they end up in the right range.
 */
#define isdigitch(Anychar) isdigit((u_char)(Anychar))
#define islowerch(Anychar) islower((u_char)(Anychar))
#define isupperch(Anychar) isupper((u_char)(Anychar))
#define tolowerch(Anychar) tolower((u_char)(Anychar))

-- 
Garance Alistair Drosehn                =     dro...@rpi.edu
Senior Systems Programmer               or   g...@freebsd.org
Rensselaer Polytechnic Institute;  Troy NY


Reply via email to