Skip to content

Commit

Permalink
Fix undefined behavior in tree's Clone() (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
hcho3 authored Jan 7, 2023
1 parent a6f4802 commit 9900b48
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions include/treelite/tree_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,17 @@ template <typename T>
inline ContiguousArray<T>
ContiguousArray<T>::Clone() const {
ContiguousArray clone;
clone.buffer_ = static_cast<T*>(std::malloc(sizeof(T) * capacity_));
if (!clone.buffer_) {
throw Error("Could not allocate memory for the clone");
if (buffer_) {
clone.buffer_ = static_cast<T*>(std::malloc(sizeof(T) * capacity_));
if (!clone.buffer_) {
throw Error("Could not allocate memory for the clone");
}
std::memcpy(clone.buffer_, buffer_, sizeof(T) * size_);
} else {
TREELITE_CHECK_EQ(size_, 0);
TREELITE_CHECK_EQ(capacity_, 0);
clone.buffer_ = nullptr;
}
std::memcpy(clone.buffer_, buffer_, sizeof(T) * size_);
clone.size_ = size_;
clone.capacity_ = capacity_;
clone.owned_buffer_ = true;
Expand Down

0 comments on commit 9900b48

Please sign in to comment.