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 21-Apr-08, at 11:32 AM, Joerg Sonnenberger wrote:
A simple mask just hides the problem and makes it even worse.

No, not really. The warning is only possible for one case that's nowhere near the majority of cases in my experience. The warning doesn't work for any variable defined as "int" or "long" or even "signed char" (though of course the latter will always be in range of the defined _ctype_ array object).

Now values
outside the allowed range will turn into silent wrong numbers.

Well, yes, but that's well within the right of the implementation as per the relevant standards. In any case it's safer than allowing out- of-bounds array accesses (which at best will also silently return the wrong values).

We could change the expression to be more careful using the "?:" operator instead of a mask perhaps, but I'm not sure of the runtime cost. It might be better to go with inline functions instead in that case. I think the mask is generally the most efficient operation on all NetBSD supported architectures if the goal is simply to ensure the array access is kept in bounds.

This is absolutely the wrong approach to the problem. As soon as you
consider non-ASCII charsets, mapping EOF to 255 will return *wrong*

Actually, no, it doesn't, at least on NetBSD.   Try it!  :-)

(Non-ASCII charsets have no relevance w.r.t. the macro versions of the basic "ctype" APIs in NetBSD, which are the ones I'm suggesting need some form of protection to ensure out-of-bounds array accesses do not happen.)

                                        Greg A. Woods; Planix, Inc.

Home | Main Index | Thread Index | Old Index