Fix: path.getrelative() gives incorrect results for paths containing "//"
This commit is contained in:
parent
ffc53d870e
commit
8d8a3e3574
@ -8,6 +8,36 @@
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static void normalize(char* buffer, const char* path)
|
||||
{
|
||||
strcpy(buffer, path);
|
||||
do_translate(buffer, '/');
|
||||
|
||||
/* remove any duplicate slashes within the path */
|
||||
char* src = buffer;
|
||||
char* dst = buffer;
|
||||
char last = '\0';
|
||||
|
||||
while (*src != '\0') {
|
||||
/* if I don't have consecutive slashes, keep the char */
|
||||
if (*src != '/' || last != '/') {
|
||||
*(dst++) = *src;
|
||||
}
|
||||
|
||||
/* Allow double-slash at the start of the string, so absolute
|
||||
* UNC paths can be expressed, but nowhere else */
|
||||
if (src != buffer) {
|
||||
last = (*src);
|
||||
}
|
||||
|
||||
/* check the next one */
|
||||
++src;
|
||||
}
|
||||
|
||||
*dst = '\0';
|
||||
}
|
||||
|
||||
|
||||
int path_getrelative(lua_State* L)
|
||||
{
|
||||
int i, last, count;
|
||||
@ -18,10 +48,8 @@ int path_getrelative(lua_State* L)
|
||||
const char* p2 = luaL_checkstring(L, 2);
|
||||
|
||||
/* normalize the paths */
|
||||
strcpy(src, p1);
|
||||
do_translate(src, '/');
|
||||
strcpy(dst, p2);
|
||||
do_translate(dst, '/');
|
||||
normalize(src, p1);
|
||||
normalize(dst, p2);
|
||||
|
||||
/* same directory? */
|
||||
if (strcmp(src, dst) == 0) {
|
||||
|
@ -176,6 +176,14 @@
|
||||
test.isequal("/opt/include", path.getrelative("/home/me/src/project", "/opt/include"))
|
||||
end
|
||||
|
||||
function suite.ignoresExtraSlashes2()
|
||||
test.isequal("..", path.getrelative("/a//b/c","/a/b"))
|
||||
end
|
||||
|
||||
function suite.ignoresExtraSlashes3()
|
||||
test.isequal("..", path.getrelative("/a///b/c","/a/b"))
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- path.isabsolute() tests
|
||||
|
Loading…
Reference in New Issue
Block a user