diff --git a/src/host/path_normalize.c b/src/host/path_normalize.c index 64c5cf25..47de06fd 100644 --- a/src/host/path_normalize.c +++ b/src/host/path_normalize.c @@ -13,6 +13,7 @@ int path_normalize(lua_State* L) char buffer[0x4000]; char* src; char* dst; + char* ptr; char last; const char* path = luaL_checkstring(L, 1); @@ -30,6 +31,24 @@ int path_normalize(lua_State* L) ch = '/'; } + /* filter out .. */ + if (ch == '.' && last == '.') { + ptr = dst - 3; + while (ptr >= buffer) { + if (*ptr != '/') { + --ptr; + } + else { + dst = ptr; + break; + } + } + if (ptr >= buffer) { + ++src; + continue; + } + } + /* add to the result, filtering out duplicate slashes */ if (ch != '/' || last != '/') { *(dst++) = ch; diff --git a/tests/base/test_path.lua b/tests/base/test_path.lua index 31dc208b..cd3e7664 100755 --- a/tests/base/test_path.lua +++ b/tests/base/test_path.lua @@ -364,3 +364,28 @@ function suite.wildcards_escapeStarStar() test.isequal("Images/.*%.bmp", path.wildcards("Images/**.bmp")) end + + + +-- +-- path.normalize tests +-- + function suite.normalize_Test1() + local p = path.normalize("d:/game/../test") + test.isequal("d:/test", p) + end + + function suite.normalize_Test2() + local p = path.normalize("d:/game/../../test") + test.isequal("d:/../test", p) + end + + function suite.normalize_Test3() + local p = path.normalize("../../test") + test.isequal("../../test", 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")) + end \ No newline at end of file