Skip to content

Handle invalid UTF-8 in inline annotation comments - #3271

Merged
soutaro merged 1 commit into
ruby:masterfrom
Shopify:fix/invalid-utf8-inline-comments
Nov 9, 2026
Merged

soutaro merged 1 commit into
ruby:masterfrom
Shopify:fix/invalid-utf8-inline-comments

Conversation

@Morriar

@Morriar Morriar commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Prevent malformed UTF-8 in inline RBS comments from raising ArgumentError.

CommentBlock#trailing_annotation identified annotation comments using a regexp:

comment.location.slice.start_with?(/#[:\[]/)

For a comment containing malformed UTF-8, this operation raises before the annotation reaches the RBS parser:

String#start_with?: invalid byte sequence in UTF-8 (ArgumentError)

Fix

Use literal prefix matching:

comment.location.slice.start_with?("#:", "#[")

This check does not validate the rest of the comment as UTF-8. The malformed annotation therefore reaches the existing RBS lexer, which reports a parsing error. CommentBlock converts that error into an AnnotationSyntaxError diagnostic.

Reproduction

A Ruby source containing a literal invalid byte in an inline annotation comment previously raised ArgumentError:

class Foo
  attr_reader :value #: Integer # <0xDD>
end

Tests

Added an end-to-end Prism.parse → RBS::InlineParser.parse regression test covering:

  • 0xDD
  • 0xFF
  • lone 0xC3
  • truncated 0xE2 0x82

Each case now produces RBS::InlineParser::Diagnostic::AnnotationSyntaxError instead of raising ArgumentError.

@Morriar
Morriar marked this pull request as ready for review November 5, 2026 18:51

@soutaro soutaro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@soutaro
soutaro added this pull request to the merge queue Nov 9, 2026
Merged via the queue into ruby:master with commit 84bcc63 Nov 9, 2026
39 of 40 checks passed
@soutaro
soutaro deleted the fix/invalid-utf8-inline-comments branch November 9, 2026 03:46
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