Skip to content

Commit 55c2eab

Browse files
Merge pull request #23899 from protocolbuffers/33rc2-cp
Backport fixes to 33.x
2 parents 182a623 + 4376591 commit 55c2eab

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

java/core/src/main/java/com/google/protobuf/Descriptors.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,45 @@ private static FileDescriptor[] findDescriptors(
495495
return descriptors.toArray(new FileDescriptor[0]);
496496
}
497497

498+
/**
499+
* This method is for backward compatibility with generated code which passed an
500+
* InternalDescriptorAssigner.
501+
*
502+
* @deprecated Any gencode which is using this method is far out of support and should be
503+
* regenerated with a newer version of the protobuf compiler.
504+
*/
505+
@Deprecated
506+
public static void internalBuildGeneratedFileFrom(
507+
final String[] descriptorDataParts,
508+
final FileDescriptor[] dependencies,
509+
final InternalDescriptorAssigner descriptorAssigner) {
510+
final byte[] descriptorBytes = latin1Cat(descriptorDataParts);
511+
512+
FileDescriptorProto proto;
513+
try {
514+
proto = FileDescriptorProto.parseFrom(descriptorBytes);
515+
} catch (InvalidProtocolBufferException e) {
516+
throw new IllegalArgumentException(
517+
"Failed to parse protocol buffer descriptor for generated code.", e);
518+
}
519+
520+
final FileDescriptor result;
521+
try {
522+
// When building descriptors for generated code, we allow unknown
523+
// dependencies by default.
524+
result = buildFrom(proto, dependencies, true);
525+
} catch (DescriptorValidationException e) {
526+
throw new IllegalArgumentException(
527+
"Invalid embedded descriptor for \"" + proto.getName() + "\".", e);
528+
}
529+
530+
final ExtensionRegistry registry = descriptorAssigner.assignDescriptors(result);
531+
532+
if (registry != null) {
533+
throw new RuntimeException("assignDescriptors must return null");
534+
}
535+
}
536+
498537
/**
499538
* This method is to be called by generated code only. It is equivalent to {@code buildFrom}
500539
* except that the {@code FileDescriptorProto} is encoded in protocol buffer wire format.

src/google/protobuf/arena.h

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ constexpr bool FieldHasArenaOffset() {
124124
return !std::is_same_v<T, ArenaRepT>;
125125
}
126126

127+
// TODO - Some types have a deprecated arena-enabled constructor,
128+
// as we plan to remove it in favor of using arena offsets, but for now Arena
129+
// needs to call it. While the arena constructor exists, we will call the
130+
// `InternalVisibility` override to silence the warning.
131+
template <typename T>
132+
constexpr bool HasDeprecatedArenaConstructor() {
133+
return std::is_base_of_v<internal::RepeatedPtrFieldBase, T> &&
134+
!std::is_same_v<T, internal::RepeatedPtrFieldBase>;
135+
}
136+
127137
template <typename T>
128138
void arena_delete_object(void* PROTOBUF_NONNULL object) {
129139
delete reinterpret_cast<T*>(object);
@@ -478,16 +488,12 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8)
478488
static T* PROTOBUF_NONNULL ConstructOnArena(void* PROTOBUF_NONNULL ptr,
479489
Arena& arena, Args&&... args) {
480490
// TODO - ClangTidy gives warnings for calling the deprecated
481-
// `RepeatedPtrField(Arena*)` constructor here, but this is the correct
482-
// way to call it as it will allow us to silently switch to a different
483-
// constructor once arena pointers are removed from RepeatedPtrFields.
484-
// While this constructor exists, we will call the `InternalVisibility`
485-
// override to silence the warning.
486-
//
487-
// Note: RepeatedPtrFieldBase is sometimes constructed internally, and it
488-
// doesn't have `InternalVisibility` constructors.
489-
if constexpr (std::is_base_of_v<internal::RepeatedPtrFieldBase, T> &&
490-
!std::is_same_v<T, internal::RepeatedPtrFieldBase>) {
491+
// constructors here, which leads to log spam. It is correct to invoke
492+
// these constructors through the Arena class as it will allow us to
493+
// silently switch to a different constructor once arena pointers are
494+
// removed. While these constructors exists, we will call the
495+
// `InternalVisibility` overrides to silence the warning.
496+
if constexpr (internal::HasDeprecatedArenaConstructor<T>()) {
491497
return new (ptr) T(internal::InternalVisibility(), &arena,
492498
static_cast<Args&&>(args)...);
493499
} else {
@@ -510,7 +516,8 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8)
510516
// Fields which use arena offsets don't have constructors that take an
511517
// arena pointer. Since the arena is nullptr, it is safe to default
512518
// construct the object.
513-
if constexpr (internal::FieldHasArenaOffset<T>()) {
519+
if constexpr (internal::FieldHasArenaOffset<T>() ||
520+
internal::HasDeprecatedArenaConstructor<T>()) {
514521
return new T();
515522
} else {
516523
return new T(nullptr);
@@ -590,7 +597,8 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8)
590597
static_assert(is_arena_constructable<T>::value,
591598
"Can only construct types that are ArenaConstructable");
592599
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
593-
if constexpr (internal::FieldHasArenaOffset<T>()) {
600+
if constexpr (internal::FieldHasArenaOffset<T>() ||
601+
internal::HasDeprecatedArenaConstructor<T>()) {
594602
return new T(static_cast<Args&&>(args)...);
595603
} else {
596604
return new T(nullptr, static_cast<Args&&>(args)...);
@@ -744,7 +752,12 @@ Arena::DefaultConstruct(Arena* PROTOBUF_NULLABLE arena) {
744752
static_assert(is_destructor_skippable<T>::value, "");
745753
void* mem = arena != nullptr ? arena->AllocateAligned(sizeof(T))
746754
: ::operator new(sizeof(T));
747-
return new (mem) T(arena);
755+
756+
if constexpr (internal::HasDeprecatedArenaConstructor<T>()) {
757+
return new (mem) T(internal::InternalVisibility(), arena);
758+
} else {
759+
return new (mem) T(arena);
760+
}
748761
}
749762

750763
template <typename T>

src/google/protobuf/repeated_ptr_field.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ class ABSL_ATTRIBUTE_WARN_UNUSED RepeatedPtrField final
10221022

10231023
// Arena enabled constructors: for internal use only.
10241024
RepeatedPtrField(internal::InternalVisibility, Arena* arena)
1025-
: RepeatedPtrField(arena) {}
1025+
: RepeatedPtrFieldBase(arena) {}
10261026
RepeatedPtrField(internal::InternalVisibility, Arena* arena,
10271027
const RepeatedPtrField& rhs)
10281028
: RepeatedPtrField(arena, rhs) {}

0 commit comments

Comments
 (0)