Fix range marker
What does this MR do and why?
Fixes an infinite loop and potential errors in Gitlab::StringRangeMarker#position_mapping that can occur when processing commit messages with concatenated URLs or email addresses without whitespace.
Contributes to #585096
Problem
The position_mapping method can enter an infinite loop or raise errors when:
rich_linecontains unclosed HTML tags at the end- There's a length mismatch between
raw_lineandrich_linedue to HTML entity unescaping - The method attempts to access characters beyond the
rich_linelength
This occurs during commit message rendering when processing text with concatenated URLs or email addresses without whitespace.
Solution
- Add bounds checking to break early when
rich_posexceedsrich_line.length - Add nil checks in the HTML tag skipping loop to prevent infinite loops on unclosed tags
- Add
.compactcall to filter out nil values from position mappings
How to set up and validate locally
Run the new test cases:
bundle exec rspec spec/lib/gitlab/string_range_marker_spec.rbThe tests cover:
- Length mismatch between raw and rich lines due to HTML entity unescaping
- Rich line ending before raw line iteration completes
- Unclosed HTML tags at the end of rich line
- HTML entities creating boundary conditions
Edited by Vasilii Iakliushin