Skip to content

Commit

Permalink
apacheGH-43885: [C++][CI] Catch potential integer overflow in PoolBuf…
Browse files Browse the repository at this point in the history
…fer (apache#43886)

This should fix https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=71200

* GitHub Issue: apache#43885

Lead-authored-by: Antoine Pitrou <[email protected]>
Co-authored-by: Antoine Pitrou <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
pitrou authored Aug 29, 2024
1 parent 4f91c8f commit 6b24253
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions cpp/src/arrow/memory_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ class PoolBuffer final : public ResizableBuffer {
}
uint8_t* ptr = mutable_data();
if (!ptr || capacity > capacity_) {
int64_t new_capacity = bit_util::RoundUpToMultipleOf64(capacity);
ARROW_ASSIGN_OR_RAISE(int64_t new_capacity, RoundCapacity(capacity));
if (ptr) {
RETURN_NOT_OK(pool_->Reallocate(capacity_, new_capacity, alignment_, &ptr));
} else {
Expand All @@ -878,7 +878,7 @@ class PoolBuffer final : public ResizableBuffer {
if (ptr && shrink_to_fit && new_size <= size_) {
// Buffer is non-null and is not growing, so shrink to the requested size without
// excess space.
int64_t new_capacity = bit_util::RoundUpToMultipleOf64(new_size);
ARROW_ASSIGN_OR_RAISE(int64_t new_capacity, RoundCapacity(new_size));
if (capacity_ != new_capacity) {
// Buffer hasn't got yet the requested size.
RETURN_NOT_OK(pool_->Reallocate(capacity_, new_capacity, alignment_, &ptr));
Expand Down Expand Up @@ -916,6 +916,13 @@ class PoolBuffer final : public ResizableBuffer {
}

private:
static Result<int64_t> RoundCapacity(int64_t capacity) {
if (capacity > std::numeric_limits<int64_t>::max() - 63) {
return Status::OutOfMemory("capacity too large");
}
return bit_util::RoundUpToMultipleOf64(capacity);
}

MemoryPool* pool_;
int64_t alignment_;
};
Expand Down

0 comments on commit 6b24253

Please sign in to comment.