Skip to content

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

Open
Morriar wants to merge 1 commit into
ruby:masterfrom
Shopify:fix/invalid-utf8-inline-comments
Open

Morriar wants to merge 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
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.

1 participant