[Gdal-dev] Assertion in .tab driver

Mateusz Loskot mateusz at loskot.net
Fri Jun 15 08:04:14 EDT 2007


Mateusz Loskot wrote:
> Daniel wrote:
>> If he got a crash it could very well  be the same problem since
>> isspace expects input that can be represented as an unsigned char or
>> EOF.
>>
>> Looking at Microsofts documentation for isspace at:
>> http://msdn2.microsoft.com/en-us/library/y13z34da(VS.80).aspx
>>
>> "When used with a debug CRT library, isspace will display a CRT assert
>> if passed a parameter that is not EOF or in the range of 0 through
>> 0xFF. When used with a non-debug CRT library, isspace will use the
>> parameter as an index into an array, with undefined results if the
>> parameter is not EOF or in the range of 0 through 0xFF."
> 
> Daniel,
> 
> That's one of the reason why old C functions should be replaced with C++
> and safe equivalents (see below).
> 
>> So this could very well cause a crash. I don't think that the correct
>> solution is to use iswspace since the rest of the code can't handle
>> wide chars anyway.
> 
> IMO, it is not.
> 
> Instead of old functions, it's much better to use ctype facet object
> which serves information about char type characteristics for current
> locale. The facet-based solution is wrapped with template function
> from <locale> library:
> 
> // Test >127 character
> const char c = 128;
> bool space = std::isspace<char>(c, std::locale()));

One additional note, it's not required to specify the type explicitly.
The type can be deduced in compile-time, so the solution is generic:

signed char a = 128;
unsigned char b = 0x20;

std::isspace(a, std::locale()); // returns false
std::isspace(b, std::locale()); // returns true

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Gdal-dev mailing list