summaryrefslogtreecommitdiff
path: root/src/lj_parse.c
diff options
context:
space:
mode:
authorMike Pall <mike>2022-12-22 00:03:06 +0100
committerMike Pall <mike>2022-12-22 00:03:06 +0100
commit8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b (patch)
treedae089564f58db2963bae8e3530c1faae104cc61 /src/lj_parse.c
parentb2791179ef96d652d00d78d2a8780af690537f6a (diff)
downloadluajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.gz
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.bz2
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.zip
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).
Diffstat (limited to 'src/lj_parse.c')
-rw-r--r--src/lj_parse.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/lj_parse.c b/src/lj_parse.c
index 5a8bcff9..57eb11cc 100644
--- a/src/lj_parse.c
+++ b/src/lj_parse.c
@@ -951,22 +951,22 @@ static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
951#if LJ_HASFFI 951#if LJ_HASFFI
952 if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */ 952 if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */
953 GCcdata *cd = cdataV(&e->u.nval); 953 GCcdata *cd = cdataV(&e->u.nval);
954 int64_t *p = (int64_t *)cdataptr(cd); 954 uint64_t *p = (uint64_t *)cdataptr(cd);
955 if (cd->ctypeid == CTID_COMPLEX_DOUBLE) 955 if (cd->ctypeid == CTID_COMPLEX_DOUBLE)
956 p[1] ^= (int64_t)U64x(80000000,00000000); 956 p[1] ^= U64x(80000000,00000000);
957 else 957 else
958 *p = -*p; 958 *p = ~*p+1u;
959 return; 959 return;
960 } else 960 } else
961#endif 961#endif
962 if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */ 962 if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */
963 TValue *o = expr_numtv(e); 963 TValue *o = expr_numtv(e);
964 if (tvisint(o)) { 964 if (tvisint(o)) {
965 int32_t k = intV(o); 965 int32_t k = intV(o), negk = (int32_t)(~(uint32_t)k+1u);
966 if (k == -k) 966 if (k == negk)
967 setnumV(o, -(lua_Number)k); 967 setnumV(o, -(lua_Number)k);
968 else 968 else
969 setintV(o, -k); 969 setintV(o, negk);
970 return; 970 return;
971 } else { 971 } else {
972 o->u64 ^= U64x(80000000,00000000); 972 o->u64 ^= U64x(80000000,00000000);