Commit 9d7b9fb7 authored by Rui Ueyama's avatar Rui Ueyama Committed by Ian Lance Taylor

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
parent d4613483
...@@ -281,6 +281,18 @@ func (d *decoder) Read(p []byte) (n int, err error) { ...@@ -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]) d.nbuf = copy(d.buf[0:], d.buf[nsrc:d.nbuf])
continue // copy out and return 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. // Out of input, out of decoded output. Check errors.
......
...@@ -197,3 +197,14 @@ func TestBig(t *testing.T) { ...@@ -197,3 +197,14 @@ func TestBig(t *testing.T) {
t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i) 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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment