]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base64: fix decode reports incorrect index
authorJosselin Costanzi <josselin@costanzi.fr>
Sun, 5 Mar 2017 17:04:30 +0000 (18:04 +0100)
committerIan Lance Taylor <iant@golang.org>
Mon, 6 Mar 2017 19:28:03 +0000 (19:28 +0000)
Fix Decode to return the correct illegal data index from a corrupted
input that contains whitespaces.

Fixes #19406

Change-Id: Ib2b2b6ed7e41f024d0da2bd035caec4317c2869c
Reviewed-on: https://go-review.googlesource.com/37837
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

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

index d2efad4518b5f9d4a77f7c8c8b257cbcd70049d8..b15754ee933865706f414788cdb469266ac01fbd 100644 (file)
@@ -254,6 +254,7 @@ func (e CorruptInputError) Error() string {
 // indicates if end-of-message padding or a partial quantum was encountered
 // and thus any additional data is an error.
 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
+       var inIdx int
        si := 0
 
        // skip over newlines
@@ -275,6 +276,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
                                break
                        }
                        in := src[si]
+                       inIdx = si
 
                        si++
                        // skip over newlines
@@ -287,7 +289,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
                                switch j {
                                case 0, 1:
                                        // incorrect padding
-                                       return n, false, CorruptInputError(si - 1)
+                                       return n, false, CorruptInputError(inIdx)
                                case 2:
                                        // "==" is expected, the first "=" is already consumed.
                                        if si == len(src) {
@@ -314,7 +316,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
                        }
                        dbuf[j] = enc.decodeMap[in]
                        if dbuf[j] == 0xFF {
-                               return n, false, CorruptInputError(si - 1)
+                               return n, false, CorruptInputError(inIdx)
                        }
                }
 
index e2e1d59f3c0d5c547e7332d32f40ae7e15a2fc5b..00b3d6171f94c35373ad3cdb7734d858248400f1 100644 (file)
@@ -220,6 +220,8 @@ func TestDecodeCorrupt(t *testing.T) {
                {"AAAA", -1},
                {"AAAAAA=", 7},
                {"YWJjZA=====", 8},
+               {"A!\n", 1},
+               {"A=\n", 1},
        }
        for _, tc := range testCases {
                dbuf := make([]byte, StdEncoding.DecodedLen(len(tc.input)))