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

Unmarshaling empty messages is incompatible with proto.Unmarshal #60

Open
jzelinskie opened this issue Sep 17, 2022 · 0 comments
Open

Comments

@jzelinskie
Copy link

When unmarshaling an empty message embedded in another message, vtprotobuf is allocating the message, but proto.Unmarshal is using a typed nil.

Steps to reproduce:

syntax = "proto3";
package repro;

message TopLevel {
  message Empty {}
  Empty embedded = 1;
}
func TestEmbeddedEmpty(t *testing.T) {
	m := &pb.TopLevel{Embedded: &pb.TopLevel_Empty{}}

	// Marshal it with protobuf.
	protobuf, err := proto.Marshal(m)
	if err != nil {
		t.Fatal(err)
	}

	// Unmarshal it with protobuf.
	pbm := &pb.TopLevel{}
	if err := proto.Unmarshal(protobuf, m); err != nil {
		t.Fatal(err)
	}

	// Unmarshal it with vtprotobuf.
	vtm := &pb.TopLevel{}
	if err := vtm.UnmarshalVT(protobuf); err != nil {
		t.Fatal(err)
	}

	fmt.Printf("%#v\n\n", pbm)
	fmt.Printf("%#v\n", vtm)

	require.True(t, pbm.EqualVT(vtm), "EqualVT")
	require.True(t, proto.Equal(pbm, vtm), "proto.Equal")
}

Output:

&proto.TopLevel{state:impl.MessageState{NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, DoNotCompare:pragma.DoNotCompare{}, DoNotCopy:pragma.DoNotCopy{}, atomicMessageInfo:(*impl.MessageInfo)(nil)}, sizeCache:0, unknownFields:[]uint8(nil), Embedded:(*proto.TopLevel_Empty)(nil)}

&proto.TopLevel{state:impl.MessageState{NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, DoNotCompare:pragma.DoNotCompare{}, DoNotCopy:pragma.DoNotCopy{}, atomicMessageInfo:(*impl.MessageInfo)(nil)}, sizeCache:0, unknownFields:[]uint8(nil), Embedded:(*proto.TopLevel_Empty)(0xc00011fbc0)}

--- FAIL: TestEmbeddedEmpty (0.00s)
    main_test.go:37: 
        	Error Trace:	main_test.go:37
        	Error:      	Should be true
        	Test:       	TestEmbeddedEmpty
        	Messages:   	EqualVT
FAIL
exit status 1
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

1 participant