From 87151c82b68023e4224b016a6a66ead2c4b8ece7 Mon Sep 17 00:00:00 2001 From: Caleb Spare Date: Sun, 13 Mar 2016 17:59:26 -0700 Subject: [PATCH] encoding/base64: correct DecodedLen overestimate for unpadded encodings While we're at it, add tests for EncodedLen and DecodedLen. Fixes #14803. Change-Id: I200c72cf11c51669b8d9f70c6e57ece359f7ae61 Reviewed-on: https://go-review.googlesource.com/20649 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/encoding/base64/base64.go | 2 +- src/encoding/base64/base64_test.go | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go index 0de9b40f856..c2116d8a343 100644 --- a/src/encoding/base64/base64.go +++ b/src/encoding/base64/base64.go @@ -459,7 +459,7 @@ func NewDecoder(enc *Encoding, r io.Reader) io.Reader { func (enc *Encoding) DecodedLen(n int) int { if enc.padChar == NoPadding { // Unpadded data may end with partial block of 2-3 characters. - return (n*6 + 7) / 8 + return n * 6 / 8 } // Padded base64 should always be a multiple of 4 characters in length. return n / 4 * 3 diff --git a/src/encoding/base64/base64_test.go b/src/encoding/base64/base64_test.go index eebf113212a..19ddb92f644 100644 --- a/src/encoding/base64/base64_test.go +++ b/src/encoding/base64/base64_test.go @@ -234,6 +234,51 @@ func TestDecodeCorrupt(t *testing.T) { } } +func TestEncodedLen(t *testing.T) { + for _, tt := range []struct { + enc *Encoding + n int + want int + }{ + {RawStdEncoding, 0, 0}, + {RawStdEncoding, 1, 2}, + {RawStdEncoding, 2, 3}, + {RawStdEncoding, 3, 4}, + {RawStdEncoding, 7, 10}, + {StdEncoding, 0, 0}, + {StdEncoding, 1, 4}, + {StdEncoding, 2, 4}, + {StdEncoding, 3, 4}, + {StdEncoding, 4, 8}, + {StdEncoding, 7, 12}, + } { + if got := tt.enc.EncodedLen(tt.n); got != tt.want { + t.Errorf("EncodedLen(%d): got %d, want %d", tt.n, got, tt.want) + } + } +} + +func TestDecodedLen(t *testing.T) { + for _, tt := range []struct { + enc *Encoding + n int + want int + }{ + {RawStdEncoding, 0, 0}, + {RawStdEncoding, 2, 1}, + {RawStdEncoding, 3, 2}, + {RawStdEncoding, 4, 3}, + {RawStdEncoding, 10, 7}, + {StdEncoding, 0, 0}, + {StdEncoding, 4, 3}, + {StdEncoding, 8, 6}, + } { + if got := tt.enc.DecodedLen(tt.n); got != tt.want { + t.Errorf("DecodedLen(%d): got %d, want %d", tt.n, got, tt.want) + } + } +} + func TestBig(t *testing.T) { n := 3*1000 + 1 raw := make([]byte, n)