From: Rui Ueyama Date: Sun, 27 Apr 2014 02:56:06 +0000 (-0700) Subject: encoding/ascii85: handle non-data bytes correctly X-Git-Tag: go1.3beta2~185 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9d7b9fb7d01c831e67b82f352676097835af01fc;p=gostls13.git encoding/ascii85: handle non-data bytes correctly 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 --- diff --git a/src/pkg/encoding/ascii85/ascii85.go b/src/pkg/encoding/ascii85/ascii85.go index e2afc58714..60da304b55 100644 --- a/src/pkg/encoding/ascii85/ascii85.go +++ b/src/pkg/encoding/ascii85/ascii85.go @@ -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. diff --git a/src/pkg/encoding/ascii85/ascii85_test.go b/src/pkg/encoding/ascii85/ascii85_test.go index 77bc465d59..aad199b4fa 100644 --- a/src/pkg/encoding/ascii85/ascii85_test.go +++ b/src/pkg/encoding/ascii85/ascii85_test.go @@ -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) + } +}