Skip to content

Commit

Permalink
Fix File.join with empty path component (#5915)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and ysbaddaden committed Apr 4, 2018
1 parent 9662abe commit b5a3a65
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions spec/std/file_spec.cr
Expand Up @@ -306,6 +306,11 @@ describe "File" do
File.join(["foo", "bar", "baz"]).should eq("foo/bar/baz")
File.join(["foo", "//bar//", "baz///"]).should eq("foo//bar//baz///")
File.join(["/foo/", "/bar/", "/baz/"]).should eq("/foo/bar/baz/")
File.join(["", "foo"]).should eq("foo")
File.join(["foo", ""]).should eq("foo/")
File.join(["", "", "foo"]).should eq("foo")
File.join(["foo", "", "bar"]).should eq("foo/bar")
File.join(["foo", "", "", "bar"]).should eq("foo/bar")
end

it "chown" do
Expand Down
6 changes: 5 additions & 1 deletion src/dir.cr
Expand Up @@ -231,7 +231,11 @@ class Dir
return 0 if Dir.exists?(path)

components = path.split(File::SEPARATOR)
if components.first == "." || components.first == ""
case components.first
when ""
components.shift
subpath = "/"
when "."
subpath = components.shift
else
subpath = "."
Expand Down
8 changes: 6 additions & 2 deletions src/file.cr
Expand Up @@ -707,15 +707,17 @@ class File < IO::FileDescriptor
# ```
def self.join(parts : Array | Tuple) : String
String.build do |str|
first = true
parts.each_with_index do |part, index|
part.check_no_null_byte
next if part.empty? && index != parts.size - 1

str << SEPARATOR if index > 0
str << SEPARATOR unless first

byte_start = 0
byte_count = part.bytesize

if index > 0 && part.starts_with?(SEPARATOR)
if !first && part.starts_with?(SEPARATOR)
byte_start += 1
byte_count -= 1
end
Expand All @@ -725,6 +727,8 @@ class File < IO::FileDescriptor
end

str.write part.unsafe_byte_slice(byte_start, byte_count)

first = false
end
end
end
Expand Down

0 comments on commit b5a3a65

Please sign in to comment.