diff --git a/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java b/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java index ef30749325..adf5a2abbf 100644 --- a/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java +++ b/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java @@ -151,7 +151,7 @@ public class ProtoCaliperBenchmark { } } - @SuppressWarnings("IgnoredPureGetter") + @SuppressWarnings({"IgnoredPureGetter", "CheckReturnValue"}) @Benchmark void serializeToByteArray(int reps) throws IOException { if (sampleMessageList.size() == 0) { diff --git a/java/core/src/main/java/com/google/protobuf/MessageLite.java b/java/core/src/main/java/com/google/protobuf/MessageLite.java index f9b2f6653f..8c172eef8c 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/MessageLite.java @@ -59,6 +59,7 @@ import java.io.OutputStream; * * @author kenton@google.com Kenton Varda */ +@CheckReturnValue public interface MessageLite extends MessageLiteOrBuilder { /** @@ -138,6 +139,7 @@ public interface MessageLite extends MessageLiteOrBuilder { /** Abstract interface implemented by Protocol Message builders. */ interface Builder extends MessageLiteOrBuilder, Cloneable { /** Resets all fields to their default values. */ + @CanIgnoreReturnValue Builder clear(); /** @@ -181,11 +183,12 @@ public interface MessageLite extends MessageLiteOrBuilder { *

Note: The caller should call {@link CodedInputStream#checkLastTagWas(int)} after calling * this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF. * - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream */ + @CanIgnoreReturnValue Builder mergeFrom(CodedInputStream input) throws IOException; /** @@ -193,11 +196,12 @@ public interface MessageLite extends MessageLiteOrBuilder { * that you want to be able to parse must be registered in {@code extensionRegistry}. Extensions * not in the registry will be treated as unknown fields. * - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream */ + @CanIgnoreReturnValue Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws IOException; @@ -209,10 +213,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException; /** @@ -220,10 +225,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; @@ -232,10 +238,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException; /** @@ -243,10 +250,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException; /** @@ -254,10 +262,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; @@ -266,10 +275,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; @@ -283,12 +293,13 @@ public interface MessageLite extends MessageLiteOrBuilder { * *

Despite usually reading the entire input, this does not close the stream. * - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(InputStream input) throws IOException; /** @@ -298,6 +309,7 @@ public interface MessageLite extends MessageLiteOrBuilder { * * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws IOException; @@ -317,6 +329,7 @@ public interface MessageLite extends MessageLiteOrBuilder { * *

This is equivalent to the {@code Message::MergeFrom} method in C++. */ + @CanIgnoreReturnValue Builder mergeFrom(MessageLite other); /** @@ -326,11 +339,12 @@ public interface MessageLite extends MessageLiteOrBuilder { * * @return true if successful, or false if the stream is at EOF when the method starts. Any * other error (including reaching EOF during parsing) causes an exception to be thrown. - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream */ + @CanIgnoreReturnValue // TODO(kak): should this be @CheckReturnValue instead? boolean mergeDelimitedFrom(InputStream input) throws IOException; /** @@ -338,11 +352,12 @@ public interface MessageLite extends MessageLiteOrBuilder { * * @return true if successful, or false if the stream is at EOF when the method starts. Any * other error (including reaching EOF during parsing) causes an exception to be thrown. - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream */ + @CanIgnoreReturnValue // TODO(kak): should this be @CheckReturnValue instead? boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws IOException; } diff --git a/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java b/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java index 7a5ef3ed51..927b95fcc2 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java +++ b/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java @@ -36,6 +36,7 @@ package com.google.protobuf; * * @author jonp@google.com (Jon Perlow) */ +@CheckReturnValue public interface MessageLiteOrBuilder { /** * Get an instance of the type with no fields set. Because no fields are set, all getters for diff --git a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java index 55864afa72..f4c4baf4a2 100644 --- a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java @@ -1529,7 +1529,7 @@ public class GeneratedMessageTest { assertThat(builder.getFooInt()).isEqualTo(123); TestOneof2 message = builder.buildPartial(); assertThat(message.hasFooInt()).isTrue(); - assertThat(123).isEqualTo(message.getFooInt()); + assertThat(message.getFooInt()).isEqualTo(123); assertThat(builder.clearFooInt().hasFooInt()).isFalse(); TestOneof2 message2 = builder.build(); diff --git a/java/core/src/test/java/com/google/protobuf/TestUtil.java b/java/core/src/test/java/com/google/protobuf/TestUtil.java index 46a4d17d61..71a540a0c4 100644 --- a/java/core/src/test/java/com/google/protobuf/TestUtil.java +++ b/java/core/src/test/java/com/google/protobuf/TestUtil.java @@ -235,6 +235,7 @@ import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestUnpackedTypes; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.Collections; @@ -3842,7 +3843,11 @@ public final class TestUtil { private static ByteString readBytesFromResource(String name) { try { - return ByteString.readFrom(TestUtil.class.getResourceAsStream(name)); + InputStream in = TestUtil.class.getResourceAsStream(name); + if (in == null) { // + throw new RuntimeException("Tests data file " + name + " is missing."); + } + return ByteString.readFrom(in); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h index 3b5f16c389..0f9f81366f 100644 --- a/src/google/protobuf/arena.h +++ b/src/google/protobuf/arena.h @@ -315,6 +315,15 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { static_cast(args)...); } + // API to delete any objects not on an arena. This can be used to safely + // clean up messages or repeated fields without knowing whether or not they're + // owned by an arena. The pointer passed to this function should not be used + // again. + template + PROTOBUF_ALWAYS_INLINE static void Destroy(T* obj) { + if (InternalGetOwningArena(obj) == nullptr) delete obj; + } + // Allocates memory with the specific size and alignment. void* AllocateAligned(size_t size, size_t align = 8) { if (align <= 8) { @@ -411,8 +420,23 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { template class InternalHelper { private: - // Provides access to protected GetOwningArena to generated messages. - static Arena* GetOwningArena(const T* p) { return p->GetOwningArena(); } + struct Rank1 {}; + struct Rank0 : Rank1 {}; + + static Arena* GetOwningArena(const T* p) { + return GetOwningArena(Rank0{}, p); + } + + template + static auto GetOwningArena(Rank0, const U* p) + -> decltype(p->GetOwningArena()) { + return p->GetOwningArena(); + } + + template + static Arena* GetOwningArena(Rank1, const U* p) { + return nullptr; + } static void InternalSwap(T* a, T* b) { a->InternalSwap(b); } @@ -771,25 +795,6 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { return nullptr; } - template - PROTOBUF_ALWAYS_INLINE static Arena* GetOwningArena(const T* value) { - return GetOwningArenaInternal( - value, std::is_convertible()); - } - - // Implementation for GetOwningArena(). All and only message objects have - // GetOwningArena() method. - template - PROTOBUF_ALWAYS_INLINE static Arena* GetOwningArenaInternal( - const T* value, std::true_type) { - return InternalHelper::GetOwningArena(value); - } - template - PROTOBUF_ALWAYS_INLINE static Arena* GetOwningArenaInternal( - const T* /* value */, std::false_type) { - return nullptr; - } - void* AllocateAlignedWithHookForArray(size_t n, size_t align, const std::type_info* type) { if (align <= 8) { diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc index 7539b4b296..017b5f8097 100644 --- a/src/google/protobuf/arena_unittest.cc +++ b/src/google/protobuf/arena_unittest.cc @@ -307,6 +307,26 @@ TEST(ArenaTest, InitialBlockTooSmall) { } } +TEST(ArenaTest, CreateDestroy) { + TestAllTypes original; + TestUtil::SetAllFields(&original); + + // Test memory leak. + Arena arena; + TestAllTypes* heap_message = Arena::CreateMessage(nullptr); + TestAllTypes* arena_message = Arena::CreateMessage(&arena); + + *heap_message = original; + *arena_message = original; + + Arena::Destroy(heap_message); + Arena::Destroy(arena_message); + + // The arena message should still exist. + EXPECT_EQ(strlen(original.optional_string().c_str()), + strlen(arena_message->optional_string().c_str())); +} + TEST(ArenaTest, Parsing) { TestAllTypes original; TestUtil::SetAllFields(&original); diff --git a/src/google/protobuf/compiler/cpp/generator.cc b/src/google/protobuf/compiler/cpp/generator.cc index 63a2bcebed..f26bb6b97e 100644 --- a/src/google/protobuf/compiler/cpp/generator.cc +++ b/src/google/protobuf/compiler/cpp/generator.cc @@ -135,8 +135,6 @@ bool CppGenerator::Generate(const FileDescriptor* file, .insert(options[i].second.substr(pos, next_pos - pos)); pos = next_pos + 1; } while (pos < options[i].second.size()); - } else if (options[i].first == "verified_lazy") { - file_options.unverified_lazy = false; } else if (options[i].first == "unverified_lazy_message_sets") { file_options.unverified_lazy_message_sets = true; } else if (options[i].first == "message_owned_arena_trial") { diff --git a/src/google/protobuf/compiler/cpp/options.h b/src/google/protobuf/compiler/cpp/options.h index 5d935e9e6e..5c1375ebd1 100644 --- a/src/google/protobuf/compiler/cpp/options.h +++ b/src/google/protobuf/compiler/cpp/options.h @@ -80,7 +80,6 @@ struct Options { bool annotate_accessor = false; bool unused_field_stripping = false; bool unverified_lazy_message_sets = false; - bool unverified_lazy = false; bool profile_driven_inline_string = true; bool message_owned_arena_trial = false; bool force_split = false; diff --git a/src/google/protobuf/generated_message_reflection_unittest.cc b/src/google/protobuf/generated_message_reflection_unittest.cc index 50ce399199..2ef2734162 100644 --- a/src/google/protobuf/generated_message_reflection_unittest.cc +++ b/src/google/protobuf/generated_message_reflection_unittest.cc @@ -1261,8 +1261,6 @@ TEST(GeneratedMessageReflectionTest, UsageErrors) { const Reflection* reflection = message.GetReflection(); const Descriptor* descriptor = message.GetDescriptor(); -#define f(NAME) descriptor->FindFieldByName(NAME) - // Testing every single failure mode would be too much work. Let's just // check a few. EXPECT_DEATH( @@ -1301,8 +1299,6 @@ TEST(GeneratedMessageReflectionTest, UsageErrors) { " Message type: protobuf_unittest.TestAllTypes\n" " Field : protobuf_unittest.ForeignMessage.c\n" " Problem : Field does not match message type."); - -#undef f } #endif // PROTOBUF_HAS_DEATH_TEST diff --git a/src/google/protobuf/generated_message_tctable_impl.h b/src/google/protobuf/generated_message_tctable_impl.h index c64b264dc7..53bdc76da6 100644 --- a/src/google/protobuf/generated_message_tctable_impl.h +++ b/src/google/protobuf/generated_message_tctable_impl.h @@ -187,48 +187,48 @@ static_assert(kFmtShift + kFmtBits <= 16, "too many bits"); // Convenience aliases (16 bits, with format): enum FieldType : uint16_t { // Numeric types: - kBool = kFkVarint | kRep8Bits, - - kFixed32 = kFkFixed | kRep32Bits | kFmtUnsigned, - kUInt32 = kFkVarint | kRep32Bits | kFmtUnsigned, - kSFixed32 = kFkFixed | kRep32Bits | kFmtSigned, - kInt32 = kFkVarint | kRep32Bits | kFmtSigned, - kSInt32 = kFkVarint | kRep32Bits | kFmtSigned | kTvZigZag, - kFloat = kFkFixed | kRep32Bits | kFmtFloating, - kEnum = kFkVarint | kRep32Bits | kFmtEnum | kTvEnum, - kEnumRange = kFkVarint | kRep32Bits | kFmtEnum | kTvRange, - kOpenEnum = kFkVarint | kRep32Bits | kFmtEnum, - - kFixed64 = kFkFixed | kRep64Bits | kFmtUnsigned, - kUInt64 = kFkVarint | kRep64Bits | kFmtUnsigned, - kSFixed64 = kFkFixed | kRep64Bits | kFmtSigned, - kInt64 = kFkVarint | kRep64Bits | kFmtSigned, - kSInt64 = kFkVarint | kRep64Bits | kFmtSigned | kTvZigZag, - kDouble = kFkFixed | kRep64Bits | kFmtFloating, - - kPackedBool = kFkPackedVarint | kRep8Bits, - - kPackedFixed32 = kFkPackedFixed | kRep32Bits | kFmtUnsigned, - kPackedUInt32 = kFkPackedVarint | kRep32Bits | kFmtUnsigned, - kPackedSFixed32 = kFkPackedFixed | kRep32Bits | kFmtSigned, - kPackedInt32 = kFkPackedVarint | kRep32Bits | kFmtSigned, - kPackedSInt32 = kFkPackedVarint | kRep32Bits | kFmtSigned | kTvZigZag, - kPackedFloat = kFkPackedFixed | kRep32Bits | kFmtFloating, - kPackedEnum = kFkPackedVarint | kRep32Bits | kFmtEnum | kTvEnum, - kPackedEnumRange = kFkPackedVarint | kRep32Bits | kFmtEnum | kTvRange, - kPackedOpenEnum = kFkPackedVarint | kRep32Bits | kFmtEnum, - - kPackedFixed64 = kFkPackedFixed | kRep64Bits | kFmtUnsigned, - kPackedUInt64 = kFkPackedVarint | kRep64Bits | kFmtUnsigned, - kPackedSFixed64 = kFkPackedFixed | kRep64Bits | kFmtSigned, - kPackedInt64 = kFkPackedVarint | kRep64Bits | kFmtSigned, - kPackedSInt64 = kFkPackedVarint | kRep64Bits | kFmtSigned | kTvZigZag, - kPackedDouble = kFkPackedFixed | kRep64Bits | kFmtFloating, + kBool = 0 | kFkVarint | kRep8Bits, + + kFixed32 = 0 | kFkFixed | kRep32Bits | kFmtUnsigned, + kUInt32 = 0 | kFkVarint | kRep32Bits | kFmtUnsigned, + kSFixed32 = 0 | kFkFixed | kRep32Bits | kFmtSigned, + kInt32 = 0 | kFkVarint | kRep32Bits | kFmtSigned, + kSInt32 = 0 | kFkVarint | kRep32Bits | kFmtSigned | kTvZigZag, + kFloat = 0 | kFkFixed | kRep32Bits | kFmtFloating, + kEnum = 0 | kFkVarint | kRep32Bits | kFmtEnum | kTvEnum, + kEnumRange = 0 | kFkVarint | kRep32Bits | kFmtEnum | kTvRange, + kOpenEnum = 0 | kFkVarint | kRep32Bits | kFmtEnum, + + kFixed64 = 0 | kFkFixed | kRep64Bits | kFmtUnsigned, + kUInt64 = 0 | kFkVarint | kRep64Bits | kFmtUnsigned, + kSFixed64 = 0 | kFkFixed | kRep64Bits | kFmtSigned, + kInt64 = 0 | kFkVarint | kRep64Bits | kFmtSigned, + kSInt64 = 0 | kFkVarint | kRep64Bits | kFmtSigned | kTvZigZag, + kDouble = 0 | kFkFixed | kRep64Bits | kFmtFloating, + + kPackedBool = 0 | kFkPackedVarint | kRep8Bits, + + kPackedFixed32 = 0 | kFkPackedFixed | kRep32Bits | kFmtUnsigned, + kPackedUInt32 = 0 | kFkPackedVarint | kRep32Bits | kFmtUnsigned, + kPackedSFixed32 = 0 | kFkPackedFixed | kRep32Bits | kFmtSigned, + kPackedInt32 = 0 | kFkPackedVarint | kRep32Bits | kFmtSigned, + kPackedSInt32 = 0 | kFkPackedVarint | kRep32Bits | kFmtSigned | kTvZigZag, + kPackedFloat = 0 | kFkPackedFixed | kRep32Bits | kFmtFloating, + kPackedEnum = 0 | kFkPackedVarint | kRep32Bits | kFmtEnum | kTvEnum, + kPackedEnumRange = 0 | kFkPackedVarint | kRep32Bits | kFmtEnum | kTvRange, + kPackedOpenEnum = 0 | kFkPackedVarint | kRep32Bits | kFmtEnum, + + kPackedFixed64 = 0 | kFkPackedFixed | kRep64Bits | kFmtUnsigned, + kPackedUInt64 = 0 | kFkPackedVarint | kRep64Bits | kFmtUnsigned, + kPackedSFixed64 = 0 | kFkPackedFixed | kRep64Bits | kFmtSigned, + kPackedInt64 = 0 | kFkPackedVarint | kRep64Bits | kFmtSigned, + kPackedSInt64 = 0 | kFkPackedVarint | kRep64Bits | kFmtSigned | kTvZigZag, + kPackedDouble = 0 | kFkPackedFixed | kRep64Bits | kFmtFloating, // String types: - kBytes = kFkString | kFmtArray, - kRawString = kFkString | kFmtUtf8 | kTvUtf8Debug, - kUtf8String = kFkString | kFmtUtf8 | kTvUtf8, + kBytes = 0 | kFkString | kFmtArray, + kRawString = 0 | kFkString | kFmtUtf8 | kTvUtf8Debug, + kUtf8String = 0 | kFkString | kFmtUtf8 | kTvUtf8, // Message types: kMessage = kFkMessage, @@ -236,7 +236,6 @@ enum FieldType : uint16_t { // Map types: kMap = kFkMap, }; - // clang-format on } // namespace field_layout diff --git a/src/google/protobuf/map.h b/src/google/protobuf/map.h index 008c192253..a0e1b610a8 100644 --- a/src/google/protobuf/map.h +++ b/src/google/protobuf/map.h @@ -1364,7 +1364,8 @@ class Map { template void insert(InputIt first, InputIt last) { for (; first != last; ++first) { - try_emplace(first->first, first->second); + auto&& pair = *first; + try_emplace(pair.first, pair.second); } } void insert(std::initializer_list values) { diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index 3fb734e5cb..730b06708c 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -246,7 +246,7 @@ class RepeatedField final { Element* mutable_data(); const Element* data() const; - // Swaps entire contents with "other". If they are separate arenas then, + // Swaps entire contents with "other". If they are separate arenas, then // copies data between each other. void Swap(RepeatedField* other); @@ -313,8 +313,14 @@ class RepeatedField final { iterator erase(const_iterator first, const_iterator last); // Gets the Arena on which this RepeatedField stores its elements. + // Message-owned arenas are not exposed by this method, which will return + // nullptr for messages owned by MOAs. inline Arena* GetArena() const { - return GetOwningArena(); + Arena* arena = GetOwningArena(); + if (arena == nullptr || arena->InternalIsMessageOwnedArena()) { + return nullptr; + } + return arena; } // For internal use only. diff --git a/src/google/protobuf/repeated_ptr_field.h b/src/google/protobuf/repeated_ptr_field.h index 401230bf51..c0822e083b 100644 --- a/src/google/protobuf/repeated_ptr_field.h +++ b/src/google/protobuf/repeated_ptr_field.h @@ -612,7 +612,16 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { temp.Destroy(); // Frees rep_ if `other` had no arena. } - inline Arena* GetArena() const { return arena_; } + // Gets the Arena on which this RepeatedPtrField stores its elements. + // Message-owned arenas are not exposed by this method, which will return + // nullptr for messages owned by MOAs. + inline Arena* GetArena() const { + Arena* arena = GetOwningArena(); + if (arena == nullptr || arena->InternalIsMessageOwnedArena()) { + return nullptr; + } + return arena; + } protected: inline Arena* GetOwningArena() const { return arena_; } @@ -762,7 +771,7 @@ class GenericTypeHandler { } } static inline Arena* GetOwningArena(GenericType* value) { - return Arena::GetOwningArena(value); + return Arena::InternalGetOwningArena(value); } static inline void Clear(GenericType* value) { value->Clear(); } diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc index e0a807ffbb..f1a815022d 100644 --- a/src/google/protobuf/stubs/common.cc +++ b/src/google/protobuf/stubs/common.cc @@ -178,7 +178,7 @@ void NullLogHandler(LogLevel /* level */, const char* /* filename */, } static LogHandler* log_handler_ = &DefaultLogHandler; -static std::atomic log_silencer_count_ = ATOMIC_VAR_INIT(0); +static std::atomic log_silencer_count_{0}; LogMessage& LogMessage::operator<<(const std::string& value) { message_ += value; diff --git a/src/google/protobuf/util/internal/protostream_objectsource.h b/src/google/protobuf/util/internal/protostream_objectsource.h index 8ed2ca9bb4..a134d900f2 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.h +++ b/src/google/protobuf/util/internal/protostream_objectsource.h @@ -77,9 +77,6 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { public: struct RenderOptions { - RenderOptions() = default; - RenderOptions(const RenderOptions&) = default; - // Sets whether or not to use lowerCamelCase casing for enum values. If set // to false, enum values are output without any case conversions. //