Skip to content

Commit

Permalink
encoding/base32: fix buffer without padding
Browse files Browse the repository at this point in the history
If unpadded content was passed, in some occassions content was omitted,
because the division result was floored. Ceiling it makes sure all
content is always read.

Fixes golang#65166

Signed-off-by: Niklas Ott <ceriath12@gmail.com>
  • Loading branch information
ceriath committed Apr 24, 2024
1 parent 960fa9b commit 9a0bb1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/encoding/base32/base32.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package base32

import (
"io"
"math"
"slices"
"strconv"
)
Expand Down Expand Up @@ -467,7 +468,7 @@ func (d *decoder) Read(p []byte) (n int, err error) {
}

// Read a chunk.
nn := len(p) / 5 * 8
nn := int(math.Ceil(float64(len(p))/float64(5)) * 8)
if nn < 8 {
nn = 8
}
Expand Down
18 changes: 18 additions & 0 deletions src/encoding/base32/base32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ func TestEncoderBuffering(t *testing.T) {
}
}

func TestDecoderBufferingAll(t *testing.T) {
for bs := 0; bs <= 12; bs++ {
for _, s := range pairs {
decoder := NewDecoder(StdEncoding, strings.NewReader(s.encoded))
buf := make([]byte, len(s.decoded)+bs)

var n int
var err error
n, err = decoder.Read(buf)

if err != nil && err != io.EOF {
t.Errorf("Read from %q at pos %d = %d, unexpected error %v", s.encoded, len(s.decoded), n, err)
}
testEqual(t,"Decoding/%d of %q = %q, want %q\n", bs, s.encoded, string(buf[:n]), s.decoded)
}
}
}

func TestDecode(t *testing.T) {
for _, p := range pairs {
dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)))
Expand Down

0 comments on commit 9a0bb1f

Please sign in to comment.