Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent from panic encoding scalar types #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ func (enc *Encoder) Write() (int, error) {
return i, err
}

func (enc *Encoder) getPreviousRune() byte {
// getPreviousRune returns false when the buffer is empty.
func (enc *Encoder) getPreviousRune() (byte, bool) {
last := len(enc.buf) - 1
return enc.buf[last]
if last < 0 {
return ' ', false
}

return enc.buf[last], true
}
32 changes: 16 additions & 16 deletions encode_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ func (enc *Encoder) AddArrayKeyNullEmpty(key string, v MarshalerJSONArray) {
func (enc *Encoder) Array(v MarshalerJSONArray) {
if v.IsNil() {
enc.grow(3)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
enc.writeByte(']')
return
}
enc.grow(100)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
Expand All @@ -87,8 +87,8 @@ func (enc *Encoder) ArrayOmitEmpty(v MarshalerJSONArray) {
return
}
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
Expand All @@ -100,8 +100,8 @@ func (enc *Encoder) ArrayOmitEmpty(v MarshalerJSONArray) {
// value must implement Marshaler
func (enc *Encoder) ArrayNullEmpty(v MarshalerJSONArray) {
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
if v.IsNil() {
Expand All @@ -123,8 +123,8 @@ func (enc *Encoder) ArrayKey(key string, v MarshalerJSONArray) {
}
if v.IsNil() {
enc.grow(2 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -134,8 +134,8 @@ func (enc *Encoder) ArrayKey(key string, v MarshalerJSONArray) {
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -157,8 +157,8 @@ func (enc *Encoder) ArrayKeyOmitEmpty(key string, v MarshalerJSONArray) {
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -177,8 +177,8 @@ func (enc *Encoder) ArrayKeyNullEmpty(key string, v MarshalerJSONArray) {
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
if v.IsNil() {
Expand Down
24 changes: 12 additions & 12 deletions encode_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (enc *Encoder) AddBoolKeyNullEmpty(key string, v bool) {
// Bool adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Bool(v bool) {
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
if v {
Expand All @@ -79,8 +79,8 @@ func (enc *Encoder) BoolOmitEmpty(v bool) {
return
}
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeString("true")
Expand All @@ -89,8 +89,8 @@ func (enc *Encoder) BoolOmitEmpty(v bool) {
// BoolNullEmpty adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) BoolNullEmpty(v bool) {
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
if v == false {
Expand All @@ -108,8 +108,8 @@ func (enc *Encoder) BoolKey(key string, value bool) {
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -130,8 +130,8 @@ func (enc *Encoder) BoolKeyOmitEmpty(key string, v bool) {
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -149,8 +149,8 @@ func (enc *Encoder) BoolKeyNullEmpty(key string, v bool) {
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand Down
16 changes: 8 additions & 8 deletions encode_embedded_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func (enc *Encoder) encodeEmbeddedJSON(v *EmbeddedJSON) ([]byte, error) {
// it expects the JSON to be of proper format.
func (enc *Encoder) AddEmbeddedJSON(v *EmbeddedJSON) {
enc.grow(len(*v) + 4)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeBytes(*v)
Expand All @@ -40,8 +40,8 @@ func (enc *Encoder) AddEmbeddedJSONOmitEmpty(v *EmbeddedJSON) {
if v == nil || len(*v) == 0 {
return
}
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeBytes(*v)
Expand All @@ -58,8 +58,8 @@ func (enc *Encoder) AddEmbeddedJSONKey(key string, v *EmbeddedJSON) {
}
}
enc.grow(len(key) + len(*v) + 5)
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -82,8 +82,8 @@ func (enc *Encoder) AddEmbeddedJSONKeyOmitEmpty(key string, v *EmbeddedJSON) {
return
}
enc.grow(len(key) + len(*v) + 5)
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand Down
8 changes: 4 additions & 4 deletions encode_null.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ func (enc *Encoder) AddNull() {
// Null adds a `null` to be encoded. Must be used while encoding an array.`
func (enc *Encoder) Null() {
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeBytes(nullBytes)
Expand All @@ -28,8 +28,8 @@ func (enc *Encoder) NullKey(key string) {
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand Down
10 changes: 10 additions & 0 deletions encode_null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ func TestEncodeNull(t *testing.T) {
baseJSON string
expectedJSON string
}{
{
name: "encode null",
baseJSON: ``,
expectedJSON: `null,null`,
},
{
name: "basic 1st element",
baseJSON: `[`,
Expand Down Expand Up @@ -44,6 +49,11 @@ func TestEncodeNullKey(t *testing.T) {
baseJSON string
expectedJSON string
}{
{
name: "encode null",
baseJSON: ``,
expectedJSON: `"foo":null,"bar":null`,
},
{
name: "basic 1st element",
baseJSON: `{`,
Expand Down
48 changes: 24 additions & 24 deletions encode_number_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func (enc *Encoder) AddFloat64OmitEmpty(v float64) {
// Float64 adds a float64 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Float64(v float64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, v, 'f', -1, 64)
Expand All @@ -135,8 +135,8 @@ func (enc *Encoder) Float64OmitEmpty(v float64) {
return
}
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, v, 'f', -1, 64)
Expand All @@ -146,8 +146,8 @@ func (enc *Encoder) Float64OmitEmpty(v float64) {
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Float64NullEmpty(v float64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
if v == 0 {
Expand Down Expand Up @@ -175,8 +175,8 @@ func (enc *Encoder) Float64Key(key string, value float64) {
return
}
}
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.grow(10)
Expand All @@ -198,8 +198,8 @@ func (enc *Encoder) Float64KeyOmitEmpty(key string, v float64) {
return
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -217,8 +217,8 @@ func (enc *Encoder) Float64KeyNullEmpty(key string, v float64) {
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand Down Expand Up @@ -250,8 +250,8 @@ func (enc *Encoder) AddFloat32NullEmpty(v float32) {

// Float32 adds a float32 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Float32(v float32) {
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
Expand All @@ -264,8 +264,8 @@ func (enc *Encoder) Float32OmitEmpty(v float32) {
return
}
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
Expand All @@ -275,8 +275,8 @@ func (enc *Encoder) Float32OmitEmpty(v float32) {
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Float32NullEmpty(v float32) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
if v == 0 {
Expand Down Expand Up @@ -311,8 +311,8 @@ func (enc *Encoder) Float32Key(key string, v float32) {
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -334,8 +334,8 @@ func (enc *Encoder) Float32KeyOmitEmpty(key string, v float32) {
return
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand All @@ -353,8 +353,8 @@ func (enc *Encoder) Float32KeyNullEmpty(key string, v float32) {
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
r, ok := enc.getPreviousRune()
if ok && r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
Expand Down
Loading