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

Decoder.{{Type}}Null does not behave as documented when encountering incorrect typed json elements. #158

Open
Foxcapades opened this issue Oct 26, 2020 · 0 comments

Comments

@Foxcapades
Copy link

Foxcapades commented Oct 26, 2020

This was initially detected with Decoder.StringNull, but it appears to affect all Decoder.{{Type}}Null methods.

As per the StringNull method documentation, when the next value is not a string or null, the method should return an InvalidUnmarshalError, however the method returns no such error.

Version in use: github.com/francoispqt/gojay v1.2.13

Method docs:

// StringNull decodes the JSON value within an object or an array to a **string.
// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.

Demonstration:

package main

import (
	"bytes"
	"fmt"
	"github.com/francoispqt/gojay"
)

func main() {
	var tmp *string

	dec := gojay.NewDecoder(bytes.NewBufferString("13245"))

	// This should _not_ return nil.  It should return an InvalidUnmarshalError.
	fmt.Println(dec.StringNull(&tmp))
}

Which results with:

<nil>

It appears that the Decoder method decodeStringNull is calling skipData after encountering an invalid character (and assigning the correct error to dec.err), which just skips over the bad data and then returns nil, if skipData returns nil, and completely ignores the initial error.

Starting from https://github.com/francoispqt/gojay/blob/master/decode_string.go#L82-L88

		default:
			dec.err = dec.makeInvalidUnmarshalErr(v)
			err := dec.skipData()
			if err != nil {
				return err
			}
			return nil
Foxcapades added a commit to Foxcapades/gojay that referenced this issue Oct 27, 2020
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

No branches or pull requests

1 participant