From 5bee60d969b9201bdc5f8cf1b919a1c80bb23cf4 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 14 Nov 2024 09:09:13 -0800 Subject: [PATCH] Internal change. PiperOrigin-RevId: 696545060 --- .../cpp/field_generators/message_field.cc | 31 +++++++------------ .../cpp/field_generators/string_field.cc | 10 ++---- .../cpp/field_generators/string_view_field.cc | 10 ++---- src/google/protobuf/compiler/cpp/helpers.h | 18 +++++++++++ src/google/protobuf/repeated_field.cc | 7 +++++ src/google/protobuf/repeated_ptr_field.h | 26 ++++++++++++++-- 6 files changed, 63 insertions(+), 39 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc index b52edfade989..a92e78441619 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc @@ -783,26 +783,17 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { return _internal_mutable_$name_internal$(); } )cc"); - p->Emit( - { - {"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"}, - {"GetExtraArg", - [&] { - p->Emit(opts_->safe_boundary_check - ? ", reinterpret_cast($kDefault$)" - : ""); - }}, - }, - R"cc( - inline const $Submsg$& $Msg$::$name$(int index) const - ABSL_ATTRIBUTE_LIFETIME_BOUND { - $WeakDescriptorSelfPin$; - $annotate_get$; - // @@protoc_insertion_point(field_get:$pkg.Msg.field$) - $StrongRef$; - return _internal_$name_internal$().$Get$(index$GetExtraArg$); - } - )cc"); + p->Emit({GetEmitRepeatedFieldGetterSub(*opts_, p)}, + R"cc( + inline const $Submsg$& $Msg$::$name$(int index) const + ABSL_ATTRIBUTE_LIFETIME_BOUND { + $WeakDescriptorSelfPin$; + $annotate_get$; + // @@protoc_insertion_point(field_get:$pkg.Msg.field$) + $StrongRef$; + return $getter$; + } + )cc"); p->Emit(R"cc( inline $Submsg$* $Msg$::add_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { $WeakDescriptorSelfPin$; diff --git a/src/google/protobuf/compiler/cpp/field_generators/string_field.cc b/src/google/protobuf/compiler/cpp/field_generators/string_field.cc index 439df1a4ba47..76dda20f2a0d 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/string_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/string_field.cc @@ -850,13 +850,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { bool bytes = field_->type() == FieldDescriptor::TYPE_BYTES; p->Emit( { - {"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"}, - {"GetExtraArg", - [&] { - p->Emit(opts_->safe_boundary_check - ? ", $pbi$::GetEmptyStringAlreadyInited()" - : ""); - }}, + GetEmitRepeatedFieldGetterSub(*opts_, p), {"bytes_tag", [&] { if (bytes) { @@ -878,7 +872,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const { $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) - return _internal_$name_internal$().$Get$(index$GetExtraArg$); + return $getter$; } inline std::string* $Msg$::mutable_$name$(int index) ABSL_ATTRIBUTE_LIFETIME_BOUND { diff --git a/src/google/protobuf/compiler/cpp/field_generators/string_view_field.cc b/src/google/protobuf/compiler/cpp/field_generators/string_view_field.cc index c9869f368678..d5d82d1d09b8 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/string_view_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/string_view_field.cc @@ -666,20 +666,14 @@ void RepeatedStringView::GenerateAccessorDeclarations(io::Printer* p) const { void RepeatedStringView::GenerateInlineAccessorDefinitions( io::Printer* p) const { - p->Emit({{"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"}, - {"GetExtraArg", - [&] { - p->Emit(opts_->safe_boundary_check - ? ", $pbi$::GetEmptyStringAlreadyInited()" - : ""); - }}}, + p->Emit({GetEmitRepeatedFieldGetterSub(*opts_, p)}, R"cc( inline absl::string_view $Msg$::$name$(int index) const ABSL_ATTRIBUTE_LIFETIME_BOUND { $WeakDescriptorSelfPin$; $annotate_get$; // @@protoc_insertion_point(field_get:$pkg.Msg.field$) - return _internal_$name_internal$().$Get$(index$GetExtraArg$); + return $getter$; } inline void $Msg$::set_$name$(int index, const std::string& value) { $WeakDescriptorSelfPin$; diff --git a/src/google/protobuf/compiler/cpp/helpers.h b/src/google/protobuf/compiler/cpp/helpers.h index da17961b39b4..d29ffffdda29 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h @@ -1183,6 +1183,24 @@ bool HasOnDeserializeTracker(const Descriptor* descriptor, // signature. bool NeedsPostLoopHandler(const Descriptor* descriptor, const Options& options); +// Emit the repeated field getter for the custom options. +// If safe_boundary_check is specified, it calls the internal checked getter. +inline auto GetEmitRepeatedFieldGetterSub(const Options& options, + io::Printer* p) { + return io::Printer::Sub{ + "getter", + [&options, p] { + if (options.safe_boundary_check) { + p->Emit(R"cc( + $pbi$::CheckedGetOrDefault(_internal_$name_internal$(), index) + )cc"); + } else { + p->Emit(R"cc(_internal_$name_internal$().Get(index))cc"); + } + }} + .WithSuffix(""); +} + // Priority used for static initializers. enum InitPriority { kInitPriority101, diff --git a/src/google/protobuf/repeated_field.cc b/src/google/protobuf/repeated_field.cc index 0626e157361e..adc4c5100b15 100644 --- a/src/google/protobuf/repeated_field.cc +++ b/src/google/protobuf/repeated_field.cc @@ -26,6 +26,13 @@ namespace google { namespace protobuf { +namespace internal { + +void LogIndexOutOfBounds(int index, int size) { + ABSL_DLOG(FATAL) << "Index " << index << " out of bounds " << size; +} + +} // namespace internal template <> PROTOBUF_EXPORT_TEMPLATE_DEFINE size_t diff --git a/src/google/protobuf/repeated_ptr_field.h b/src/google/protobuf/repeated_ptr_field.h index 684dd609404a..dc9f996aa596 100644 --- a/src/google/protobuf/repeated_ptr_field.h +++ b/src/google/protobuf/repeated_ptr_field.h @@ -62,7 +62,6 @@ namespace internal { class MergePartialFromCodedStreamHelper; class SwapFieldHelper; - } // namespace internal namespace internal { @@ -117,6 +116,8 @@ class GenericTypeHandler; // // // Only needs to be implemented if SpaceUsedExcludingSelf() is called. // static int SpaceUsedLong(const Type&); +// +// static const Type& default_instance(); // }; class PROTOBUF_EXPORT RepeatedPtrFieldBase { template @@ -839,6 +840,11 @@ class GenericTypeHandler { static inline size_t SpaceUsedLong(const Type& value) { return value.SpaceUsedLong(); } + + static const Type& default_instance() { + return *static_cast( + MessageTraits::default_instance()); + } }; template <> @@ -875,6 +881,10 @@ class GenericTypeHandler { static size_t SpaceUsedLong(const Type& value) { return sizeof(value) + StringSpaceUsedExcludingSelfLong(value); } + + static const Type& default_instance() { + return GetEmptyStringAlreadyInited(); + } }; } // namespace internal @@ -1178,7 +1188,6 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { using RepeatedPtrFieldBase::InternalGetArenaOffset; - private: using InternalArenaConstructable_ = void; using DestructorSkippable_ = void; @@ -1311,7 +1320,6 @@ inline Element& RepeatedPtrField::at(int index) return RepeatedPtrFieldBase::at(index); } - template inline Element* RepeatedPtrField::Mutable(int index) ABSL_ATTRIBUTE_LIFETIME_BOUND { @@ -1967,6 +1975,18 @@ class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator { RepeatedPtrField* field_; }; +// A utility function for logging that doesn't need any template types. +void LogIndexOutOfBounds(int index, int size); + +template +const T& CheckedGetOrDefault(const RepeatedPtrField& field, int index) { + if (ABSL_PREDICT_FALSE(index < 0 || index >= field.size())) { + LogIndexOutOfBounds(index, field.size()); + return GenericTypeHandler::default_instance(); + } + return field.Get(index); +} + } // namespace internal // Provides a back insert iterator for RepeatedPtrField instances,