Skip to content

Invalid number swallowed without any error #12

Description

@sKopheK
"text-embedding-3-small": {
      "vector": [1.4564884488666646, 2, 3, 4, 5, .0516156161551515, 7],
      "info": "PROMPT \"representation\" MODEL \"text-embedding-3-small\""
    }

results in

"text-embedding-3-small": {
      "vector": [1.4564884488666646, 2, 3, 4, 5]
    }

how can i get some error reported instead of swallowing syntax error and cutting the rest of valid tokens off within parent?

Activity

  1. CNSeniorious000 commented on Jul 18, 2025

    @CNSeniorious000
    Member

    Thank you for raising this issue! The behavior of partial-json here is indeed not ideal. To be honest, there are a lot of similar problems, but people probably haven't mentioned them because they simply haven't encountered them.

    The Python version of this library handles this case more thoroughly—it throws an error when it encounters invalid JSON. I think I can fix this issue in v0.2 of the JavaScript library.

    However, I'm quite busy these days, so I'm wondering, is this something you need urgently? Maybe you could consider submitting a PR?

    Minimal reproduction

    import { parse } from "partial-json"
    
    console.log(parse(`
         [abc
    `))

    Expected behavior

    An error should be thrown instead of returning [].

    @sKopheK Do you agree with this expected behavior?

  2. sKopheK commented on Jul 18, 2025

    @sKopheK
    Author

    hello, @CNSeniorious000, thanks for the quick reply.
    I'm ok with both suggested and current behavior, just need to know that provided JSON is malformed (not considering incomplete of course) - throwing an exception with whatever it parsed might work even though I have probably never seen such combination :).

  3. sKopheK commented on Jul 21, 2025

    @sKopheK
    Author

    hi, found another, even more serious one: when there's space between empty array brackets, everything after this gets discarded:

    example:

    [
    { "id":1,"arr":["hello"]},{"id"     :2    ,    "arr": [        ],"more":"yaya"},{"id":3,"arr":["!"]}
    ]
    

    returns

    [
        {
            "id": 1,
            "arr": [
                "hello"
            ]
        },
        {
            "id": 2,
            "arr": []
        }
    ]
    

    empty space between curly brackets is fine

  4. CNSeniorious000 commented on Jul 21, 2025

    @CNSeniorious000
    Member

    Really thank you for discovering this!

    I will be attending a hackathon starting tomorrow, but I will make sure to release a new version of this package that addresses these issues no later than July 30th.

  5. added a commit that references this issue on Aug 4, 2025
  6. joszamama commented on Nov 23, 2026

    @joszamama

    Still present in 0.1.7 and on main (86fd4bc), Node.js 26. A minimal case for the "space between empty array brackets" report above, two documents, one per line. Paste into an empty folder:

    npm install --ignore-scripts partial-json@0.1.7 >/dev/null
    echo 'W1sgXSwxXQp7ImEiOlsgXSwiYiI6MX0K' | base64 -d > cases.txt
    cat cases.txt
    cat > repro.cjs <<'EOF'
    const fs = require('fs')
    const { parse } = require('partial-json')
    for (const text of fs.readFileSync(process.argv[2], 'utf8').split('\n').filter(Boolean)) {
      console.log(text, '->', JSON.stringify(parse(text)), '| JSON.parse:', JSON.stringify(JSON.parse(text)))
    }
    EOF
    node repro.cjs cases.txt

    Output:

    [[ ],1]
    {"a":[ ],"b":1}
    [[ ],1] -> [[]] | JSON.parse: [[],1]
    {"a":[ ],"b":1} -> {"a":[]} | JSON.parse: {"a":[],"b":1}
    

    In parseArr (src/index.ts L125-L144) there is no skipBlank() after index++, so for [ ] the loop calls parseAny(), which reaches parseNum and throws a malformed-JSON error. The catch returns the partial array because Allow.ARR is set, without consuming ], so the enclosing array or object ends at that ] and the rest of the input is ignored. Adding skipBlank() after index++ (as parseObj does after {) fixes this case; PR #13 contains that line, if the rest of #13 or #19 needs more time.

    I found this with the Fandango fuzzer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions