Skip to content

Commit

Permalink
capnp: strip NUL byte from TextList.BytesAt
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen committed Jun 16, 2016
1 parent 4c2c5a6 commit cca51f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion list.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ func (l TextList) BytesAt(i int) ([]byte, error) {
if err != nil {
return nil, err
}
return p.Data(), nil
b := p.Data()
if len(b) == 0 {
return b, nil
}
return b[:len(b)-1 : len(b)], nil
}

// Set sets the i'th string in the list to v.
Expand Down
27 changes: 27 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package capnp

import (
"bytes"
"testing"
)

Expand Down Expand Up @@ -52,6 +53,32 @@ func TestToListDefault(t *testing.T) {
}
}

func TestTextListBytesAt(t *testing.T) {
msg := &Message{Arena: SingleSegment([]byte{
0, 0, 0, 0, 0, 0, 0, 0,
0x01, 0, 0, 0, 0x22, 0, 0, 0,
'f', 'o', 'o', 0, 0, 0, 0, 0,
})}
seg, err := msg.Segment(0)
if err != nil {
t.Fatal(err)
}
list := TextList{List{
seg: seg,
off: 8,
length: 1,
size: ObjectSize{DataSize: 1},
depthLimit: maxDepth,
}}
b, err := list.BytesAt(0)
if err != nil {
t.Errorf("list.BytesAt(0) error: %v", err)
}
if !bytes.Equal(b, []byte("foo")) {
t.Errorf("list.BytesAt(0) = %q; want \"foo\"", b)
}
}

func TestListValue(t *testing.T) {
_, seg, err := NewMessage(SingleSegment(nil))
if err != nil {
Expand Down

0 comments on commit cca51f0

Please sign in to comment.