segmentio/asm

base64 decode error

Mrs4s opened this issue · 3 comments

Mrs4s commented

Playground: https://play.golang.org/p/QlHEKjlenGp
Result:
image

it looks like random error, sometimes there is no mistake.

Hi @Mrs4s, thanks for sending this in. I'll take a look!

asm/base64/base64_amd64.go

Lines 145 to 147 in 86603cd

func (enc *Encoding) DecodeString(s string) ([]byte, error) {
src := *(*[]byte)(unsafe.Pointer(&s))
dst := make([]byte, enc.base.DecodedLen(len(s)))

Hi @kalamay , I think this unsafe usage is not correct, because string header has only 2 field but slice header has 3 field.
I have a program using this lib and it panics in std library encoding/json randomly on a computer without avx2 feature.
with panic info:

runtime error: slice bounds out of range [:8] with capacity 4

panic({0xc5d660, 0xc0003ce0a8})
        runtime/panic.go:1038 +0x215
encoding/base64.(*Encoding).Decode(0xc0000ca580, {0xc000418000, 0x5fc7, 0x5fc7}, {0xc0008120d1, 0x7fb4, 0xc000058c00})
        encoding/base64/base64.go:483 +0x605
github.com/segmentio/asm/base64.(*Encoding).Decode(0x41259a, {0xc000418000, 0x6a34044efc013184, 0x636f727070410f01}, {0xc0008120d1, 0xb2e589b8e41701b1, 0x0})
        github.com/segmentio/asm@v1.0.0/base64/base64_amd64.go:138 +0x14d
github.com/segmentio/asm/base64.(*Encoding).DecodeString(0xc00007e370, {0xc0008120d1, 0x7fb4})
        github.com/segmentio/asm@v1.0.0/base64/base64_amd64.go:148 +0xb7

I am convinced that it is caused by the unsafe convertion which produced incorrect slice capacity.

Yep, that was this issue. I've included a few updates to some internal table handling as well, but this issue should be resolved now. I appreciate you reporting this issue to us.