Skip to content

Commit e27c9ac

Browse files
authored
Fix #123, Merge pull request #125 from mangiucugna/codex/fix-infinite-loop-with-in-repair-json
Fix comment parser infinite loop
2 parents e02e076 + b79873c commit e27c9ac

File tree

4 files changed

+8
-1
lines changed

4 files changed

+8
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ then you can use use it in your code like this
6464
good_json_string = repair_json(bad_json_string)
6565
# If the string was super broken this will return an empty string
6666

67+
6768
You can use this library to completely replace `json.loads()`:
6869

6970
import json_repair

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
33
build-backend = "setuptools.build_meta"
44
[project]
55
name = "json_repair"
6-
version = "0.45.0"
6+
version = "0.45.1"
77
license = {file = "LICENSE"}
88
authors = [
99
{ name="Stefano Baccianella", email="[email protected]" },

src/json_repair/json_parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,11 @@ def parse_comment(self) -> str:
790790
break
791791
self.log(f"Found block comment: {comment}")
792792
return ""
793+
else:
794+
# Skip standalone '/' characters that are not part of a comment
795+
# to avoid getting stuck in an infinite loop
796+
self.index += 1
797+
return ""
793798
return "" # pragma: no cover
794799

795800
def get_char_at(self, count: int = 0) -> str | Literal[False]:

tests/test_json_repair.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_general_edge_cases():
8383
assert repair_json("[[1\n\n]") == "[[1]]"
8484
assert repair_json("string") == ""
8585
assert repair_json("stringbeforeobject {}") == "{}"
86+
assert repair_json("/") == ""
8687

8788

8889
def test_mixed_data_types():

0 commit comments

Comments
 (0)