Skip to content

Commit

Permalink
internal/encoding/messageset: don't modify input data when unmarshaling
Browse files Browse the repository at this point in the history
When combining multiple message fields in a MessageSet item (a case
which should never happen in practice), unmarshal could modify the input
data. Fix it to not do so. Add a general check to ensure that unmarshal
operations don't modify the input.

Change-Id: Idde46e6132a1dc96c374f9146efff81783c3bef3
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/223818
Reviewed-by: Joe Tsai <joetsai@google.com>
  • Loading branch information
neild committed Mar 18, 2020
1 parent 29677a9 commit d387405
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions internal/encoding/messageset/messageset.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ func ConsumeFieldValue(b []byte, wantLen bool) (typeid wire.Number, message []by
}
if message == nil {
if wantLen {
message = b[:n]
message = b[:n:n]
} else {
message = m
message = m[:len(m):len(m)]
}
} else {
// This case should never happen in practice, but handle it for
Expand All @@ -174,7 +174,7 @@ func ConsumeFieldValue(b []byte, wantLen bool) (typeid wire.Number, message []by
if wantLen {
_, nn := wire.ConsumeVarint(message)
m0 := message[nn:]
message = message[:0]
message = nil
message = wire.AppendVarint(message, uint64(len(m0)+len(m)))
message = append(message, m0...)
message = append(message, m...)
Expand Down
9 changes: 7 additions & 2 deletions proto/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package proto_test

import (
"bytes"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -34,8 +35,12 @@ func TestDecode(t *testing.T) {
return
}

// Aliasing check: Modifying the original wire bytes shouldn't
// affect the unmarshaled message.
// Aliasing check: Unmarshal shouldn't modify the original wire
// bytes, and modifying the original wire bytes shouldn't affect
// the unmarshaled message.
if !bytes.Equal(test.wire, wire) {
t.Errorf("Unmarshal unexpectedly modified its input")
}
for i := range wire {
wire[i] = 0
}
Expand Down

0 comments on commit d387405

Please sign in to comment.