Skip to content

fix IndexError in DateTimeParser.parse with malformed format strings#1243

Open
Mr-Neutr0n wants to merge 1 commit intoarrow-py:masterfrom
Mr-Neutr0n:fix/parser-indexerror-malformed-fmt
Open

fix IndexError in DateTimeParser.parse with malformed format strings#1243
Mr-Neutr0n wants to merge 1 commit intoarrow-py:masterfrom
Mr-Neutr0n:fix/parser-indexerror-malformed-fmt

Conversation

@Mr-Neutr0n
Copy link
Copy Markdown

Malformed format strings can cause DateTimeParser.parse() to raise an IndexError instead of a ParserMatchError. This happens when the format string contains characters that, after regex escaping and bracket expression handling, produce a token list that doesn't align with the named groups in the generated regex pattern. match.group(token) then blows up with IndexError: no such group.

The fix wraps the match.group() calls in a try/except to catch IndexError and raise ParserMatchError instead, which is the expected exception type for parse failures.

Reproducer from #1191:

from arrow.parser import DateTimeParser
p = DateTimeParser()
p.parse("foo", "[-FFFFFFFFFFFFFFFFFFFFF-[-FFFFFFFFFFFF[ |||||||| 7   v.dG(dG\\][3zasks  &")
# Before: IndexError: no such group
# After: ParserMatchError: Failed to match '...' when parsing 'foo'.

Added a regression test. All 723 existing parser tests still pass.

Fixes #1191

when a malformed format string produces regex groups that don't match
the extracted token list, match.group(token) raises IndexError instead
of a proper ParserMatchError. catch IndexError in the token extraction
loop and raise ParserMatchError instead.

fixes arrow-py#1191
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (master@b247ad0). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             master     #1243   +/-   ##
==========================================
  Coverage          ?   100.00%           
==========================================
  Files             ?        10           
  Lines             ?      2318           
  Branches          ?       358           
==========================================
  Hits              ?      2318           
  Misses            ?         0           
  Partials          ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexError in DateTimeParser.parse

1 participant