copy content of frame when expanding

This commit is contained in:
Song Gao
2016-04-04 13:23:30 -05:00
parent 6b909109c3
commit a8b0271b56
2 changed files with 11 additions and 3 deletions
+4 -1
View File
@@ -72,7 +72,8 @@ func (f Frame) Payload() []byte {
}
// Resize re-slices (*f) so that len(*f) holds exactly payloadSize bytes of
// payload. If cap(*f) is not large enough, a new slice is made.
// payload. If cap(*f) is not large enough, a new slice is made and content
// from old slice is copied to the new one.
//
// If len(*f) is less than 14 bytes, it is assumed to be not tagged.
//
@@ -107,7 +108,9 @@ func (f *Frame) Prepare(dst net.HardwareAddr, src net.HardwareAddr, tagging Tagg
func (f *Frame) resize(length int) {
if cap(*f) < length {
old := *f
*f = make(Frame, length, length)
copy(*f, old)
} else {
*f = (*f)[:length]
}
+7 -2
View File
@@ -53,9 +53,14 @@ func TestPrepare(t *testing.T) {
func TestResize(t *testing.T) {
var frame Frame
(&frame).Resize(1024)
expectedLength := 6 + 6 + int(NotTagged) + 2 + 1024
(&frame).Resize(8)
expectedLength := 6 + 6 + int(NotTagged) + 2 + 8
if len(frame) != expectedLength {
t.Fatalf("frame does not have correct length. expected %d; got %d\n", expectedLength, len(frame))
}
frame.Payload()[0] = 42
(&frame).Resize(1024)
if frame.Payload()[0] != 42 {
t.Fatalf("expanded frame does not have same content\n")
}
}