Skip to content

Commit

Permalink
Merge pull request #266 from ceshihao/fix_base64_with_whitespace
Browse files Browse the repository at this point in the history
fix base64 contains newline case
  • Loading branch information
taowen committed Apr 24, 2018
2 parents 6c702ce + 6a6742f commit 2ddf6d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion misc_tests/jsoniter_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package misc_tests
import (
"bytes"
"encoding/json"
"testing"

"github.com/json-iterator/go"
"github.com/stretchr/testify/require"
"testing"
)

func Test_empty_array(t *testing.T) {
Expand Down Expand Up @@ -168,6 +169,17 @@ func Test_decode_byte_array_from_base64(t *testing.T) {
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) {
should := require.New(t)
data := []byte{}
Expand Down
8 changes: 7 additions & 1 deletion reflect_native.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package jsoniter

import (
"bytes"
"encoding/base64"
"github.com/modern-go/reflect2"
"reflect"
"strconv"
"unsafe"

"github.com/modern-go/reflect2"
)

const ptrSize = 32 << uintptr(^uintptr(0)>>63)
Expand Down Expand Up @@ -418,6 +420,10 @@ func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
case StringValue:
encoding := base64.StdEncoding
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]
decodedLen := encoding.DecodedLen(len(src))
dst := make([]byte, decodedLen)
Expand Down

0 comments on commit 2ddf6d7

Please sign in to comment.