[Opendnssec-develop] quicksorter moved to trunk

Björn Stenberg bjorn at haxx.se
Mon Mar 8 09:30:04 UTC 2010


Ray.Bellis at nominet.org.uk wrote:
> I'd have to say that for best portability the correct fix would be 
> to make sure that chars that need to be unsigned are actually declared 
> that way, rather than relying on compiler flags to change the defaults.

Yes, it's a bit unorthodox. But it's a choice between two evils. I'll explain:

It's a little-known fact that C has not two but three distinct char types: signed char, unsigned char and char.

All the ANSI C string functions take 'char*' as parameter type. With the strict compiler flags we use, passing an 'unsigned char*' or a 'signed char*' to a parameter that takes a 'char*' produces a compiler warning: passing argument 1 of 'strcpy' differ in signedness.

The standard way to silence these warnings is to use typecasts. Lots and lots of typecasts. The disadvantage with flooding the code with typecasts, apart from making it more noisy and hard to read, is that by definition this also disables the static type checking and hence makes the code more susceptible to mistakes and bugs.

There are of course other options. We can use the mem* functions instead of str*, if we add some buffer length housekeeping. We can write wrappers for all the standard C functions that hide away the typecasting. Etc.

In the end I chose to try and keep the code clean. It's pretty gritty as it is. And since all C compilers I have seen have the equivalent of the -funsigned-char option (due to the sign of 'char' being unspecified), I did not consider it a portability hurdle.

If the consensus is that typecasts are preferred over this compiler flag, I'm not going to argue against it.

-- 
Björn



More information about the Opendnssec-develop mailing list