Skip to content

Scanner accepts malformed JSON numbers during indexing #3

Description

@whyvineet

_Scanner._number_end in index.py currently identifies a number by consuming any consecutive run of -+.eE0-9 characters, without validating the JSON number grammar.

As a result, malformed numbers such as 1.2.3, --5, 2e, and 1-2 are accepted while building the index. The invalid input is only detected later when the value is accessed, where it raises a raw ValueError from Python's numeric conversion.

This means malformed JSON is successfully indexed and the error occurs far from the point where the invalid syntax was encountered.

Reproduction

import bytejson

open("./tmp/badnum.json", "w").write("1.2.3")

db = bytejson.open("./tmp/badnum.json")
db.to_python()

Observed:

ValueError: could not convert string to float: '1.2.3'

Other malformed numbers such as:

--5
2e
1-2
1.2.3

are similarly accepted by the scanner and fail only when the value is accessed.

Expected behavior

Malformed JSON numbers should be rejected while building the index, with a clear JSONScanError.

For example:

db = bytejson.open("./tmp/badnum.json")

should fail during indexing rather than allowing an invalid document to enter the indexed representation.

This is consistent with the scanner's behavior for other malformed JSON syntax, where errors are reported during scanning/index construction.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions