Skip to content

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/[email protected]/encode.go:201
github.com/francoispqt/gojay.(*Encoder).Float64(0xc0000bb920, 0x4045000000000000)
	/tmp/gopath957066451/pkg/mod/github.com/francoispqt/[email protected]/encode_number_float.go:124 +0x205
github.com/francoispqt/gojay.(*Encoder).Float(...)
	/tmp/gopath957066451/pkg/mod/github.com/francoispqt/[email protected]/encode_number_float.go:61
github.com/francoispqt/gojay.(*Encoder).AddFloat64(...)
	/tmp/gopath957066451/pkg/mod/github.com/francoispqt/[email protected]/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.

1 participant