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_line contains unclosed HTML tags at the end
  • There's a length mismatch between raw_line and rich_line due to HTML entity unescaping
  • The method attempts to access characters beyond the rich_line length

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_pos exceeds rich_line.length
  • Add nil checks in the HTML tag skipping loop to prevent infinite loops on unclosed tags
  • Add .compact call 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.rb

The 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

Merge request reports

Loading