Skip to content

Commit

Permalink
Fix protocolbuffers#9947: make the ABI identical between debug and no…
Browse files Browse the repository at this point in the history
…n-debug builds

(cherry picked from commit 0574167)
  • Loading branch information
pitrou authored and gcjenkinson committed Feb 22, 2024
1 parent 8686ff6 commit e748ad8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/google/protobuf/message_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,18 +520,14 @@ void GenericTypeHandler<std::string>::Merge(const std::string& from,
*to = from;
}

// Non-inline implementations of InternalMetadata routines
#if defined(NDEBUG) || defined(_MSC_VER)
// for opt and MSVC builds, the destructor is defined in the header.
#else
// Non-inline implementations of InternalMetadata destructor
// This is moved out of the header because the GOOGLE_DCHECK produces a lot of code.
InternalMetadata::~InternalMetadata() {
void InternalMetadata::CheckedDestruct() {
if (HasMessageOwnedArenaTag()) {
GOOGLE_DCHECK(!HasUnknownFieldsTag());
delete reinterpret_cast<Arena*>(ptr_ - kMessageOwnedArenaTagMask);
}
}
#endif

// Non-inline variants of std::string specializations for
// various InternalMetadata routines.
Expand Down
13 changes: 10 additions & 3 deletions src/google/protobuf/metadata_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ class InternalMetadata {
GOOGLE_DCHECK(!is_message_owned || arena != nullptr);
}

#if defined(NDEBUG) || defined(_MSC_VER)
// To keep the ABI identical between debug and non-debug builds,
// the destructor is always defined here even though it may delegate
// to a non-inline private method.
// (see https://github.com/protocolbuffers/protobuf/issues/9947)
~InternalMetadata() {
#if defined(NDEBUG) || defined(_MSC_VER)
if (HasMessageOwnedArenaTag()) {
delete reinterpret_cast<Arena*>(ptr_ - kMessageOwnedArenaTagMask);
}
}
#else
~InternalMetadata();
CheckedDestruct();
#endif
}

template <typename T>
void Delete() {
Expand Down Expand Up @@ -269,6 +273,9 @@ class InternalMetadata {
PROTOBUF_NOINLINE void DoSwap(T* other) {
mutable_unknown_fields<T>()->Swap(other);
}

// Private helper with debug checks for ~InternalMetadata()
void CheckedDestruct();
};

// String Template specializations.
Expand Down

0 comments on commit e748ad8

Please sign in to comment.