filter out /./ in path.join and path.normalize

This commit is contained in:
Tom van Dijck 2015-05-26 17:38:48 +00:00 committed by Damien Courtois
parent 812d374a87
commit b874b79597
3 changed files with 24 additions and 0 deletions

View File

@ -28,6 +28,12 @@ int path_join(lua_State* L)
part = luaL_checkstring(L, i);
len = strlen(part);
/* remove leading "./" */
while (strncmp(part, "./", 2) == 0) {
part += 2;
len -= 2;
}
/* remove trailing slashes */
while (len > 1 && part[len - 1] == '/') {
--len;

View File

@ -47,6 +47,15 @@ int path_normalize(lua_State* L)
}
}
/* filter out /./ */
if (ch == '/' && last == '.') {
ptr = dst - 2;
if (ptr >= buffer && ptr[0] == '/') {
dst = ptr;
continue;
}
}
/* add to the result, filtering out duplicate slashes */
if (ch != '/' || last != '/') {
*(dst++) = ch;

View File

@ -275,6 +275,10 @@
test.isequal("foo", path.join("p1/p2/p3", "../../../foo"))
end
function suite.join_ignoreLeadingDots()
test.isequal("p1/p2/foo", path.join("p1/p2", "././foo"))
end
function suite.join_OnUptoParentOfBase()
test.isequal("../../p1", path.join("p1/p2/p3/p4/p5/p6/p7/", "../../../../../../../../../p1"))
end
@ -429,6 +433,11 @@
test.isequal("..", p)
end
function suite.normalize_singleDot()
local p = path.normalize("../../generated/Protocol/External/BattlePay/./asterion.pb.cc")
test.isequal("../../generated/Protocol/External/BattlePay/asterion.pb.cc", p)
end
function suite.normalize()
test.isequal("d:/ProjectB/bin", path.normalize("d:/ProjectA/../ProjectB/bin"))
test.isequal("/ProjectB/bin", path.normalize("/ProjectA/../ProjectB/bin"))