Skip to content

Commit

Permalink
Minor fixes.
Browse files Browse the repository at this point in the history
* Update libpotassco.
* Ensure that memcpy/memmove is not called with a null src/dest.
* Fix incorrect use of setEnumerationConstraint() in "testMtBug1".
* Fix potential uninitialized read in DefaultMinimize::attach.
  • Loading branch information
BenKaufmann committed Sep 20, 2024
1 parent de62ed7 commit 909c8db
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.1)
project(CLASP VERSION 3.3.10.14 LANGUAGES CXX)
project(CLASP VERSION 3.3.10.15 LANGUAGES CXX)
# Enable folders in IDEs like Visual Studio
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if (POLICY CMP0063)
Expand Down
24 changes: 15 additions & 9 deletions clasp/util/left_right_sequence.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2010-2017 Benjamin Kaufmann
// Copyright (c) 2010-present Benjamin Kaufmann
//
// This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/
//
Expand Down Expand Up @@ -179,11 +179,13 @@ void left_right_rep<L, R>::realloc() {
size_type min_cap = 4 * block_size;
if (new_cap < min_cap) new_cap = min_cap;
buf_type* temp = (buf_type*)::operator new(new_cap*sizeof(buf_type));
// copy left
std::memcpy(temp, begin(), left_size()*sizeof(L));
// copy right
size_type r = cap_ - right_;
std::memcpy(temp+(new_cap-r), right(), right_size() * sizeof(R));
if (!empty()) {
// copy left
std::memcpy(temp, begin(), left_size()*sizeof(L));
// copy right
std::memcpy(temp+(new_cap-r), right(), right_size() * sizeof(R));
}
// swap
release();
buf_ = temp;
Expand Down Expand Up @@ -282,8 +284,10 @@ class left_right_sequence : public bk_lib::detail::select_base<L, R, i>::type {
buf_type* e = this->extra();
size_type c = base_type::inline_raw_cap;
size_type r = c - (this->right_size()*sizeof(right_type));
std::memcpy(e, this->begin(), this->left_size() * sizeof(left_type));
std::memcpy(e+r, this->right(), this->right_size()* sizeof(right_type));
if (!this->empty()) {
std::memcpy(e, this->begin(), this->left_size() * sizeof(left_type));
std::memcpy(e+r, this->right(), this->right_size()* sizeof(right_type));
}
this->release();
this->buf_ = e;
this->cap_ = c;
Expand Down Expand Up @@ -330,8 +334,10 @@ void left_right_sequence<L, R, i>::copy(const left_right_sequence& other) {
}
this->left_ = other.left_;
this->right_= this->cap_ - (other.right_size()*sizeof(right_type));
std::memcpy(this->begin(), other.begin(), other.left_size()*sizeof(left_type));
std::memcpy(this->right(), const_cast<left_right_sequence&>(other).right(), other.right_size()*sizeof(right_type));
if (!other.empty()) {
std::memcpy(this->begin(), other.begin(), other.left_size()*sizeof(left_type));
std::memcpy(this->right(), const_cast<left_right_sequence&>(other).right(), other.right_size()*sizeof(right_type));
}
}

template <class L, class R, unsigned i>
Expand Down
30 changes: 22 additions & 8 deletions clasp/util/pod_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ namespace bk_lib { namespace detail {
struct Memcpy {
Memcpy(const T* first) : first_(first) {}
void operator()(T* out, std::size_t n) const {
std::memcpy(out, first_, n*sizeof(T));
if (out && n) {
std::memcpy(out, first_, n*sizeof(T));
}
}
const T* first_;
};
Expand Down Expand Up @@ -181,7 +183,9 @@ class pod_vector {
* \post size() == other.size() && capacity() == other.size()
*/
pod_vector(const pod_vector& other) : ebo_(other.size(), other.get_allocator()) {
std::memcpy(ebo_.buf, other.begin(), other.size()*sizeof(T));
if (const_pointer buf = other.begin()) {
std::memcpy(ebo_.buf, buf, other.size()*sizeof(T));
}
ebo_.size = other.size();
}

Expand Down Expand Up @@ -341,7 +345,7 @@ class pod_vector {

//! reallocates storage if necessary but never changes the size() of this pod_vector.
/*!
* \note if n is <= capacity() reserve is a noop. Otherwise a reallocation takes place
* \note if n is <= capacity() reserve is a noop. Otherwise, a reallocation takes place
* and capacity() >= n after reserve returned.
* \note reallocation invalidates all references, pointers and iterators referring to
* elements in this pod_vector.
Expand All @@ -351,7 +355,9 @@ class pod_vector {
void reserve(size_type n) {
if (n > capacity()) {
T* temp = ebo_.allocate(n);
std::memcpy(temp, ebo_.buf, size()*sizeof(T));
if (ebo_.buf) {
std::memcpy(temp, ebo_.buf, size()*sizeof(T));
}
ebo_.release();
ebo_.buf = temp;
ebo_.cap = n;
Expand Down Expand Up @@ -451,7 +457,9 @@ class pod_vector {
void append_realloc(size_type n, const T& x) {
size_type new_cap = grow_size(n);
pointer temp = ebo_.allocate(new_cap);
std::memcpy(temp, ebo_.buf, size()*sizeof(T));
if (ebo_.buf) {
std::memcpy(temp, ebo_.buf, size()*sizeof(T));
}
detail::fill(temp+size(), temp+size()+n, x);
ebo_.release();
ebo_.buf = temp;
Expand All @@ -460,7 +468,9 @@ class pod_vector {
}
void move_right(iterator pos, size_type n) {
assert( (pos || n == 0) && (ebo_.eos() - pos) >= (int)n);
std::memmove(pos + n, pos, (end() - pos) * sizeof(T));
if (pos) {
std::memmove(pos + n, pos, (end() - pos) * sizeof(T));
}
}
template <class It>
void insert_range(iterator pos, It first, It last, std::random_access_iterator_tag,
Expand Down Expand Up @@ -499,11 +509,15 @@ class pod_vector {
pointer temp = ebo_.allocate(new_cap);
size_type prefix = static_cast<size_type>(pos-begin());
// copy prefix
std::memcpy(temp, begin(), prefix*sizeof(T));
if (const_pointer buf = begin()) {
std::memcpy(temp, buf, prefix*sizeof(T));
}
// insert new stuff
pred(temp+prefix, n);
// copy suffix
std::memcpy(temp+prefix+n, pos, (end()-pos)*sizeof(T));
if (pos) {
std::memcpy(temp+prefix+n, pos, (end()-pos)*sizeof(T));
}
ebo_.release();
ebo_.buf = temp;
ebo_.size+= n;
Expand Down
2 changes: 1 addition & 1 deletion libpotassco
4 changes: 3 additions & 1 deletion src/clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ SharedLiterals* SharedLiterals::newShareable(const Literal* lits, uint32 size, C
SharedLiterals::SharedLiterals(const Literal* a_lits, uint32 size, ConstraintType t, uint32 refs)
: size_type_( (size << 2) + t ) {
refCount_ = std::max(uint32(1),refs);
std::memcpy(lits_, a_lits, size*sizeof(Literal));
if (a_lits) {
std::memcpy(lits_, a_lits, size*sizeof(Literal));
}
}

uint32 SharedLiterals::simplify(Solver& s) {
Expand Down
7 changes: 5 additions & 2 deletions src/minimize_constraint.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2010-2017 Benjamin Kaufmann
// Copyright (c) 2010-present Benjamin Kaufmann
//
// This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/
//
Expand Down Expand Up @@ -193,7 +193,10 @@ DefaultMinimize::DefaultMinimize(SharedData* d, const OptParams& params)
, pos_(d->lits)
, undo_(0)
, undoTop_(0)
, size_(d->numRules()) {
, posTop_(0)
, size_(d->numRules())
, actLev_(0)
, step_() {
step_.type = params.algo;
if (step_.type == OptParams::bb_hier && d->numRules() == 1) {
step_.type = 0;
Expand Down
6 changes: 3 additions & 3 deletions tests/minimize_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2006-2017 Benjamin Kaufmann
// Copyright (c) 2006-present Benjamin Kaufmann
//
// This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/
//
Expand Down Expand Up @@ -1100,8 +1100,8 @@ TEST_CASE("Core-guided minimize", "[constraint][asp]") {
p.opts |= OptParams::usc_disjoint;
MinimizeConstraint* m1 = data->attach(s1, p);
MinimizeConstraint* m2 = data->attach(s2, p);
s1.setEnumerationConstraint(m1);
s2.setEnumerationConstraint(m2);
SingleOwnerPtr<Constraint, DestroyObject> del1(m1);
SingleOwnerPtr<Constraint, DestroyObject> del2(m2);
BasicSolve solve(s1);
LitVec gp;
while (m1->integrate(s1) || m1->handleUnsat(s1, true, gp)) {
Expand Down

0 comments on commit 909c8db

Please sign in to comment.