From 8e47c0d842fc8e41343ba5307d6ea4db4c20225d Mon Sep 17 00:00:00 2001 From: Lev Nachmanson Date: Wed, 11 Mar 2026 12:42:39 -1000 Subject: [PATCH 1/3] Fixed the assertion violation in `mpz.cpp:602` when running with `-tr:arith`. **Root cause**: `vector::resize(SZ s, Args args...)` in `src/util/vector.h` took `args` by value and used `std::forward(args)` in a loop. The first iteration moved from `args`, leaving all subsequent elements with a moved-from state (`rational{0/0}` instead of `rational{0/1}`). This corrupted the coefficient vector in the pretty printer, causing a division-by-zero assertion when multiplying. **Fix**: Changed `resize` to take `Args const& args` and copy-construct each element instead of forwarding/moving. --- src/util/vector.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/vector.h b/src/util/vector.h index 8d1632ced..41f81fc95 100644 --- a/src/util/vector.h +++ b/src/util/vector.h @@ -494,7 +494,7 @@ public: } template - void resize(SZ s, Args args...) { + void resize(SZ s, Args const& args) { SZ sz = size(); if (s <= sz) { shrink(s); return; } while (s > capacity()) { @@ -505,7 +505,7 @@ public: iterator it = m_data + sz; iterator end = m_data + s; for (; it != end; ++it) { - new (it) T(std::forward(args)); + new (it) T(args); } } From 385b11f55b0d26b9b134bbbb1bf1b3f9edc4498a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 23:18:26 +0000 Subject: [PATCH 2/3] Initial plan From 01f9709ff63aa649051d447ad77d138bfd681ad3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 23:54:01 +0000 Subject: [PATCH 3/3] Add vector::resize tests including vector Co-authored-by: levnach <5377127+levnach@users.noreply.github.com> --- src/test/vector.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/test/vector.cpp b/src/test/vector.cpp index 7a13558a2..0bc2aeb36 100644 --- a/src/test/vector.cpp +++ b/src/test/vector.cpp @@ -17,8 +17,92 @@ Revision History: --*/ #include "util/vector.h" +#include "util/rational.h" #include +static void tst_resize_rational() { + // grow from empty using default initialization (zero) + vector v; + v.resize(4); + ENSURE(v.size() == 4); + for (unsigned i = 0; i < 4; ++i) + ENSURE(v[i].is_zero()); + + // shrink: elements below new size are preserved + v.resize(2); + ENSURE(v.size() == 2); + for (unsigned i = 0; i < 2; ++i) + ENSURE(v[i].is_zero()); + + // grow with explicit value initialization + rational half(1, 2); + v.resize(6, half); + ENSURE(v.size() == 6); + for (unsigned i = 0; i < 2; ++i) + ENSURE(v[i].is_zero()); + for (unsigned i = 2; i < 6; ++i) + ENSURE(v[i] == half); + + // resize to same size is a no-op + rational three(3); + v.resize(6, three); + ENSURE(v.size() == 6); + for (unsigned i = 2; i < 6; ++i) + ENSURE(v[i] == half); + + // resize to zero clears the vector + v.resize(0); + ENSURE(v.empty()); + + // grow again after being empty + rational neg(-7); + v.resize(3, neg); + ENSURE(v.size() == 3); + for (unsigned i = 0; i < 3; ++i) + ENSURE(v[i] == neg); +} + +static void tst_resize() { + // grow from empty using default initialization + svector v; + v.resize(5); + ENSURE(v.size() == 5); + ENSURE(v.capacity() >= 5); + for (unsigned i = 0; i < 5; ++i) + ENSURE(v[i] == 0); + + // shrink: elements below new size are preserved, size shrinks + v.resize(3); + ENSURE(v.size() == 3); + for (unsigned i = 0; i < 3; ++i) + ENSURE(v[i] == 0); + + // grow with explicit value initialization + v.resize(7, 42); + ENSURE(v.size() == 7); + for (unsigned i = 0; i < 3; ++i) + ENSURE(v[i] == 0); + for (unsigned i = 3; i < 7; ++i) + ENSURE(v[i] == 42); + + // resize to same size is a no-op + v.resize(7, 99); + ENSURE(v.size() == 7); + for (unsigned i = 3; i < 7; ++i) + ENSURE(v[i] == 42); + + // resize to zero clears the vector + v.resize(0); + ENSURE(v.empty()); + ENSURE(v.size() == 0); + + // grow again after being empty + v.resize(4, 10); + ENSURE(v.size() == 4); + for (unsigned i = 0; i < 4; ++i) + ENSURE(v[i] == 10); +} + static void tst1() { svector v1; ENSURE(v1.empty()); @@ -58,5 +142,7 @@ static void tst1() { } void tst_vector() { + tst_resize_rational(); + tst_resize(); tst1(); }