From 478b594d5117729694deecbcb205bb15b6085f7a Mon Sep 17 00:00:00 2001 From: Alexandru Moșoi Date: Mon, 21 Mar 2016 15:05:54 +0100 Subject: encoding/binary: fix bound check The inserted early bound checks cause the slice to expand beyond the original length of the slice. Change-Id: Ib38891605f4a9a12d3b9e2071a5f77640b083d2d Reviewed-on: https://go-review.googlesource.com/20981 Reviewed-by: Keith Randall Reviewed-by: Minux Ma --- src/encoding/binary/binary_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/encoding/binary/binary_test.go') diff --git a/src/encoding/binary/binary_test.go b/src/encoding/binary/binary_test.go index fe75a00b33..c0728e943e 100644 --- a/src/encoding/binary/binary_test.go +++ b/src/encoding/binary/binary_test.go @@ -339,6 +339,33 @@ func TestReadTruncated(t *testing.T) { } } +func testUint64SmallSliceLengthPanics() (panicked bool) { + defer func() { + panicked = recover() != nil + }() + b := [8]byte{1, 2, 3, 4, 5, 6, 7, 8} + LittleEndian.Uint64(b[:4]) + return false +} + +func testPutUint64SmallSliceLengthPanics() (panicked bool) { + defer func() { + panicked = recover() != nil + }() + b := [8]byte{} + LittleEndian.PutUint64(b[:4], 0x0102030405060708) + return false +} + +func TestEarlyBoundsChecks(t *testing.T) { + if testUint64SmallSliceLengthPanics() != true { + t.Errorf("binary.LittleEndian.Uint64 expected to panic for small slices, but didn't") + } + if testPutUint64SmallSliceLengthPanics() != true { + t.Errorf("binary.LittleEndian.PutUint64 expected to panic for small slices, but didn't") + } +} + type byteSliceReader struct { remain []byte } -- cgit v1.3