diff options
| author | Benjamin Prosnitz <bprosnitz@gmail.com> | 2023-01-13 11:54:35 -0500 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-01-27 15:28:30 +0000 |
| commit | a106defddac515db4d70e1cad162b88dd026deee (patch) | |
| tree | f4eddff3e58e4e17b9eff8686125b7aca0c05b06 /src/encoding/hex/hex_test.go | |
| parent | 6b8b7823c7fd9f3f2317f657120dc2e965d97b77 (diff) | |
| download | go-a106defddac515db4d70e1cad162b88dd026deee.tar.xz | |
hex: fix panic in Decode when len(src) > 2*len(dst)
hex.Decode never checks the length of dst and triggers a panic
if there are insufficient bytes in the slice.
There isn't document on what the behavior *should* be in this case.
Two possibilities:
1. Error dst has insufficient space (as done in this change)
2. Reduce the length of the decode to min(dst, src)
Option 1 was chosen because it seems the least surprising or
subtle.
Change-Id: I3bf029e3d928202de716830434285e3c165f26dd
Reviewed-on: https://go-review.googlesource.com/c/go/+/461958
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Benjamin Prosnitz <bprosnitz@gmail.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/encoding/hex/hex_test.go')
| -rw-r--r-- | src/encoding/hex/hex_test.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/encoding/hex/hex_test.go b/src/encoding/hex/hex_test.go index a820fe7a15..1eb169cdee 100644 --- a/src/encoding/hex/hex_test.go +++ b/src/encoding/hex/hex_test.go @@ -55,6 +55,15 @@ func TestDecode(t *testing.T) { } } +func TestDecode_tooFewDstBytes(t *testing.T) { + dst := make([]byte, 1) + src := []byte{'0', '1', '2', '3'} + _, err := Decode(dst, src) + if err == nil { + t.Errorf("expected Decode to return an error, but it returned none") + } +} + func TestEncodeToString(t *testing.T) { for i, test := range encDecTests { s := EncodeToString(test.dec) |
