Skip to content

Commit

Permalink
Fix data race in crosslink.
Browse files Browse the repository at this point in the history
This was introduced by the previous fix for delimited inheritance, and was never released.  This fix removes all getType() calls from crosslink, where it's not safe to inspect the message type, which is still a placeholder, until after crosslinking.  Using the inferred type is not necessary since we treat messages and groups the same during crosslink.

PiperOrigin-RevId: 643394981
  • Loading branch information
mkruskal-google committed Jun 14, 2024
1 parent 29f1b52 commit 3d71e22
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions java/core/src/main/java/com/google/protobuf/Descriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,9 @@ private void crossLink() throws DescriptorValidationException {
}
}

if (getJavaType() == JavaType.MESSAGE) {
// Use raw type since inferred type considers messageType which may not be fully cross
// linked yet.
if (type.getJavaType() == JavaType.MESSAGE) {
if (!(typeDescriptor instanceof Descriptor)) {
throw new DescriptorValidationException(
this, '\"' + proto.getTypeName() + "\" is not a message type.");
Expand All @@ -1911,7 +1913,7 @@ private void crossLink() throws DescriptorValidationException {
if (proto.hasDefaultValue()) {
throw new DescriptorValidationException(this, "Messages can't have default values.");
}
} else if (getJavaType() == JavaType.ENUM) {
} else if (type.getJavaType() == JavaType.ENUM) {
if (!(typeDescriptor instanceof EnumDescriptor)) {
throw new DescriptorValidationException(
this, '\"' + proto.getTypeName() + "\" is not an enum type.");
Expand All @@ -1921,7 +1923,7 @@ private void crossLink() throws DescriptorValidationException {
throw new DescriptorValidationException(this, "Field with primitive type has type_name.");
}
} else {
if (getJavaType() == JavaType.MESSAGE || getJavaType() == JavaType.ENUM) {
if (type.getJavaType() == JavaType.MESSAGE || type.getJavaType() == JavaType.ENUM) {
throw new DescriptorValidationException(
this, "Field with message or enum type missing type_name.");
}
Expand All @@ -1942,7 +1944,7 @@ private void crossLink() throws DescriptorValidationException {
}

try {
switch (getType()) {
switch (type) {
case INT32:
case SINT32:
case SFIXED32:
Expand Down Expand Up @@ -2017,7 +2019,7 @@ private void crossLink() throws DescriptorValidationException {
if (isRepeated()) {
defaultValue = Collections.emptyList();
} else {
switch (getJavaType()) {
switch (type.getJavaType()) {
case ENUM:
// We guarantee elsewhere that an enum type always has at least
// one possible value.
Expand All @@ -2027,7 +2029,7 @@ private void crossLink() throws DescriptorValidationException {
defaultValue = null;
break;
default:
defaultValue = getJavaType().defaultDefault;
defaultValue = type.getJavaType().defaultDefault;
break;
}
}
Expand Down

0 comments on commit 3d71e22

Please sign in to comment.