Skip to content

Commit f4ee35d

Browse files
committed
updated
1 parent b5ea90c commit f4ee35d

2 files changed

Lines changed: 15 additions & 34 deletions

File tree

cpp/src/arrow/buffer_builder.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636

3737
namespace arrow {
3838

39-
static inline bool BufferBuilderCapacityTooLarge(int64_t capacity) {
40-
return ARROW_PREDICT_FALSE(capacity > std::numeric_limits<int64_t>::max() - 63);
41-
}
42-
4339
// ----------------------------------------------------------------------
4440
// Buffer builder classes
4541

@@ -79,12 +75,6 @@ class ARROW_EXPORT BufferBuilder {
7975
/// shrinking the builder.
8076
/// \return Status
8177
Status Resize(const int64_t new_capacity, bool shrink_to_fit = true) {
82-
if (ARROW_PREDICT_FALSE(new_capacity < 0)) {
83-
return Status::Invalid("Resize: negative capacity");
84-
}
85-
if (ARROW_PREDICT_FALSE(BufferBuilderCapacityTooLarge(new_capacity))) {
86-
return Status::CapacityError("Resize: capacity overflow");
87-
}
8878
if (buffer_ == NULLPTR) {
8979
ARROW_ASSIGN_OR_RAISE(buffer_,
9080
AllocateResizableBuffer(new_capacity, alignment_, pool_));
@@ -110,16 +100,10 @@ class ARROW_EXPORT BufferBuilder {
110100
internal::AddWithOverflow(size_, additional_bytes, &min_capacity))) {
111101
return Status::CapacityError("Reserve: capacity overflow");
112102
}
113-
if (ARROW_PREDICT_FALSE(BufferBuilderCapacityTooLarge(min_capacity))) {
114-
return Status::CapacityError("Reserve: capacity overflow");
115-
}
116103
if (min_capacity <= capacity_) {
117104
return Status::OK();
118105
}
119106
int64_t requested_capacity = GrowByFactor(capacity_, min_capacity);
120-
if (ARROW_PREDICT_FALSE(BufferBuilderCapacityTooLarge(requested_capacity))) {
121-
return Status::CapacityError("Reserve: capacity overflow");
122-
}
123107
return Resize(requested_capacity, false);
124108
}
125109

@@ -342,9 +326,6 @@ class TypedBufferBuilder<
342326
new_capacity, static_cast<int64_t>(sizeof(T)), &num_bytes))) {
343327
return Status::CapacityError("Resize: capacity overflow");
344328
}
345-
if (ARROW_PREDICT_FALSE(BufferBuilderCapacityTooLarge(num_bytes))) {
346-
return Status::CapacityError("Resize: capacity overflow");
347-
}
348329
return bytes_builder_.Resize(num_bytes, shrink_to_fit);
349330
}
350331

@@ -357,9 +338,6 @@ class TypedBufferBuilder<
357338
additional_elements, static_cast<int64_t>(sizeof(T)), &num_bytes))) {
358339
return Status::CapacityError("Reserve: size overflow");
359340
}
360-
if (ARROW_PREDICT_FALSE(BufferBuilderCapacityTooLarge(num_bytes))) {
361-
return Status::CapacityError("Reserve: size overflow");
362-
}
363341
return bytes_builder_.Reserve(num_bytes);
364342
}
365343

@@ -525,9 +503,6 @@ class TypedBufferBuilder<bool> {
525503
internal::AddWithOverflow(bit_length_, additional_elements, &min_length))) {
526504
return Status::CapacityError("Reserve: capacity overflow");
527505
}
528-
if (ARROW_PREDICT_FALSE(BufferBuilderCapacityTooLarge(min_length))) {
529-
return Status::CapacityError("Reserve: capacity overflow");
530-
}
531506
return Resize(min_length, false);
532507
}
533508

cpp/src/arrow/memory_pool.cc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ namespace {
6969

7070
constexpr char kDefaultBackendEnvVar[] = "ARROW_DEFAULT_MEMORY_POOL";
7171
constexpr char kDebugMemoryEnvVar[] = "ARROW_DEBUG_MEMORY_POOL";
72+
constexpr int64_t kMaxBufferCapacity = std::numeric_limits<int64_t>::max() - 63;
73+
74+
Status ValidateBufferCapacity(int64_t capacity, const char* operation) {
75+
if (ARROW_PREDICT_FALSE(capacity < 0)) {
76+
return Status::Invalid(operation, ": negative capacity");
77+
}
78+
if (ARROW_PREDICT_FALSE(capacity > kMaxBufferCapacity)) {
79+
return Status::CapacityError(operation, ": capacity overflow");
80+
}
81+
return Status::OK();
82+
}
7283

7384
enum class MemoryPoolBackend : uint8_t { System, Jemalloc, Mimalloc };
7485

@@ -920,9 +931,7 @@ class PoolBuffer final : public ResizableBuffer {
920931
}
921932

922933
Status Reserve(const int64_t capacity) override {
923-
if (capacity < 0) {
924-
return Status::Invalid("Negative buffer capacity: ", capacity);
925-
}
934+
RETURN_NOT_OK(ValidateBufferCapacity(capacity, "Reserve"));
926935
uint8_t* ptr = mutable_data();
927936
if (!ptr || capacity > capacity_) {
928937
ARROW_ASSIGN_OR_RAISE(int64_t new_capacity, RoundCapacity(capacity));
@@ -938,9 +947,7 @@ class PoolBuffer final : public ResizableBuffer {
938947
}
939948

940949
Status Resize(const int64_t new_size, bool shrink_to_fit = true) override {
941-
if (ARROW_PREDICT_FALSE(new_size < 0)) {
942-
return Status::Invalid("Negative buffer resize: ", new_size);
943-
}
950+
RETURN_NOT_OK(ValidateBufferCapacity(new_size, "Resize"));
944951
uint8_t* ptr = mutable_data();
945952
if (ptr && shrink_to_fit && new_size <= size_) {
946953
// Buffer is non-null and is not growing, so shrink to the requested size without
@@ -984,9 +991,7 @@ class PoolBuffer final : public ResizableBuffer {
984991

985992
private:
986993
static Result<int64_t> RoundCapacity(int64_t capacity) {
987-
if (capacity > std::numeric_limits<int64_t>::max() - 63) {
988-
return Status::OutOfMemory("capacity too large");
989-
}
994+
DCHECK_LE(capacity, kMaxBufferCapacity);
990995
return bit_util::RoundUpToMultipleOf64(capacity);
991996
}
992997

@@ -1026,6 +1031,7 @@ Result<std::unique_ptr<ResizableBuffer>> AllocateResizableBuffer(const int64_t s
10261031
Result<std::unique_ptr<ResizableBuffer>> AllocateResizableBuffer(const int64_t size,
10271032
const int64_t alignment,
10281033
MemoryPool* pool) {
1034+
RETURN_NOT_OK(ValidateBufferCapacity(size, "AllocateResizableBuffer"));
10291035
return ResizePoolBuffer<std::unique_ptr<ResizableBuffer>>(
10301036
PoolBuffer::MakeUnique(pool, alignment), size);
10311037
}

0 commit comments

Comments
 (0)