From 480e211e5047c5fe020c25adc7e141b2463d7e64 Mon Sep 17 00:00:00 2001 From: shams Date: Sat, 14 Nov 2026 16:01:22 +0530 Subject: [PATCH 1/2] fix quadratic time of escaped quotes in closes_quoted_section closes_quoted_section sliced the rest of the content line on every call, and it runs once per escaped quote in a quoted parameter value, so parsing a value made of many \" pairs was quadratic. Iterate by index over the same range instead. AI: Claude Opus 5.5 found the quadratic scan while reviewing the parser, wrote the fix, the regression test and the change log entry, and benchmarked the before and after times. I reviewed and ran the changes. --- news/+escaped-quote-scan-linear.bugfix | 1 + src/icalendar/parser/parameter.py | 5 ++++- .../tests/test_parser_quadratic_unfolding.py | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 news/+escaped-quote-scan-linear.bugfix 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..3bb7636a9 100644 --- a/src/icalendar/parser/parameter.py +++ b/src/icalendar/parser/parameter.py @@ -162,7 +162,10 @@ 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 :]: + # index instead of slicing: a slice copies the rest of the line on every + # call, which is quadratic for a value with many escaped quotes + 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 From c4fca7944e529d04505d428a11ba25a655502e51 Mon Sep 17 00:00:00 2001 From: shams Date: Tue, 17 Nov 2026 11:13:11 +0530 Subject: [PATCH 2/2] explain why closes_quoted_section must not slice the line --- src/icalendar/parser/parameter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/icalendar/parser/parameter.py b/src/icalendar/parser/parameter.py index 3bb7636a9..d6393da27 100644 --- a/src/icalendar/parser/parameter.py +++ b/src/icalendar/parser/parameter.py @@ -162,8 +162,9 @@ 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. """ - # index instead of slicing: a slice copies the rest of the line on every - # call, which is quadratic for a value with many escaped quotes + # 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":