diff options
| author | Rui Ueyama <ruiu@google.com> | 2014-04-26 19:56:06 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2014-04-26 19:56:06 -0700 |
| commit | 9d7b9fb7d01c831e67b82f352676097835af01fc (patch) | |
| tree | 36638d47ed49b0bd9e6d0bb1a485cecd28e0c5b0 /src/pkg/encoding | |
| parent | d46134830fff5ab20950762f7b7371a7dbef5871 (diff) | |
| download | go-9d7b9fb7d01c831e67b82f352676097835af01fc.tar.xz | |
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
Diffstat (limited to 'src/pkg/encoding')
| -rw-r--r-- | src/pkg/encoding/ascii85/ascii85.go | 12 | ||||
| -rw-r--r-- | src/pkg/encoding/ascii85/ascii85_test.go | 11 |
2 files changed, 23 insertions, 0 deletions
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) + } +} |
