Commit a6d3cc29 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

encoding/base64: don't lose a byte of output when encountering trailing garbage

Fixes #7733

LGTM=minux.ma
R=golang-codereviews, minux.ma
CC=golang-codereviews, nigeltao, r, rsc
https://golang.org/cl/88330044
parent 6ddd995a
......@@ -250,7 +250,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
}
if len(src) > 0 {
// trailing garbage
return n, false, CorruptInputError(olen - len(src))
err = CorruptInputError(olen - len(src))
}
dlen, end = j, true
break
......@@ -277,7 +277,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
n += dlen - 1
}
return n, end, nil
return n, end, err
}
// Decode decodes src using the encoding enc. It writes at most
......
......@@ -9,6 +9,7 @@ import (
"errors"
"io"
"io/ioutil"
"reflect"
"strings"
"testing"
"time"
......@@ -165,6 +166,7 @@ func TestDecodeCorrupt(t *testing.T) {
{"AAA=", -1},
{"AAAA", -1},
{"AAAAAA=", 7},
{"YWJjZA=====", 8},
}
for _, tc := range testCases {
dbuf := make([]byte, StdEncoding.DecodedLen(len(tc.input)))
......@@ -329,3 +331,14 @@ bqbPb06551Y4
t.Error("Decoded results not equal")
}
}
func TestDecoderIssue7733(t *testing.T) {
s, err := StdEncoding.DecodeString("YWJjZA=====")
want := CorruptInputError(8)
if !reflect.DeepEqual(want, err) {
t.Errorf("Error = %v; want CorruptInputError(8)")
}
if string(s) != "abcd" {
t.Errorf("DecodeString = %q; want abcd", s)
}
}
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