Skip to content

Commit 5f14bde

Browse files
committed
proto: fix and cleanup test for deterministic marshal with custom marshalers
PR #650 added a check to error out when a custom marshaler was called and deterministic marshaling was also specified. That change performed the check in a relatively obscure location that did not catch all code paths. Since determinism can only be enabled on the Buffer type, we check it up front in Buffer.Marshal. Also, change the test to avoid code injection into generated sources.
1 parent 14aad3d commit 5f14bde

File tree

5 files changed

+370
-411
lines changed

5 files changed

+370
-411
lines changed

proto/all_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,14 +2303,22 @@ func TestInvalidUTF8(t *testing.T) {
23032303
}
23042304
}
23052305

2306+
type CustomRawMessage []byte
2307+
2308+
func (m *CustomRawMessage) Marshal() ([]byte, error) {
2309+
return []byte(*m), nil
2310+
}
2311+
func (m *CustomRawMessage) Reset() { *m = nil }
2312+
func (m *CustomRawMessage) String() string { return fmt.Sprintf("%x", *m) }
2313+
func (m *CustomRawMessage) ProtoMessage() {}
2314+
23062315
func TestDeterministicErrorOnCustomMarshaler(t *testing.T) {
2307-
u := uint64(0)
2308-
in := &CustomDeterministicMarshaler{Field1: &u}
2316+
in := CustomRawMessage{1, 2, 3}
23092317
var b1 Buffer
23102318
b1.SetDeterministic(true)
2311-
err := b1.Marshal(in)
2312-
if !strings.Contains(err.Error(), "deterministic") {
2313-
t.Fatalf("Expected: %s but got %s", "proto: deterministic not supported by the Marshal method of test_proto.CustomDeterministicMarshaler", err.Error())
2319+
err := b1.Marshal(&in)
2320+
if err == nil || !strings.Contains(err.Error(), "deterministic") {
2321+
t.Fatalf("Marshal error:\ngot %v\nwant deterministic not supported error", err)
23142322
}
23152323
}
23162324

proto/table_marshal.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,6 @@ func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte
225225
// If the message can marshal itself, let it do it, for compatibility.
226226
// NOTE: This is not efficient.
227227
if u.hasmarshaler {
228-
if deterministic {
229-
return nil, errors.New("proto: deterministic not supported by the Marshal method of " + u.typ.String())
230-
}
231228
m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
232229
b1, err := m.Marshal()
233230
b = append(b, b1...)
@@ -2718,6 +2715,11 @@ func Marshal(pb Message) ([]byte, error) {
27182715
// a Buffer for most applications.
27192716
func (p *Buffer) Marshal(pb Message) error {
27202717
var err error
2718+
if p.deterministic {
2719+
if _, ok := pb.(Marshaler); ok {
2720+
return fmt.Errorf("proto: deterministic not supported by the Marshal method of %T", pb)
2721+
}
2722+
}
27212723
if m, ok := pb.(newMarshaler); ok {
27222724
siz := m.XXX_Size()
27232725
p.grow(siz) // make sure buf has enough capacity

proto/test_proto/deterministic.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)