LibJS+AK: Fix integer overflow UB on (any Int32 - -2147483648)

It wasn't safe to use addition_would_overflow(a, -b) to check if
subtraction (a - b) would overflow, since it doesn't cover this case.

I don't know why we didn't have subtraction_would_overflow(), so this
patch adds it. :^)
This commit is contained in:
Andreas Kling 2024-05-18 10:58:11 +02:00
parent 1de475b404
commit b2e6843055
Notes: sideshowbarker 2024-07-17 07:31:31 +09:00
3 changed files with 27 additions and 1 deletions

View file

@ -0,0 +1,10 @@
test("basic integer overflow correctness", () => {
expect(2147483647 + 1).toBe(2147483648);
expect(2147483648 - 1).toBe(2147483647);
expect(0 - 2147483647).toBe(-2147483647);
expect(0 - 2147483648).toBe(-2147483648);
expect(0 - -2147483647).toBe(2147483647);
expect(0 - -2147483648).toBe(2147483648);
expect(0 + -2147483647).toBe(-2147483647);
expect(0 + -2147483648).toBe(-2147483648);
});