Browse Source

copy content of frame when expanding

Song Gao 9 years ago
parent
commit
a8b0271b56
2 changed files with 11 additions and 3 deletions
  1. 4 1
      ethernet/frame.go
  2. 7 2
      ethernet/frame_test.go

+ 4 - 1
ethernet/frame.go

@@ -72,7 +72,8 @@ func (f Frame) Payload() []byte {
 }
 }
 
 
 // Resize re-slices (*f) so that len(*f) holds exactly payloadSize bytes of
 // 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.
 // 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) {
 func (f *Frame) resize(length int) {
 	if cap(*f) < length {
 	if cap(*f) < length {
+		old := *f
 		*f = make(Frame, length, length)
 		*f = make(Frame, length, length)
+		copy(*f, old)
 	} else {
 	} else {
 		*f = (*f)[:length]
 		*f = (*f)[:length]
 	}
 	}

+ 7 - 2
ethernet/frame_test.go

@@ -53,9 +53,14 @@ func TestPrepare(t *testing.T) {
 
 
 func TestResize(t *testing.T) {
 func TestResize(t *testing.T) {
 	var frame Frame
 	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 {
 	if len(frame) != expectedLength {
 		t.Fatalf("frame does not have correct length. expected %d; got %d\n", expectedLength, len(frame))
 		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")
+	}
 }
 }