Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/+escaped-quote-scan-linear.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed parsing time growing quadratically with the number of escaped quotes, such as ``\"``, in a quoted parameter value. A content line of about 1 MiB took 100 seconds to parse. I used AI (Claude Opus 5.5) to find the issue and to draft the fix, the test, and this change log entry. @shamsManna
6 changes: 5 additions & 1 deletion src/icalendar/parser/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def closes_quoted_section(text: str, quote_index: int) -> bool:
section. A DQUOTE followed by other text is an escaped quote inside
the value instead.
"""
for ch in text[quote_index + 1 :]:
# Parsers call this for every DQUOTE in a quoted value, so only look at
# the characters right after the quote. Slicing ``text`` here would copy
# the rest of the line on each call.
for index in range(quote_index + 1, len(text)):
ch = text[index]
if ch in " \t":
continue
return ch in _STRUCTURAL_DELIMITERS
Expand Down
16 changes: 16 additions & 0 deletions src/icalendar/tests/test_parser_quadratic_unfolding.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ def test_pathological_input_parses_quickly(payload):
# enough to never flake on slow CI while still catching a return of the
# quadratic behaviour.
assert time.perf_counter() - start < 5


def test_escaped_quotes_in_parameter_value_parse_quickly():
"""Each escaped quote in a quoted parameter value checks if it closes the
quoted section. Copying the rest of the line for this check made a value
with many escaped quotes quadratic.
"""
ics = (
'BEGIN:VCALENDAR\r\nVERSION:2.0\r\nX;X-P="'
+ '\\"' * 200_000
+ '":v\r\nEND:VCALENDAR\r\n'
)
start = time.perf_counter()
calendar = Calendar.from_ical(ics)
assert time.perf_counter() - start < 5
assert calendar["X"].params["X-P"] == '"' * 200_000
Loading