Skip to content

Commit b7d3c2d

Browse files
ClaytonKnittelcopybara-github
authored andcommitted
Call GetArena() instead of accessing arena_ directly in RepeatedPtrFieldBase.
PiperOrigin-RevId: 805963204
1 parent 524e985 commit b7d3c2d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/google/protobuf/repeated_ptr_field.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ void RepeatedPtrFieldBase::MergeFrom<std::string>(
143143
for (; src < end_assign; ++dst, ++src) {
144144
(*dst)->assign(**src);
145145
}
146-
if (Arena* const arena = arena_) {
146+
Arena* const arena = GetArena();
147+
if (arena != nullptr) {
147148
for (; src < end; ++dst, ++src) {
148149
*dst = Arena::Create<std::string>(arena, **src);
149150
}

src/google/protobuf/repeated_ptr_field.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase {
168168

169169
~RepeatedPtrFieldBase() {
170170
#ifndef NDEBUG
171-
// Try to trigger segfault / asan failure in non-opt builds if arena_
172-
// lifetime has ended before the destructor.
173-
if (arena_) (void)arena_->SpaceAllocated();
171+
// Try to trigger segfault / asan failure in non-opt builds if the arena
172+
// lifetime has ended before the destructor. Note that `GetArena()` is
173+
// not free, but this is debug-only.
174+
const Arena* arena = GetArena();
175+
if (arena != nullptr) (void)arena->SpaceAllocated();
174176
#endif
175177
}
176178

@@ -228,7 +230,7 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase {
228230

229231
// TODO: arena check is redundant once all `RepeatedPtrField`s
230232
// with non-null arena are owned by the arena.
231-
if (ABSL_PREDICT_FALSE(arena_ != nullptr)) return;
233+
if (ABSL_PREDICT_FALSE(GetArena() != nullptr)) return;
232234

233235
using H = CommonHandler<TypeHandler>;
234236
int n = allocated_size();
@@ -481,7 +483,7 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase {
481483
// this case because otherwise a loop calling AddAllocated() followed by
482484
// Clear() would leak memory.
483485
using H = CommonHandler<TypeHandler>;
484-
Delete<H>(element_at(current_size_), arena_);
486+
Delete<H>(element_at(current_size_), GetArena());
485487
} else if (current_size_ < allocated_size()) {
486488
// We have some cleared objects. We don't care about their order, so we
487489
// can just move the first one to the end to make space.

0 commit comments

Comments
 (0)