Skip to content

Commit 29356f7

Browse files
committed
fix infinite loop
1 parent 8d57431 commit 29356f7

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

extract_nextjs.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,31 @@ def iter_flight_record_values(nextjs_string):
2222
length = len(nextjs_string)
2323

2424
while pos < length:
25-
# Find next record boundary: start of string or just after newline.
26-
if pos > 0:
27-
newline_pos = nextjs_string.find("\n", pos)
28-
if newline_pos == -1:
29-
break
30-
pos = newline_pos + 1
31-
32-
key_start = pos
33-
while pos < length and nextjs_string[pos].isalnum():
34-
pos += 1
35-
36-
# Expect at least one key character, then a colon.
37-
if pos == key_start or pos >= length or nextjs_string[pos] != ":":
25+
line_end = nextjs_string.find("\n", pos)
26+
if line_end == -1:
27+
line_end = length
28+
29+
line = nextjs_string[pos:line_end]
30+
separator_idx = line.find(":")
31+
32+
# Move to the next line by default to guarantee forward progress.
33+
next_pos = line_end + 1
34+
35+
# Expect records formatted as <alnum_key>:<json_value>.
36+
if separator_idx <= 0:
37+
pos = next_pos
3838
continue
3939

40-
key = nextjs_string[key_start:pos]
41-
value_start = pos + 1
40+
key = line[:separator_idx]
41+
if not key.isalnum():
42+
pos = next_pos
43+
continue
44+
45+
value_start = pos + separator_idx + 1
4246

4347
# Flight records like I[...] / T... are references, not JSON values.
4448
if value_start < length and nextjs_string[value_start] in {"I", "T", "H"}:
45-
pos = value_start
49+
pos = next_pos
4650
continue
4751

4852
try:
@@ -52,6 +56,8 @@ def iter_flight_record_values(nextjs_string):
5256
# Skip malformed/non-JSON records and continue scanning.
5357
pass
5458

59+
pos = next_pos
60+
5561

5662
def _print_json_block(title, raw_json):
5763
print(f"\n{'=' * 60}")

0 commit comments

Comments
 (0)