From ee419f22e0eec21243540d4b7ffe40bd194ed293 Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Thu, 5 Sep 2024 17:17:57 -0700 Subject: [PATCH] Bugfix: Make extensions beyond n=16 immutable. Previously, we only make extensions immutable if they were in the FieldSet's array, maximum size 16. The overflow entries in the TreeMap weren't made immutable. PiperOrigin-RevId: 671564444 --- .../src/main/java/com/google/protobuf/FieldSet.java | 11 +++++++++-- .../test/java/com/google/protobuf/ParserLiteTest.java | 5 ++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/FieldSet.java b/java/core/src/main/java/com/google/protobuf/FieldSet.java index debad4e971bb..c81a1d1fcfd5 100644 --- a/java/core/src/main/java/com/google/protobuf/FieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/FieldSet.java @@ -101,8 +101,15 @@ public void makeImmutable() { int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop. for (int i = 0; i < n; ++i) { Entry entry = fields.getArrayEntryAt(i); - if (entry.getValue() instanceof GeneratedMessageLite) { - ((GeneratedMessageLite) entry.getValue()).makeImmutable(); + Object value = entry.getValue(); + if (value instanceof GeneratedMessageLite) { + ((GeneratedMessageLite) value).makeImmutable(); + } + } + for (Map.Entry entry : fields.getOverflowEntries()) { + Object value = entry.getValue(); + if (value instanceof GeneratedMessageLite) { + ((GeneratedMessageLite) value).makeImmutable(); } } fields.makeImmutable(); diff --git a/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java b/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java index 5433bb4c349e..c903e95246d4 100644 --- a/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java @@ -76,15 +76,14 @@ public void testParseExtensionsLite() throws Exception { } @Test - public void testParseExtensionsLite_extensionIsMutable() throws Exception { + public void testParseExtensionsLite_extensionIsImmutable() throws Exception { TestAllExtensionsLite message = TestAllExtensionsLite.parseFrom( TestUtilLite.getAllLiteExtensionsSet().toByteArray(), TestUtilLite.getExtensionRegistryLite()); Object nested = message.getExtension(UnittestLite.optionalNestedMessageExtensionLite); if (nested instanceof GeneratedMessageLite) { - // TODO: Make this false. - assertThat(((GeneratedMessageLite) nested).isMutable()).isTrue(); + assertThat(((GeneratedMessageLite) nested).isMutable()).isFalse(); } }