From 3a4699a6de31738f1212b3a326af58fe39e22f85 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Tue, 7 Jul 2026 20:54:59 +0200 Subject: [PATCH] Fix empty vector argument array construction --- changelog.in | 11 +++++++++++ gecode/kernel/data/array.hpp | 3 ++- test/array.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/changelog.in b/changelog.in index 3b76bb1a95..da6b386fd8 100755 --- a/changelog.in +++ b/changelog.in @@ -83,6 +83,17 @@ Rank: minor Fix MSVC compilation of matrix slice conversion operators by fully qualifying their dependent return types. +[ENTRY] +Module: kernel +What: bug +Rank: minor +Issue: 205 +Thanks: intractabilis +[DESCRIPTION] +Fix construction of argument arrays from empty `std::vector` instances +so zero-length copies do not take the address of a nonexistent first +element. + [ENTRY] Module: float What: bug diff --git a/gecode/kernel/data/array.hpp b/gecode/kernel/data/array.hpp index ffae114704..3e53964ea0 100644 --- a/gecode/kernel/data/array.hpp +++ b/gecode/kernel/data/array.hpp @@ -1557,7 +1557,8 @@ namespace Gecode { ArgArrayBase::ArgArrayBase(const std::vector& aa) : n(static_cast(aa.size())), capacity(n < onstack_size ? onstack_size : n), a(allocate(n)) { - heap.copy(a,&aa[0],n); + if (n > 0) + heap.copy(a,aa.data(),n); } template diff --git a/test/array.cpp b/test/array.cpp index 1f0a3433a0..74fefb4dd2 100644 --- a/test/array.cpp +++ b/test/array.cpp @@ -34,6 +34,8 @@ #include #include +#include + #include "test/test.hh" /// Check the test result and handle failed test @@ -190,6 +192,32 @@ namespace Test { } }; + /// Test name prefix for vector construction + static const std::string vectorPrefix("Array::Vector::"); + + /// %Class for testing construction from empty vectors + class EmptyVector : public Test::Base { + public: + /// Initialize test + EmptyVector(void) : Test::Base(vectorPrefix + "Empty") {} + /// Perform actual tests + bool run(void) { + std::vector ints; + Gecode::IntArgs ia(ints); + if (ia.size() != 0) + return false; + + std::vector intVars; + Gecode::IntVarArgs iva(intVars); + if (iva.size() != 0) + return false; + + std::vector boolVars; + Gecode::BoolVarArgs bva(boolVars); + return bva.size() == 0; + } + } emptyVectorTest; + /// %Class for testing the VarArray iterator class VarArrayIterator : public Iterator { protected: