tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: using the interfaces in ctype.h




On Apr 17, 2008, at 7:47 AM, Greg A. Woods; Planix, Inc. wrote:

On 17-Apr-08, at 8:11 AM, Christos Zoulas wrote:

It is not an issue of avoiding the warnings. For example isspace() is
defined in:

http://www.opengroup.org/onlinepubs/009695399/functions/ isspace.html

as:

  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.

So by casting a signed char to an int, you can get a large negative number and you end up with "undefined behavior" which might mean a core dump,
so don't do it!


Argh! Sorry, I should have been able to figure that out myself, given the usual implementations. My brain doesn't seem so good at seeing the possibility of negative array indexing -- too many years of sloppy assumptions.

However what you and Alan said about simply casting to (unsigned char) isn't sufficient, at least on some platforms (IRIX-6, IIRC), at least not to keep the compiler happy.

Sadly I do get the least warnings from every platform when the cast is only to (int). I wonder if the compiler can be taught to detect uses where a parameter may have a value out of range even though the data type it is declared as is wide enough. The only solution the compiler can really believe though, I think, would be a double cast where the value is narrowed sufficiently before being widened again for the call.


I believe this is called ADA

James

Just as sadly Joerg's advice to read about the value in the CAVEATS section doesn't help, at least not on NetBSD-4.0 or earlier. Unfortunately I also missed the section you quoted from the SUS standard, so I'm not sure peeking at -current would have helped me. Neither the SUSv2 docs online, nor any variant of the NetBSD manual pages, contain any good examples.

--
                                        Greg A. Woods; Planix, Inc.
                                        <woods%planix.ca@localhost>






Home | Main Index | Thread Index | Old Index