From 940910efb6234a1fbaa30ad488f49580a09c0d9f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Feb 2026 18:11:53 +0000 Subject: [PATCH] Fix undefined behavior in SMALL_INT_MAX/MIN constants by using unsigned arithmetic Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com> --- src/util/mpz.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/util/mpz.h b/src/util/mpz.h index 555b5e1de..51f1e0d68 100644 --- a/src/util/mpz.h +++ b/src/util/mpz.h @@ -99,8 +99,9 @@ private: static constexpr int SMALL_BITS = sizeof(uintptr_t) * 8 - 1; // Maximum and minimum values that can be stored as small integers - static constexpr int64_t SMALL_INT_MAX = (static_cast(1) << (SMALL_BITS - 1)) - 1; - static constexpr int64_t SMALL_INT_MIN = -(static_cast(1) << (SMALL_BITS - 1)); + // Use unsigned arithmetic to avoid undefined behavior on left shift + static constexpr int64_t SMALL_INT_MAX = (static_cast(1) << (SMALL_BITS - 1)) - 1; + static constexpr int64_t SMALL_INT_MIN = -(static_cast(static_cast(1) << (SMALL_BITS - 1))); static bool fits_in_small(int64_t v) { return v >= SMALL_INT_MIN && v <= SMALL_INT_MAX;