Skip to content

Commit

Permalink
Bugfix: Make extensions beyond n=16 immutable.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mhansen authored and copybara-github committed Sep 6, 2024
1 parent 3d4d514 commit ee419f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 9 additions & 2 deletions java/core/src/main/java/com/google/protobuf/FieldSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, Object> 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<T, Object> entry : fields.getOverflowEntries()) {
Object value = entry.getValue();
if (value instanceof GeneratedMessageLite) {
((GeneratedMessageLite<?, ?>) value).makeImmutable();
}
}
fields.makeImmutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit ee419f2

Please sign in to comment.