Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/google/protobuf/arena_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <memory>
#include <new> // IWYU pragma: keep for operator new
#include <string>
Expand Down Expand Up @@ -72,6 +73,7 @@ using proto2_unittest::TestRepeatedString;
using ::testing::AnyOf;
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::Gt;
using ::testing::HasSubstr;
using ::testing::Optional;
using ::testing::Pointee;
Expand Down Expand Up @@ -1786,6 +1788,34 @@ TEST(ArenaTest, StartingBlockSize) {
EXPECT_EQ(custom_arena.SpaceAllocated(), options.start_block_size);
}

TEST(ArenaTest, VeryLargeAllocIn32BitMode) {
if (sizeof(size_t) != 4) {
GTEST_SKIP() << "Only care about 32-bit mode.";
}

static absl::optional<size_t> large_allocation_size;

// Make sure we don't have bugs allocating buffers greater that ptrdiff_t max.
// It will likely fail, but it should fail in the allocator.

ArenaOptions options;
options.block_alloc = [](size_t n) {
if (n > size_t{std::numeric_limits<ptrdiff_t>::max()}) {
large_allocation_size = n;
// Just fake it. Doesn't matter.
n = 1000000;
}
return ::operator new(n);
};
options.block_dealloc = [](void* ptr, size_t) { ::operator delete(ptr); };

Arena arena(options);
void* ptr = arena.AllocateAligned(size_t{1} << 31);
EXPECT_NE(ptr, nullptr);
EXPECT_THAT(large_allocation_size,
Optional(Gt(size_t{std::numeric_limits<ptrdiff_t>::max()})));
}

TEST(ArenaTest, BlockSizeDoubling) {
Arena arena;
EXPECT_EQ(0, arena.SpaceUsed());
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/serial_arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class PROTOBUF_EXPORT SerialArena {
ABSL_DCHECK_GE(limit_, ptr());
char* ret = ptr();
if (ABSL_PREDICT_FALSE(limit_ - ret < static_cast<ptrdiff_t>(n))) {
// if (ABSL_PREDICT_FALSE(static_cast<size_t>(limit_ - ret) < n)) {
return false;
}
internal::UnpoisonMemoryRegion(ret, n);
Expand Down
Loading