Skip to content

chore: fix panic - #67

Merged
apbendi merged 2 commits into
mainfrom
panic-fix
Jul 25, 2026
Merged

chore: fix panic#67
apbendi merged 2 commits into
mainfrom
panic-fix

Conversation

@thelostone-mc

@thelostone-mc thelostone-mc commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Problem

offset_to_line in src/check/utils.rs converts a finding's source location to a line number for display. It iterated content.chars().enumerate()character indices — but compared them against start, which comes from solang Locs and is a byte offset. The two only coincide for pure-ASCII files, which is why this went unnoticed.

For files containing multi-byte UTF-8 (emoji, non-Latin comments), there are two symptoms:

  1. Panic: if a finding's byte offset exceeds the file's total character count (any finding near the end of a file with enough multi-byte content), the loop runs out of characters and hits unreachable!("content.len() > start"), crashing the entire scopelint check run:
    thread 'main' panicked at src/check/utils.rs:212:5:
    internal error: entered unreachable code: content.len() > start
    
    Repro: a file with a // 🚀🚀🚀... (300 emoji) comment followed by a naming violation.
  2. Wrong line numbers: with less multi-byte content the loop merely returns late, attributing findings to later lines than the truth (e.g. a violation on line 6 reported on line 8).

Fix

Iterate with char_indices() instead, which yields byte offsets, making the comparison byte-to-byte. The debug_assert!/unreachable! are replaced with a defensive fallback returning the last line when start is at or past EOF.

This was the only chars().enumerate() offset comparison in the codebase — other modules (e.g. inline_config.rs) already use char_indices() correctly.

thelostone-mc and others added 2 commits July 25, 2026 15:26
…on tests

Writing regression tests for the byte-vs-char fix surfaced a residual
off-by-one present in both the original code and the fix: the line
counter incremented before the offset comparison, so a finding whose
next character is a newline (e.g. a one-character identifier at end of
line) was reported one line late. Counting newlines strictly before the
offset removes the iteration-order subtlety entirely.

Tests cover ASCII offsets, multi-byte content before the offset (was
reported lines late), a byte offset beyond the file's char count (was
an unreachable! panic), and offsets at EOF.
@apbendi
apbendi marked this pull request as ready for review July 25, 2026 19:32
@apbendi
apbendi merged commit ac7d1f6 into main Jul 25, 2026
4 checks passed
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.

2 participants