Commit a125bdb4 authored by Russ Cox's avatar Russ Cox

encoding/base64: do not slice past output unnecessarily

Base64-encoding 32 bytes results in a 44-byte string.
While in general a 44-byte string might decode to 33 bytes,
if you take a 44-byte string that actually only encodes 32 bytes,
and you try to decode it into 32 bytes, that should succeed.
Instead it fails trying to do a useless dst[33:] slice operation.
Delete that slice operation.

Noticed while preparing CL 156322.

Change-Id: I8024bf28a65e2638675b980732b2ff91c66c62cf
Reviewed-on: https://go-review.googlesource.com/c/go/+/164628Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 2f8d2427
...@@ -282,7 +282,7 @@ func (e CorruptInputError) Error() string { ...@@ -282,7 +282,7 @@ func (e CorruptInputError) Error() string {
func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err error) { func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err error) {
// Decode quantum using the base64 alphabet // Decode quantum using the base64 alphabet
var dbuf [4]byte var dbuf [4]byte
dinc, dlen := 3, 4 dlen := 4
for j := 0; j < len(dbuf); j++ { for j := 0; j < len(dbuf); j++ {
if len(src) == si { if len(src) == si {
...@@ -292,7 +292,7 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err ...@@ -292,7 +292,7 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err
case j == 1, enc.padChar != NoPadding: case j == 1, enc.padChar != NoPadding:
return si, 0, CorruptInputError(si - j) return si, 0, CorruptInputError(si - j)
} }
dinc, dlen = j-1, j dlen = j
break break
} }
in := src[si] in := src[si]
...@@ -344,7 +344,7 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err ...@@ -344,7 +344,7 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err
// trailing garbage // trailing garbage
err = CorruptInputError(si) err = CorruptInputError(si)
} }
dinc, dlen = 3, j dlen = j
break break
} }
...@@ -369,7 +369,6 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err ...@@ -369,7 +369,6 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err
return si, 0, CorruptInputError(si - 2) return si, 0, CorruptInputError(si - 2)
} }
} }
dst = dst[dinc:]
return si, dlen - 1, err return si, dlen - 1, err
} }
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"reflect" "reflect"
"runtime/debug"
"strings" "strings"
"testing" "testing"
"time" "time"
...@@ -247,6 +248,20 @@ func TestDecodeCorrupt(t *testing.T) { ...@@ -247,6 +248,20 @@ func TestDecodeCorrupt(t *testing.T) {
} }
} }
func TestDecodeBounds(t *testing.T) {
var buf [32]byte
s := StdEncoding.EncodeToString(buf[:])
defer func() {
if err := recover(); err != nil {
t.Fatalf("Decode panicked unexpectedly: %v\n%s", err, debug.Stack())
}
}()
n, err := StdEncoding.Decode(buf[:], []byte(s))
if n != len(buf) || err != nil {
t.Fatalf("StdEncoding.Decode = %d, %v, want %d, nil", n, err, len(buf))
}
}
func TestEncodedLen(t *testing.T) { func TestEncodedLen(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
enc *Encoding enc *Encoding
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment