Project

General

Profile

Actions

Bug #22632

open

parse.y magic encoding comments accept NUL bytes

Bug #22632: parse.y magic encoding comments accept NUL bytes

Added by kddnewton (Kevin Newton) 2 days ago. Updated about 18 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:127689]

Description

I can't tell if this is intentional or not. It doesn't really seem like an issue either way, but I think it should reject NUL bytes as it's a bit odd.

# encoding: binary\0ignored

Updated by nobu (Nobuyoshi Nakada) 2 days ago Actions #1

This isn't limited to “magic comments”; NUL bytes within comments are generally ignored.
In this case, it's simply a delimiter.

I once tried to treat NUL bytes within comments as EOF, just as they are outside comments, but this attempt was abandoned because this behavior was being abused in RubyGems stub lines.

Updated by nobu (Nobuyoshi Nakada) 1 day ago Actions #2 [ruby-core:127709]

Now I think a SyntaxError would be better for such a line, while both of parse.y and prism accept it.

$ for x in parse.y prism; do echo $'# encoding: binary\0ignored' | ./ruby -vc --parser=$x -; done
ruby 4.1.0dev (2026-11-25T06:56:36Z master f63048e942) [arm64-darwin27]
Syntax OK
ruby 4.1.0dev (2026-11-25T06:56:36Z master f63048e942) +PRISM [arm64-darwin27]
Syntax OK

Shouldn't we make it so that the both raise a SyntaxError together?

Updated by nobu (Nobuyoshi Nakada) about 23 hours ago Actions #3 [ruby-core:127712]

Or do not consider such a line as a magic comment at all?

Updated by kddnewton (Kevin Newton) about 18 hours ago Actions #4 [ruby-core:127721]

My concern is just about the value of the magic comment. Basically if # encoding: binary\0ignored should result in Encoding.find("binary") or Encoding.find("binary\0ignored") with the second one being an ArgumentError. To me it seems like we should make it an ArgumentError because it can't find the encoding.

Actions

Also available in: PDF Atom