]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base32: decoder output depends on chunking of underlying reader
authorteivah <t.harsanyi@thebeat.co>
Tue, 3 May 2022 09:54:48 +0000 (09:54 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 3 May 2022 18:30:15 +0000 (18:30 +0000)
After an analysis, I figured that a way to do it could be to check, after
the call to readEncodedData whether the decoder already saw the end or not.

Fixes #38657

Change-Id: I06fd718ea4ee6ded2cb26c2866b28581ad86e271
GitHub-Last-Rev: d0b7bb38e4301a2ae9b8e588944488dbd88b39c4
GitHub-Pull-Request: golang/go#52631
Reviewed-on: https://go-review.googlesource.com/c/go/+/403315
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/encoding/base32/base32.go
src/encoding/base32/base32_test.go

index 5f3af4c8bbe28a7eec599e809391cfa2f5a7dad8..fa6e42e26c557c516b5816899a96808a2ac9ee42 100644 (file)
@@ -445,6 +445,9 @@ func (d *decoder) Read(p []byte) (n int, err error) {
        if d.nbuf < min {
                return 0, d.err
        }
+       if nn > 0 && d.end {
+               return 0, CorruptInputError(0)
+       }
 
        // Decode chunk into p, or d.out and then p if p is too small.
        var nr int
index dbd2b613b42aefac03e6545e675a016ed3d98752..323d04e68b02f5979c0d9373c9622c8497407957 100644 (file)
@@ -627,6 +627,58 @@ func TestBufferedDecodingSameError(t *testing.T) {
        }
 }
 
+func TestBufferedDecodingPadding(t *testing.T) {
+       testcases := []struct {
+               chunks        []string
+               expectedError string
+       }{
+               {[]string{
+                       "I4======",
+                       "==",
+               }, "unexpected EOF"},
+
+               {[]string{
+                       "I4======N4======",
+               }, "illegal base32 data at input byte 2"},
+
+               {[]string{
+                       "I4======",
+                       "N4======",
+               }, "illegal base32 data at input byte 0"},
+
+               {[]string{
+                       "I4======",
+                       "========",
+               }, "illegal base32 data at input byte 0"},
+
+               {[]string{
+                       "I4I4I4I4",
+                       "I4======",
+                       "I4======",
+               }, "illegal base32 data at input byte 0"},
+       }
+
+       for _, testcase := range testcases {
+               testcase := testcase
+               pr, pw := io.Pipe()
+               go func() {
+                       for _, chunk := range testcase.chunks {
+                               _, _ = pw.Write([]byte(chunk))
+                       }
+                       _ = pw.Close()
+               }()
+
+               decoder := NewDecoder(StdEncoding, pr)
+               _, err := io.ReadAll(decoder)
+
+               if err == nil && len(testcase.expectedError) != 0 {
+                       t.Errorf("case %q: got nil error, want %v", testcase.chunks, testcase.expectedError)
+               } else if err.Error() != testcase.expectedError {
+                       t.Errorf("case %q: got %v, want %v", testcase.chunks, err, testcase.expectedError)
+               }
+       }
+}
+
 func TestEncodedDecodedLen(t *testing.T) {
        type test struct {
                in      int