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

Conversation

Alma-media
Copy link

@Alma-media Alma-media commented Oct 6, 2020

The encoder panics while encoding values explicitly since there is no previous rune:

package main

import (
	"strings"

	"github.com/francoispqt/gojay"
)

func main() {
	var (
		buff    strings.Builder
		encoder = gojay.BorrowEncoder(&buff)
	)
	defer encoder.Release()

	encoder.Float64(42) // or AddFloat ... AddXxxx ...
}

the output will be:

panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/francoispqt/gojay.(*Encoder).getPreviousRune(...)
	/tmp/gopath957066451/pkg/mod/github.com/francoispqt/gojay@v1.2.13/encode.go:201
github.com/francoispqt/gojay.(*Encoder).Float64(0xc0000bb920, 0x4045000000000000)
	/tmp/gopath957066451/pkg/mod/github.com/francoispqt/gojay@v1.2.13/encode_number_float.go:124 +0x205
github.com/francoispqt/gojay.(*Encoder).Float(...)
	/tmp/gopath957066451/pkg/mod/github.com/francoispqt/gojay@v1.2.13/encode_number_float.go:61
github.com/francoispqt/gojay.(*Encoder).AddFloat64(...)
	/tmp/gopath957066451/pkg/mod/github.com/francoispqt/gojay@v1.2.13/encode_number_float.go:112
main.main()
	/tmp/sandbox969752132/prog.go:17 +0x85

fix: check the length of the buffer in order to avoid panics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant