// If the input is malformed, Decode returns the number
// of bytes decoded before the error.
func Decode(dst, src []byte) (int, error) {
- if len(dst) < DecodedLen(len(src)) {
- return 0, errors.New("encoding/hex: output buffer too small")
- }
i, j := 0, 1
for ; j < len(src); j += 2 {
p := src[j-1]
if b > 0x0f {
return i, InvalidByteError(q)
}
+ if i >= len(dst) {
+ return i, errors.New("encoding/hex: output buffer too small")
+ }
dst[i] = (a << 4) | b
i++
}
}
}
-func TestDecode_tooFewDstBytes(t *testing.T) {
+func TestDecodeDstTooSmall(t *testing.T) {
dst := make([]byte, 1)
src := []byte{'0', '1', '2', '3'}
- _, err := Decode(dst, src)
+ n, err := Decode(dst, src)
if err == nil {
t.Errorf("expected Decode to return an error, but it returned none")
}
+ if !bytes.Equal(dst[:n], []byte{0x01}) {
+ t.Errorf("output mismatch: got %x, want 01", dst[:n])
+ }
}
func TestEncodeToString(t *testing.T) {