]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base64: don't lose a byte of output when encountering trailing garbage
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 16 Apr 2014 18:32:41 +0000 (11:32 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 16 Apr 2014 18:32:41 +0000 (11:32 -0700)
Fixes #7733

LGTM=minux.ma
R=golang-codereviews, minux.ma
CC=golang-codereviews, nigeltao, r, rsc
https://golang.org/cl/88330044

src/pkg/encoding/base64/base64.go
src/pkg/encoding/base64/base64_test.go

index a6efd44615753dd08128899c75af27cb367ceca0..e38c26d0ec73cc508306f8e200de26173b9e849f 100644 (file)
@@ -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
index 0285629029e69378a07dc42bfb08aa2a651b0308..f1469c684207dbc737d4878daaa5280ec9739a1e 100644 (file)
@@ -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)
+       }
+}