Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/prism/ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ def self.with_string(string)

# Yields a PrismSource to the given block, backed by a pm_source_t.
def self.with_file(filepath, options)
unless filepath.is_a?(String)
filepath = filepath.to_path if filepath.respond_to?(:to_path)
filepath = filepath.to_str if filepath.respond_to?(:to_str)
end
# File.path applies the same coercion and validation as the C
# extension's rb_get_path: it accepts to_path/to_str objects and
# rejects paths containing a NUL byte, which would otherwise truncate
# the path when it is handed to C as a NUL-terminated string.
filepath = File.path(filepath)
options[:filepath] = filepath
raise TypeError unless filepath.is_a?(String)

# On Windows and Mac, it's expected that filepaths will be encoded in
# UTF-8. If they are not, we need to convert them to UTF-8 before
Expand Down
8 changes: 8 additions & 0 deletions test/prism/api/parse_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def test_parse_file
end
end

def test_parse_file_null_byte
Dir.mktmpdir do |dir|
path = File.join(dir, "target.txt")
File.write(path, "# secret")
assert_raise(ArgumentError) { Prism.parse_file("#{path}\0.rb") }
end
end

def test_parse_tempfile
Tempfile.create(["test_parse_tempfile", ".rb"]) do |t|
t.puts ["begin\n", " end\n"]
Expand Down
Loading