[freetds] casts and warnings

Christos Zoulas christos at zoulas.com
Wed Mar 9 19:49:42 EST 2005


On Mar 9,  7:21pm, jklowden at schemamania.org ("James K. Lowden") wrote:
-- Subject: Re: [freetds] casts and warnings

| (I would love to know what compiler issued what warning.  I doubt the
| compiler knew about isspace(3)'s peculiar unsigned char restriction.)  

You have to look at the implementation.
If it is implemented as:
#define isspace(c) (__ctype[c] & _S)

gcc will produce a warning because you are indexing an array with a char.

| As I read it, the programmer is obliged to ensure 0 <= c <= 255.  Casting
| a negative value to unsigned char achieves that in only the narrowest of
| technical senses.  0xff == 0xff, but -1 != 255.  Redefining the bits
| doesn't guard against bad inputs; it merely treats a bad input as
| something it's not.  
| 
| To do what you want, we'd do something like this:
| 
| 	for (p+=2; *p >= 0 && isspace(*p); p++) {
| 	...
| 	}
| 	if (*p < 0) {
| 		/* log error */
| 		return 0; /* or whatever */
| 	}
| 
| I think that's overkill, and I think every isspace(3) implementation will
| return zero for any negative value in every locale.  But at least we're
| not  pretending:
| 
| 	 (unsigned char) *p == 128 + (0x7F & *p)
| 
| As far as C is concerned, I assume we agree that as long as *p is
| nonnegative, no cast is needed or desired to promote char to int.  

Casting to unsigned char is the proper way to fix the problem.
There are charsets that have valid characters > 127 and the cast
makes them work.

christos


More information about the FreeTDS mailing list