Skip to content

Commit

Permalink
Fix #9947: make the ABI identical between debug and non-debug builds
Browse files Browse the repository at this point in the history
  • Loading branch information
pitrou committed Jul 19, 2022
1 parent 8be7304 commit 0574167
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 @@ -521,18 +521,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 @@ -77,15 +77,19 @@ class PROTOBUF_EXPORT 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 @@ -264,6 +268,9 @@ class PROTOBUF_EXPORT 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 0574167

Please sign in to comment.