diff options
| author | 2026-03-14 17:39:29 +0100 | |
|---|---|---|
| committer | 2026-03-14 17:46:57 +0100 | |
| commit | de8673af7c0885201f284ffd8851ece2cde8cb18 (patch) | |
| tree | 162475f18ce772b3038250f6422b7626a1d75815 | |
| parent | 1400850e7f579d2c0aa2bf2dddffc4e05d87a804 (diff) | |
eval & fold: handle division overflow for MIN/-1
| -rw-r--r-- | c/eval.c | 3 | ||||
| -rw-r--r-- | ir/fold.c | 8 |
2 files changed, 9 insertions, 2 deletions
@@ -1,5 +1,6 @@ #include "c.h" #include "../ir/ir.h" +#include <limits.h> static int targ2hosttype(enum typetag t) @@ -303,6 +304,7 @@ binop(struct expr *ex, enum evalmode mode) lhs->u /= rhs->u; break; case EDIV|S: if (!rhs->i) return 0; + if (lhs->i == LLONG_MIN && rhs->i == -1) break; lhs->i /= rhs->i; break; case EDIV|F: lhs->f /= rhs->f; break; @@ -311,6 +313,7 @@ binop(struct expr *ex, enum evalmode mode) lhs->u %= rhs->u; break; case EREM|S: if (!rhs->i) return 0; + if (lhs->i == LLONG_MIN && rhs->i == -1) lhs->i = 0; lhs->i %= rhs->i; break; @@ -41,9 +41,13 @@ foldint(enum op op, enum irclass k, union ref lr, union ref rr) case Oadd: x = l.u + r.u; break; case Osub: x = l.u - r.u; break; case Omul: x = l.u * r.u; break; - case Odiv: x = w ? l.s / r.s : (int)l.s / (int)r.s; break; + case Odiv: if (r.s == -1 && (w ? l.s == LLONG_MIN : (int)l.s == INT_MIN)) x = l.s; + else x = w ? l.s / r.s : (int)l.s / (int)r.s; + break; case Oudiv: x = w ? l.u / r.u : (uint)l.u / (uint)r.u; break; - case Orem: x = w ? l.s % r.s : (int)l.s % (int)r.s; break; + case Orem: if (r.s == -1 && (w ? l.s == LLONG_MIN : (int)l.s == INT_MIN)) x = 0; + else x = w ? l.s % r.s : (int)l.s % (int)r.s; + break; case Ourem: x = w ? l.u % r.u : (uint)l.u % (uint)r.u; break; case Oand: x = l.u & r.u; break; case Oior: x = l.u | r.u; break; |