From 3e0c21e0335f5ec01ea13a86e1fd32ee066f369d Mon Sep 17 00:00:00 2001 From: Mark Ryan Date: Fri, 16 Jun 2017 11:34:28 +0100 Subject: [PATCH] encoding: fix endless loop in TestDecoderBuffering The ascii85, base32 and base64 packages all contain a test called TestDecoderBuffering. Each of these tests contain a loop that ignores the error returned from the Read method of their decoders. The result being that the tests loop for ever if the decoders actually return an error. This commit fixes the issue by terminating the loops if an error occurs and failing the tests with a suitable error message. Change-Id: Idb385673cf9f3f6f8befe4288b4be366ab0985fd Reviewed-on: https://go-review.googlesource.com/46010 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/encoding/ascii85/ascii85_test.go | 10 +++++++--- src/encoding/base32/base32_test.go | 10 +++++++--- src/encoding/base64/base64_test.go | 10 +++++++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/encoding/ascii85/ascii85_test.go b/src/encoding/ascii85/ascii85_test.go index ba85b64156c..b8be9608632 100644 --- a/src/encoding/ascii85/ascii85_test.go +++ b/src/encoding/ascii85/ascii85_test.go @@ -135,11 +135,15 @@ func TestDecoderBuffering(t *testing.T) { decoder := NewDecoder(strings.NewReader(bigtest.encoded)) buf := make([]byte, len(bigtest.decoded)+12) var total int - for total = 0; total < len(bigtest.decoded); { - n, err := decoder.Read(buf[total : total+bs]) - testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil)) + var n int + var err error + for total = 0; total < len(bigtest.decoded) && err == nil; { + n, err = decoder.Read(buf[total : total+bs]) total += n } + if err != nil && err != io.EOF { + t.Errorf("Read from %q at pos %d = %d, unexpected error %v", bigtest.encoded, total, n, err) + } testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded) } } diff --git a/src/encoding/base32/base32_test.go b/src/encoding/base32/base32_test.go index 12256d08ee8..ee7525c997c 100644 --- a/src/encoding/base32/base32_test.go +++ b/src/encoding/base32/base32_test.go @@ -284,11 +284,15 @@ func TestDecoderBuffering(t *testing.T) { decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded)) buf := make([]byte, len(bigtest.decoded)+12) var total int - for total = 0; total < len(bigtest.decoded); { - n, err := decoder.Read(buf[total : total+bs]) - testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil)) + var n int + var err error + for total = 0; total < len(bigtest.decoded) && err == nil; { + n, err = decoder.Read(buf[total : total+bs]) total += n } + if err != nil && err != io.EOF { + t.Errorf("Read from %q at pos %d = %d, unexpected error %v", bigtest.encoded, total, n, err) + } testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded) } } diff --git a/src/encoding/base64/base64_test.go b/src/encoding/base64/base64_test.go index 59818d37757..05011fbdf38 100644 --- a/src/encoding/base64/base64_test.go +++ b/src/encoding/base64/base64_test.go @@ -189,11 +189,15 @@ func TestDecoderBuffering(t *testing.T) { decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded)) buf := make([]byte, len(bigtest.decoded)+12) var total int - for total = 0; total < len(bigtest.decoded); { - n, err := decoder.Read(buf[total : total+bs]) - testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil)) + var n int + var err error + for total = 0; total < len(bigtest.decoded) && err == nil; { + n, err = decoder.Read(buf[total : total+bs]) total += n } + if err != nil && err != io.EOF { + t.Errorf("Read from %q at pos %d = %d, unexpected error %v", bigtest.encoded, total, n, err) + } testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded) } }