(unsigned)(c+1) <= 256

Preliminary Info:
Codebase = C
Platform = Windows (32 bit)

Okay, let me start by saying that I know why the program crashes with the "(unsigned)(c+1) <= 256" error.

The program is pulling utf8 data text data from sql. When a German Umlaut or another char >0x80 is encountered, it is interpreted as a negative number.

In this particular case, the number being passed is -76.

My question is this then...: What would be the most logical options to avoid this limitation?

The following section of code is from "isctype.c":
1
2
3
4
5
6
7
8
9
#if defined (_DEBUG)
extern "C" int __cdecl _chvalidator(
        int c,
        int mask
        )
{
        _ASSERTE((unsigned)(c + 1) <= 256);
        return _chvalidator_l(NULL, c, mask);
}


If any further info is needed, please let me know. Thank you.
Simply cast to unsigned char. That will reinterpret the signed value as an 8-bit unsigned, and sign extension will work correctly when casting again to larger types.
For example:
1
2
3
char c=-76;
std::cout <<(int)c<<std::endl;                //Sign extension. 0xB4 becomes 0xFFFFFFB4.
std::cout <<(int)(unsigned char)c<<std::endl; //No sign extension. 0xB4 becomes 0x000000B4. 


Whenever you're working with binary data, always use unsigned char as the type. Believe me when I say it'll save you many headaches.

Oh, and it should be <=255. 256 is unreachable for a byte value.
Last edited on
Topic archived. No new replies allowed.