mirror of
https://github.com/songgao/packets.git
synced 2026-04-22 15:37:06 +08:00
copy content of frame when expanding
This commit is contained in:
+4
-1
@@ -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]
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user