Avoid negation of signed integers in C that may hold INT*_MIN.

Reported by minoki.
Recent C compilers 'take advantage' of the undefined behavior.
This completely changes the meaning of expressions like (k == -k).
This commit is contained in:
Mike Pall
2022-12-22 00:03:06 +01:00
parent b2791179ef
commit 8a5e398c52
12 changed files with 32 additions and 32 deletions

View File

@@ -577,7 +577,7 @@ GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned)
if (isunsigned) {
*--p = 'U';
} else if ((int64_t)n < 0) {
n = (uint64_t)-(int64_t)n;
n = ~n+1u;
sign = 1;
}
do { *--p = (char)('0' + n % 10); } while (n /= 10);