diff options
| author | Aleksei Volkov <volkov.aa@phystech.edu> | 2023-10-31 01:39:43 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-30 19:39:43 -0300 |
| commit | b8a2710a2cefbddc5e6b494db186014b341412e4 (patch) | |
| tree | 137248c695babc7b37fccd42ebda44431a27277d | |
| parent | bb9d7ed38a092dde2b27e2adc7b386272f2890ee (diff) | |
| download | luarocks-b8a2710a2cefbddc5e6b494db186014b341412e4.tar.gz luarocks-b8a2710a2cefbddc5e6b494db186014b341412e4.tar.bz2 luarocks-b8a2710a2cefbddc5e6b494db186014b341412e4.zip | |
Fix problems in path normalisation algorithm (#1541)
Current implementation of path normalisation contains various flaws:
- Trailing `.` and `..` at the end of path don't get normalised,
- Path `/` turns into an empty string after normalisation.
This patch changes implementation of normalisation to fix these.
| -rw-r--r-- | spec/dir_spec.lua | 8 | ||||
| -rw-r--r-- | src/luarocks/core/dir.lua | 11 |
2 files changed, 15 insertions, 4 deletions
diff --git a/spec/dir_spec.lua b/spec/dir_spec.lua index 9f05c664..f6ef172e 100644 --- a/spec/dir_spec.lua +++ b/spec/dir_spec.lua | |||
| @@ -52,12 +52,20 @@ describe("luarocks.dir #unit", function() | |||
| 52 | it("strips unneeded /../ and /./", function() | 52 | it("strips unneeded /../ and /./", function() |
| 53 | assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt")) | 53 | assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt")) |
| 54 | assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt")) | 54 | assert.are.same("/some/dir/file.txt", dir.normalize("/../../../some/./foo/bar/.././../dir/bla/../file.txt")) |
| 55 | assert.are.same("/some/dir", dir.normalize("/../../../some/./foo/bar/.././../dir/./some/subdir/../..")) | ||
| 56 | assert.are.same("/some/dir", dir.normalize("/../../../some/./foo/bar/.././../dir/./.")) | ||
| 55 | end) | 57 | end) |
| 56 | it("respects relative paths", function() | 58 | it("respects relative paths", function() |
| 59 | assert.are.same(".", dir.normalize(".")) | ||
| 57 | assert.are.same("boo", dir.normalize("./boo")) | 60 | assert.are.same("boo", dir.normalize("./boo")) |
| 58 | assert.are.same("/boo", dir.normalize("/./boo")) | 61 | assert.are.same("/boo", dir.normalize("/./boo")) |
| 59 | assert.are.same("../../../../boo", dir.normalize("../../../hello/world/../../../boo")) | 62 | assert.are.same("../../../../boo", dir.normalize("../../../hello/world/../../../boo")) |
| 60 | end) | 63 | end) |
| 64 | it("respects root directory", function() | ||
| 65 | assert.are.same("/", dir.normalize("/")) | ||
| 66 | assert.are.same("/", dir.normalize("/////")) | ||
| 67 | assert.are.same("/", dir.normalize("/a/b/.././../c/./../../")) | ||
| 68 | end) | ||
| 61 | end) | 69 | end) |
| 62 | 70 | ||
| 63 | end) | 71 | end) |
diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.lua index 0da88997..46dbeafd 100644 --- a/src/luarocks/core/dir.lua +++ b/src/luarocks/core/dir.lua | |||
| @@ -63,6 +63,7 @@ function dir.normalize(name) | |||
| 63 | if pathname:match("^.:") then | 63 | if pathname:match("^.:") then |
| 64 | drive, pathname = pathname:match("^(.:)(.*)$") | 64 | drive, pathname = pathname:match("^(.:)(.*)$") |
| 65 | end | 65 | end |
| 66 | pathname = pathname .. "/" | ||
| 66 | for piece in pathname:gmatch("(.-)/") do | 67 | for piece in pathname:gmatch("(.-)/") do |
| 67 | if piece == ".." then | 68 | if piece == ".." then |
| 68 | local prev = pieces[#pieces] | 69 | local prev = pieces[#pieces] |
| @@ -75,11 +76,13 @@ function dir.normalize(name) | |||
| 75 | table.insert(pieces, piece) | 76 | table.insert(pieces, piece) |
| 76 | end | 77 | end |
| 77 | end | 78 | end |
| 78 | local basename = pathname:match("[^/]+$") | 79 | if #pieces == 0 then |
| 79 | if basename then | 80 | pathname = drive .. "." |
| 80 | table.insert(pieces, basename) | 81 | elseif #pieces == 1 and pieces[1] == "" then |
| 82 | pathname = drive .. "/" | ||
| 83 | else | ||
| 84 | pathname = drive .. table.concat(pieces, "/") | ||
| 81 | end | 85 | end |
| 82 | pathname = drive .. table.concat(pieces, "/") | ||
| 83 | if protocol ~= "file" then pathname = protocol .."://"..pathname end | 86 | if protocol ~= "file" then pathname = protocol .."://"..pathname end |
| 84 | return pathname | 87 | return pathname |
| 85 | end | 88 | end |
