mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
fix base64 contains newline characters \r or \n
This commit is contained in:
parent
6c702ce12a
commit
6a6742f0a2
@ -3,9 +3,10 @@ package misc_tests
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_empty_array(t *testing.T) {
|
func Test_empty_array(t *testing.T) {
|
||||||
@ -168,6 +169,17 @@ func Test_decode_byte_array_from_base64(t *testing.T) {
|
|||||||
should.Equal([]byte{1, 2, 3}, data)
|
should.Equal([]byte{1, 2, 3}, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_decode_byte_array_from_base64_with_newlines(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
data := []byte{}
|
||||||
|
err := json.Unmarshal([]byte(`"A\rQ\nID"`), &data)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal([]byte{1, 2, 3}, data)
|
||||||
|
err = jsoniter.Unmarshal([]byte(`"A\rQ\nID"`), &data)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal([]byte{1, 2, 3}, data)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_decode_byte_array_from_array(t *testing.T) {
|
func Test_decode_byte_array_from_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
data := []byte{}
|
data := []byte{}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"github.com/modern-go/reflect2"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/modern-go/reflect2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ptrSize = 32 << uintptr(^uintptr(0)>>63)
|
const ptrSize = 32 << uintptr(^uintptr(0)>>63)
|
||||||
@ -418,6 +420,10 @@ func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
|||||||
case StringValue:
|
case StringValue:
|
||||||
encoding := base64.StdEncoding
|
encoding := base64.StdEncoding
|
||||||
src := iter.SkipAndReturnBytes()
|
src := iter.SkipAndReturnBytes()
|
||||||
|
// New line characters (\r and \n) are ignored.
|
||||||
|
// Refer to https://golang.org/pkg/encoding/base64/#Encoding.Decode
|
||||||
|
src = bytes.Replace(src, []byte(`\r`), []byte{}, -1)
|
||||||
|
src = bytes.Replace(src, []byte(`\n`), []byte{}, -1)
|
||||||
src = src[1 : len(src)-1]
|
src = src[1 : len(src)-1]
|
||||||
decodedLen := encoding.DecodedLen(len(src))
|
decodedLen := encoding.DecodedLen(len(src))
|
||||||
dst := make([]byte, decodedLen)
|
dst := make([]byte, decodedLen)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user