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

@@ -477,7 +477,7 @@ static void cp_expr_prefix(CPState *cp, CPValue *k)
} else if (cp_opt(cp, '+')) {
cp_expr_unary(cp, k); /* Nothing to do (well, integer promotion). */
} else if (cp_opt(cp, '-')) {
cp_expr_unary(cp, k); k->i32 = -k->i32;
cp_expr_unary(cp, k); k->i32 = (int32_t)(~(uint32_t)k->i32+1);
} else if (cp_opt(cp, '~')) {
cp_expr_unary(cp, k); k->i32 = ~k->i32;
} else if (cp_opt(cp, '!')) {