Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicMessage violates contract of hashCode() #19080

Open
adeloof2 opened this issue Oct 31, 2024 · 1 comment
Open

DynamicMessage violates contract of hashCode() #19080

adeloof2 opened this issue Oct 31, 2024 · 1 comment

Comments

@adeloof2
Copy link

What version of protobuf and what language are you using?
Version: protoc 28.3 / protobuf-java 4.28.3
Language: Java

What operating system (Linux, Windows, ...) and version?
Windows 11

What runtime / compiler are you using (e.g., python version or gcc version)
BellSoft Liberica 21.0.2

What did you do?

syntax = "proto3";
option java_multiple_files = true;

message ExampleMessage {
  uint32 i = 1;
}
ExampleMessage message = ExampleMessage.newBuilder().build();
DynamicMessage dynamicMessage = DynamicMessage.parseFrom(ExampleMessage.getDescriptor(), message.toByteArray());
System.out.println("objects equal:   " + message.equals(dynamicMessage));
System.out.println("hashcodes equal: " + (message.hashCode() == dynamicMessage.hashCode()));

Output:

objects equal:   true
hashcodes equal: false

What did you expect to see
The DynamicMessage's hashcode should be the same as the real message's hashcode since equals() returns true.

What did you see instead?
The hashcodes are not the same. This issue only seems to happen if the message has a field which is not set.

Anything else we should know about your project / environment
n/a

@adeloof2 adeloof2 added the untriaged auto added to all issues by default when created. label Oct 31, 2024
@esrauchg
Copy link

esrauchg commented Nov 6, 2024

Thanks for the clear bug report!

We are able to internally confirm that the hashCode() accidentally mismatches on dynamicmessage vs gencode if you have zero-valued implicit presence fields (so, the issue won't reproduce on: proto2 syntax files, proto3 files where the scalar fields are marked 'optional', or where all of non-optional fields are set to a value other than zero).

We'll look into the best / safest way to fix this in a future release.

@esrauchg esrauchg removed the untriaged auto added to all issues by default when created. label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants