diff --git a/news/+escaped-quote-scan-linear.bugfix b/news/+escaped-quote-scan-linear.bugfix new file mode 100644 index 000000000..119514822 --- /dev/null +++ b/news/+escaped-quote-scan-linear.bugfix @@ -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 diff --git a/src/icalendar/parser/parameter.py b/src/icalendar/parser/parameter.py index 7168dd808..d6393da27 100644 --- a/src/icalendar/parser/parameter.py +++ b/src/icalendar/parser/parameter.py @@ -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 diff --git a/src/icalendar/tests/test_parser_quadratic_unfolding.py b/src/icalendar/tests/test_parser_quadratic_unfolding.py index c29cc9944..e1288fff1 100644 --- a/src/icalendar/tests/test_parser_quadratic_unfolding.py +++ b/src/icalendar/tests/test_parser_quadratic_unfolding.py @@ -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