Skip to content

Commit 3feaeaa

Browse files
committed
Fix #158, add guards to avoid IndexError
1 parent d2935f1 commit 3feaeaa

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repos:
88
pass_filenames: false
99
- repo: https://github.com/astral-sh/ruff-pre-commit
1010
# Ruff version.
11-
rev: v0.14.1
11+
rev: v0.14.3
1212
hooks:
1313
# Run the linter.
1414
- id: ruff-check
@@ -38,7 +38,7 @@ repos:
3838
pass_filenames: false
3939
types: [python]
4040
- repo: https://github.com/semgrep/pre-commit
41-
rev: "v1.140.0"
41+
rev: "v1.142.0"
4242
hooks:
4343
- id: semgrep
4444
args:

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.52.3"
6+
version = "0.52.4"
77
license = "MIT"
88
license-files = ["LICENSE"]
99
authors = [

src/json_repair/parse_string.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def parse_string(self: "JSONParser") -> JSONReturnType:
193193
not self.stream_stable
194194
and char == "]"
195195
and ContextValues.ARRAY in self.context.context
196-
and string_acc[-1] != rstring_delimiter
196+
and (not string_acc or string_acc[-1] != rstring_delimiter)
197197
):
198198
# We found the end of an array and we are in array context
199199
# So let's check if we find a rstring_delimiter forward otherwise end early
@@ -226,9 +226,9 @@ def parse_string(self: "JSONParser") -> JSONReturnType:
226226
self.index += 1
227227
char = self.get_char_at()
228228
# Unclosed string ends with a \ character. This character is ignored if stream_stable = True.
229-
if self.stream_stable and not char and string_acc[-1] == "\\":
229+
if self.stream_stable and not char and string_acc and string_acc[-1] == "\\":
230230
string_acc = string_acc[:-1]
231-
if char and string_acc[-1] == "\\":
231+
if char and string_acc and string_acc[-1] == "\\":
232232
# This is a special case, if people use real strings this might happen
233233
self.log("Found a stray escape sequence, normalizing it")
234234
if char in [rstring_delimiter, "t", "n", "r", "b", "\\"]:
@@ -237,7 +237,7 @@ def parse_string(self: "JSONParser") -> JSONReturnType:
237237
string_acc += escape_seqs.get(char, char)
238238
self.index += 1
239239
char = self.get_char_at()
240-
while char and string_acc[-1] == "\\" and char in [rstring_delimiter, "\\"]:
240+
while char and string_acc and string_acc[-1] == "\\" and char in [rstring_delimiter, "\\"]:
241241
# this is a bit of a special case, if I don't do this it will close the loop or create a train of \\
242242
# I don't love it though
243243
string_acc = string_acc[:-1] + char
@@ -289,7 +289,7 @@ def parse_string(self: "JSONParser") -> JSONReturnType:
289289
)
290290
break
291291
# ChatGPT sometimes forget to quote stuff in html tags or markdown, so we do this whole thing here
292-
if char == rstring_delimiter and string_acc[-1] != "\\":
292+
if char == rstring_delimiter and string_acc and string_acc[-1] != "\\":
293293
# Special case here, in case of double quotes one after another
294294
if doubled_quotes and self.get_char_at(1) == rstring_delimiter:
295295
self.log("While parsing a string, we found a doubled quote, ignoring it")

0 commit comments

Comments
 (0)