From 3f9ba304249fa201f2766210b9686856464889c8 Mon Sep 17 00:00:00 2001 From: Sebastian Ullrich Date: Mon, 14 Nov 2022 17:51:48 +0100 Subject: [PATCH] fix: integer overflows --- .github/workflows/ci.yml | 2 +- src/runtime/mpz.cpp | 6 +++--- tests/lean/1825.lean | 2 ++ tests/lean/1825.lean.expected.out | 6 ++++++ 4 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 tests/lean/1825.lean create mode 100644 tests/lean/1825.lean.expected.out diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index daf727152c..52645f0b9f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -142,7 +142,7 @@ jobs: if: matrix.os == 'windows-2022' - name: Install Brew Packages run: | - brew install ccache tree zstd coreutils + brew install ccache tree zstd coreutils gmp if: matrix.os == 'macos-latest' - name: Cache uses: actions/cache@v2 diff --git a/src/runtime/mpz.cpp b/src/runtime/mpz.cpp index e2f5aad26c..92e9cecf8b 100644 --- a/src/runtime/mpz.cpp +++ b/src/runtime/mpz.cpp @@ -47,7 +47,7 @@ mpz::mpz(uint64 v): mpz::mpz(int64 v) { uint64 w; - if (v < 0) w = -v; + if (v < 0) w = -static_cast(v); else w = v; mpz_init_set_ui(m_val, static_cast(w)); mpz tmp(static_cast(w >> 32)); @@ -146,13 +146,13 @@ mpz & mpz::operator+=(mpz const & o) { mpz_add(m_val, m_val, o.m_val); return *t mpz & mpz::operator+=(unsigned u) { mpz_add_ui(m_val, m_val, u); return *this; } -mpz & mpz::operator+=(int u) { if (u >= 0) mpz_add_ui(m_val, m_val, u); else mpz_sub_ui(m_val, m_val, -u); return *this; } +mpz & mpz::operator+=(int u) { if (u >= 0) mpz_add_ui(m_val, m_val, u); else mpz_sub_ui(m_val, m_val, -static_cast(u)); return *this; } mpz & mpz::operator-=(mpz const & o) { mpz_sub(m_val, m_val, o.m_val); return *this; } mpz & mpz::operator-=(unsigned u) { mpz_sub_ui(m_val, m_val, u); return *this; } -mpz & mpz::operator-=(int u) { if (u >= 0) mpz_sub_ui(m_val, m_val, u); else mpz_add_ui(m_val, m_val, -u); return *this; } +mpz & mpz::operator-=(int u) { if (u >= 0) mpz_sub_ui(m_val, m_val, u); else mpz_add_ui(m_val, m_val, -static_cast(u)); return *this; } mpz & mpz::operator*=(mpz const & o) { mpz_mul(m_val, m_val, o.m_val); return *this; } diff --git a/tests/lean/1825.lean b/tests/lean/1825.lean new file mode 100644 index 0000000000..954494646c --- /dev/null +++ b/tests/lean/1825.lean @@ -0,0 +1,2 @@ +theorem boom : -2147483648 + 2147483648 = 0 := by rfl +theorem boom' : -2147483648 + 2147483648 = -18446744069414584320 := by native_decide diff --git a/tests/lean/1825.lean.expected.out b/tests/lean/1825.lean.expected.out new file mode 100644 index 0000000000..ad5c7a9d63 --- /dev/null +++ b/tests/lean/1825.lean.expected.out @@ -0,0 +1,6 @@ +1825.lean:2:8-2:13: error: application type mismatch + Lean.ofReduceBool boom'._nativeDecide_1 true (Eq.refl true) +argument has type + true = true +but function has type + Lean.reduceBool boom'._nativeDecide_1 = true → boom'._nativeDecide_1 = true