Comparison const int in ndk 17.0.4

0

I have a issue about comparision const int below.

AK_FORCE_INLINE static int intArrayToCharArray(const int *const source, const int sourceSize,
    char *dest, const int destSize) {
// We want to always terminate with a 0 char, so stop one short of the length to make
// sure there is room.
const int destLimit = destSize - 1;
int si = 0;
int di = 0;
while (si < sourceSize && di < destLimit && 0 != source[si]) {
    const int codePoint = source[si++];
    if (codePoint < 0x7F) { // One byte
        dest[di++] = codePoint;
    } else if (codePoint < 0x7FF) { // Two bytes
        if (di + 1 >= destLimit) break;
        dest[di++] = 0xC0 + (codePoint >> 6);
        dest[di++] = 0x80 + (codePoint & 0x3F);
    } else if (codePoint < 0xFFFF) { // Three bytes
        if (di + 2 >= destLimit) break;
        dest[di++] = 0xE0 + (codePoint >> 12);
        dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
        dest[di++] = 0x80 + (codePoint & 0x3F);
    } else if (codePoint <= 0x1FFFFF) { // Four bytes
        if (di + 3 >= destLimit) break;
        dest[di++] = 0xF0 + (codePoint >> 18);
        dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
        dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
        dest[di++] = 0x80 + (codePoint & 0x3F);
    } else if (codePoint <= 0x3FFFFFF) { // Five bytes
        if (di + 4 >= destLimit) break;
        dest[di++] = 0xF8 + (codePoint >> 24);
        dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
        dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
        dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
        dest[di++] = codePoint & 0x3F;
    } else if (codePoint <= 0x7FFFFFFF) { // Six bytes
        if (di + 5 >= destLimit) break;
        dest[di++] = 0xFC + (codePoint >> 30);
        dest[di++] = 0x80 + ((codePoint >> 24) & 0x3F);
        dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
        dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
        dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
        dest[di++] = codePoint & 0x3F;
    } else {
        // Not a code point... skip.
    }
}
dest[di] = 0;
return di;

}

It is not a problem with ndk 16 and lower. But when I upto ndk 17. It make bug below. Android debugger show at line

else if (codePoint <= 0x7FFFFFFF)

And this is bug:

Error:(88, 30) error: comparison 'const int' <= 2147483647 is always true [-Werror,-Wtautological-constant-compare]

Thanks for your attending.

android
c++11
android-ndk
comparison
asked on Stack Overflow May 24, 2018 by huy nguyen

1 Answer

1

Since codePoint is an int, it is a signed 32 bit number. My guess it that NDK r17 is more strict (maybe -Wtautological-constant-compare was added - can't find it the release notes).

Instead of using int, I think you need to use unsigned int (or maybe uint_least16_t or even uint_fast16_t). See also http://en.cppreference.com/w/cpp/language/types (section Character types).

answered on Stack Overflow May 24, 2018 by geisshirt

User contributions licensed under CC BY-SA 3.0