]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/ascii85: handle non-data bytes correctly
authorRui Ueyama <ruiu@google.com>
Sun, 27 Apr 2014 02:56:06 +0000 (19:56 -0700)
committerIan Lance Taylor <iant@golang.org>
Sun, 27 Apr 2014 02:56:06 +0000 (19:56 -0700)
Previously Read wouldn't return once its internal input buffer
is filled with non-data bytes.
Fixes #7875.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/90820043

src/pkg/encoding/ascii85/ascii85.go
src/pkg/encoding/ascii85/ascii85_test.go

index e2afc587140a3fedfe00cb1966105fd38668e0db..60da304b55e5e8330d7e870beba1ec61f5aaa135 100644 (file)
@@ -281,6 +281,18 @@ func (d *decoder) Read(p []byte) (n int, err error) {
                                d.nbuf = copy(d.buf[0:], d.buf[nsrc:d.nbuf])
                                continue // copy out and return
                        }
+                       if ndst == 0 && d.err == nil {
+                               // Special case: input buffer is mostly filled with non-data bytes.
+                               // Filter out such bytes to make room for more input.
+                               off := 0
+                               for i := 0; i < d.nbuf; i++ {
+                                       if d.buf[i] > ' ' {
+                                               d.buf[off] = d.buf[i]
+                                               off++
+                                       }
+                               }
+                               d.nbuf = off
+                       }
                }
 
                // Out of input, out of decoded output.  Check errors.
index 77bc465d5949c916df1c6a75c8d244fb53e25aa4..aad199b4fad56be2fe05d8f089a5e0dfb002ac54 100644 (file)
@@ -197,3 +197,14 @@ func TestBig(t *testing.T) {
                t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i)
        }
 }
+
+func TestDecoderInternalWhitespace(t *testing.T) {
+       s := strings.Repeat(" ", 2048) + "z"
+       decoded, err := ioutil.ReadAll(NewDecoder(strings.NewReader(s)))
+       if err != nil {
+               t.Errorf("Decode gave error %v", err)
+       }
+       if want := []byte("\000\000\000\000"); !bytes.Equal(want, decoded) {
+               t.Errorf("Decode failed: got %v, want %v", decoded, want)
+       }
+}