Add CMake option to run Undefined Behavior Sanitizer (UBSan) (#5211)

* Fix related errors.
This commit is contained in:
Philip Hyunsu Cho
2020-01-20 00:57:44 -08:00
committed by Jiaming Yuan
parent ff1342b252
commit 2a071cebc5
6 changed files with 34 additions and 13 deletions

View File

@@ -31,14 +31,17 @@ namespace common {
template<typename T>
struct SimpleArray {
~SimpleArray() {
free(ptr_);
std::free(ptr_);
ptr_ = nullptr;
}
void resize(size_t n) {
T* ptr = static_cast<T*>(malloc(n*sizeof(T)));
memcpy(ptr, ptr_, n_ * sizeof(T));
free(ptr_);
T* ptr = static_cast<T*>(std::malloc(n * sizeof(T)));
CHECK(ptr) << "Failed to allocate memory";
if (ptr_) {
std::memcpy(ptr, ptr_, n_ * sizeof(T));
std::free(ptr_);
}
ptr_ = ptr;
n_ = n;
}