Line offset list monotonicity assertion is only checked on the resize slow path

Open Beginner friendly
#4,347 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
backend

Research direction

Read include/prism/internal/line_offset_list.h and src/line_offset_list.c, focusing on pm_line_offset_list_append and pm_line_offset_list_append_slow. Check how the assertion behaves for both fast and slow appends, then run the relevant line offset list tests. Done means duplicate or out-of-order offsets trigger the assertion in debug builds on either path.

Written by the indexing model from the issue text.

Description

Prism records the byte offset of the start of each source line in parser->line_offsets (pm_line_offset_list_t), which must remain strictly monotonically increasing so that binary search (pm_line_offset_list_line and pm_line_offset_list_line_column) can map byte offsets to 1-based line and column numbers.

Currently, the monotonicity assertion

PRISM_ASSERT(list->size == 0 || cursor > list->offsets[list->size - 1]);

is placed inside pm_line_offset_list_append_slow (src/line_offset_list.c) rather than the inline fast path pm_line_offset_list_append (include/prism/internal/line_offset_list.h). As a result, any duplicate or out-of-order line offset appended while list->size < list->capacity silently succeeds even in debug builds with PRISM_ASSERT enabled.

Reproduction

In #4346, we see on 774aebc1f

$ ruby -Ilib -rprism -e 'p Prism.parse("<<\"\#{0}\n\#{<<0}\\C-\n\#{0}\n\n").source.offsets'
# => [0, 8, 18, 23, 24, 23, 24]

For my mac os prism version, I see an abort:

ruby  -rprism -e 'p Prism.parse("<<\"\#{0}\n\#{<<0}\\C-\n\#{0}\n\n").source.offsets'
# => [0, 8, 18, 23, 24, 23, 24]
Assertion failed: (list->size == 0 || newline_offset > list->offsets[list->size - 1]), function pm_newline_list_append, file pm_newline_list.c, line 51.
-e:1: [BUG] Aborted at 0x000000018d7c1650
ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [arm64-darwin25]

-- Crash Report log information --------------------------------------------
   See Crash Report log file in one of the following locations:
     * ~/Library/Logs/DiagnosticReports
     * /Library/Logs/DiagnosticReports
   for more details.
Don't forget to include the above Crash Report log file in bug reports.
Dominant language
C
Stars
1k
Forks
202
Avg merge
6h 18m
Merged PRs (30d)
77

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby/prism

All issues in ruby/prism

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.