aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/binary/binary_test.go
diff options
context:
space:
mode:
authorAlexandru Moșoi <mosoi@google.com>2016-03-21 15:05:54 +0100
committerAlexandru Moșoi <alexandru@mosoi.ro>2016-03-21 19:22:22 +0000
commit478b594d5117729694deecbcb205bb15b6085f7a (patch)
tree334b4985c5311ae01bf94e635573c8699dd28d0d /src/encoding/binary/binary_test.go
parent6a33f7765f79cf2f00f5ca55832d2cfab8beb289 (diff)
downloadgo-478b594d5117729694deecbcb205bb15b6085f7a.tar.xz
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 <khr@golang.org> Reviewed-by: Minux Ma <minux@golang.org>
Diffstat (limited to 'src/encoding/binary/binary_test.go')
-rw-r--r--src/encoding/binary/binary_test.go27
1 files changed, 27 insertions, 0 deletions
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
}