1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 19:43:49 +08:00

fix: incorrect unsafe usage in upstream lib

Updates #1070
This commit is contained in:
wdvxdr 2021-09-17 15:37:09 +08:00
parent 760bb175c7
commit 6d19f07eb4
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
3 changed files with 14 additions and 4 deletions

View File

@ -21,7 +21,6 @@ import (
"github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"github.com/segmentio/asm/base64"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -1118,7 +1117,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
return &LocalImageElement{File: fu.Path}, nil return &LocalImageElement{File: fu.Path}, nil
} }
if strings.HasPrefix(f, "base64") && !video { if strings.HasPrefix(f, "base64") && !video {
b, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(f, "base64://")) b, err := global.Base64DecodeString(strings.TrimPrefix(f, "base64://"))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -14,7 +14,6 @@ import (
"strings" "strings"
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
"github.com/segmentio/asm/base64"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -98,7 +97,7 @@ func FindFile(file, cache, p string) (data []byte, err error) {
return nil, err return nil, err
} }
case strings.HasPrefix(file, "base64"): case strings.HasPrefix(file, "base64"):
data, err = base64.StdEncoding.DecodeString(strings.TrimPrefix(file, "base64://")) data, err = Base64DecodeString(strings.TrimPrefix(file, "base64://"))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -8,6 +8,8 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/Mrs4s/MiraiGo/utils"
"github.com/segmentio/asm/base64"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@ -152,3 +154,13 @@ func SplitURL(s string) []string {
result = append(result, s[last:]) result = append(result, s[last:])
return result return result
} }
// Base64DecodeString decode base64 with avx2
// see https://github.com/segmentio/asm/issues/50
// avoid incorrect unsafe usage in origin library
func Base64DecodeString(s string) ([]byte, error) {
e := base64.StdEncoding
dst := make([]byte, e.DecodedLen(len(s)))
n, err := e.Decode(dst, utils.S2B(s))
return dst[:n], err
}