valyala / fastjson

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
MIT License
2.3k stars 138 forks source link

Add a check for redundant . and e in number parser #89

Open vearutop opened 1 year ago

vearutop commented 1 year ago

This PR naively fixes false positive parsing of an invalid number that contains more than one . or e. https://github.com/valyala/fastjson/issues/88#issuecomment-1413818799

package main

import (
    "fmt"

    "github.com/valyala/fastjson"
)

func main() {
    v, err := fastjson.ParseBytes([]byte(`[127.0.0.1]`))
    if err != nil {
        fmt.Println("failed:", err.Error())
    } else {
        fmt.Println("passed:", v.Type())
    }

    err = fastjson.ValidateBytes([]byte(`[127.0.0.1]`))
    if err != nil {
        fmt.Println("validation failed:", err.Error())
    }

    // Output:
    // passed: array
    // validation failed: cannot parse JSON: cannot parse array: missing ',' after array value; unparsed tail: ".0.1]"
}