Actions
Bug #22632
openparse.y magic encoding comments accept NUL bytes
Bug #22632:
parse.y magic encoding comments accept NUL bytes
Actions
Added by kddnewton (Kevin Newton) 2 days ago. Updated about 16 hours ago.
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.
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?
Or do not consider such a line as a magic comment at all?
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.