diff --git a/cpp/src/arrow/util/logging.h b/cpp/src/arrow/util/logging.h index fccc5e3085d..54f67593bec 100644 --- a/cpp/src/arrow/util/logging.h +++ b/cpp/src/arrow/util/logging.h @@ -40,7 +40,7 @@ namespace arrow { #define ARROW_CHECK(condition) \ (condition) ? 0 : ::arrow::internal::FatalLog(ARROW_FATAL) \ - << __FILE__ << __LINE__ << "Check failed: " #condition " " + << __FILE__ << __LINE__ << " Check failed: " #condition " " #ifdef NDEBUG #define ARROW_DFATAL ARROW_WARNING diff --git a/cpp/src/arrow/util/memory-pool-test.cc b/cpp/src/arrow/util/memory-pool-test.cc index 8e7dfd60baa..919f3740982 100644 --- a/cpp/src/arrow/util/memory-pool-test.cc +++ b/cpp/src/arrow/util/memory-pool-test.cc @@ -46,4 +46,18 @@ TEST(DefaultMemoryPool, OOM) { ASSERT_RAISES(OutOfMemory, pool->Allocate(to_alloc, &data)); } +TEST(DefaultMemoryPoolDeathTest, FreeLargeMemory) { + MemoryPool* pool = default_memory_pool(); + + uint8_t* data; + ASSERT_OK(pool->Allocate(100, &data)); + +#ifndef NDEBUG + EXPECT_EXIT(pool->Free(data, 120), ::testing::ExitedWithCode(1), + ".*Check failed: \\(bytes_allocated_\\) >= \\(size\\)"); +#endif + + pool->Free(data, 100); +} + } // namespace arrow diff --git a/cpp/src/arrow/util/memory-pool.cc b/cpp/src/arrow/util/memory-pool.cc index 0a58e5aa21f..fed149bc359 100644 --- a/cpp/src/arrow/util/memory-pool.cc +++ b/cpp/src/arrow/util/memory-pool.cc @@ -23,6 +23,7 @@ #include #include "arrow/util/status.h" +#include "arrow/util/logging.h" namespace arrow { @@ -81,6 +82,7 @@ int64_t InternalMemoryPool::bytes_allocated() const { void InternalMemoryPool::Free(uint8_t* buffer, int64_t size) { std::lock_guard guard(pool_lock_); + DCHECK_GE(bytes_allocated_, size); std::free(buffer); bytes_allocated_ -= size; }