GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix Arena::merge returning already-allocated memory from an empty des… · meta/folly@7737b3a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • folly/memory
    • Arena-inl.h
    • test
      • ArenaTest.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎folly/memory/Arena-inl.h‎

Copy file name to clipboardExpand all lines: folly/memory/Arena-inl.h
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ void Arena<Alloc>::merge(Arena<Alloc>&& other) {
7171
FOLLY_SAFE_CHECK(
7272
blockGoodAllocSize() == other.blockGoodAllocSize(),
7373
"cannot merge arenas of different minBlockSize");
74+
const bool wasEmpty = blocks_.empty();
7475
blocks_.splice_after(blocks_.before_begin(), other.blocks_);
7576
other.blocks_.clear();
77+
if (wasEmpty) {
78+
// currentBlock_ is still before_begin(); leaving it there would let
79+
// canReuseExistingBlock() bump-allocate into a merged block.
80+
currentBlock_ = blocks_.last();
81+
}
7682
largeBlocks_.splice_after(largeBlocks_.before_begin(), other.largeBlocks_);
7783
other.largeBlocks_.clear();
7884
other.ptr_ = other.end_ = nullptr;
Collapse file

‎folly/memory/test/ArenaTest.cpp‎

Copy file name to clipboardExpand all lines: folly/memory/test/ArenaTest.cpp
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,21 @@ TEST(Arena, Merge) {
374374
EXPECT_EQ(0, arena2.bytesUsed());
375375
}
376376

377+
TEST(Arena, MergeIntoUntouchedArena) {
378+
SysArena src;
379+
auto* live = static_cast<char*>(alloc(src, 1));
380+
*live = 'x';
381+
382+
// dst has never allocated, so its currentBlock_ is still before_begin().
383+
SysArena dst;
384+
dst.merge(std::move(src));
385+
386+
// The merged block holds live memory; it must not be handed out again, and
387+
// the allocation below (which fills with 0xff) must not land on top of it.
388+
EXPECT_NE(live, alloc(dst, 1));
389+
EXPECT_EQ('x', *live);
390+
}
391+
377392
int main(int argc, char* argv[]) {
378393
testing::InitGoogleTest(&argc, argv);
379394
folly::gflags::ParseCommandLineFlags(&argc, &argv, true);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.