Skip to content

Commit ae7ed4d

Browse files
committed
relative_to, proximate_to, is_subdir: emit warning if '..' component of input
this is ambiguous and different libraries handle this including incorrectly python.pathlib, C++ <filesystem>, java implementations,...
1 parent b1bb485 commit ae7ed4d

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

+stdlib/is_subdir.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
dir (1,1) string
77
end
88

9+
910
if ischar(subdir)
10-
s = strfind(subdir, dir) == 1 && (length(subdir) > length(dir)); %#ok<UNRCH>
11+
w = ~isempty(strfind(dir, "..")) || ~isempty(strfind(subdir, "..")); %#ok<STREMP,UNRCH>
12+
s = strfind(subdir, dir) == 1 && (length(subdir) > length(dir));
1113
else
14+
w = contains(dir, "..") || contains(subdir, "..");
1215
s = startsWith(subdir, dir) && (strlength(subdir) > strlength(dir));
1316
end
1417

18+
if ~strcmp(subdir, dir) && w
19+
warning("is_subdir: %s and/or %s is ambiguous input with '..' consider using stdlib.canonical() first", subdir, dir)
20+
end
21+
1522
end
1623

1724
%!assert(!is_subdir("/a/b", "/a/b"))
@@ -21,4 +28,4 @@
2128
%!assert(is_subdir("a/b", "a"))
2229
%!assert(!is_subdir("a", "a/.c"))
2330

24-
% this is incorrect on Windows at least%assert(is_subdir("a/b", "a/b/.."))
31+
% this is incorrect on Windows at least %assert(is_subdir("a/b", "a/b/.."))

+stdlib/relative_to.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
end
1717

1818
if stdlib.isoctave()
19+
w = ~isempty(strfind(b1, "..")); %#ok<STREMP>
1920
b = javaObject("java.io.File", b1).toPath();
2021
o = javaObject("java.io.File", o1).toPath();
2122
else
23+
w = contains(b1, "..");
2224
b = java.io.File(b1).toPath();
2325
o = java.io.File(o1).toPath();
2426
end
2527

28+
if w
29+
warning("relative_to(%s) is ambiguous base with '..' consider using stdlib.canonical() first", b1)
30+
end
31+
2632
try
2733
r = stdlib.posix(b.relativize(o).toString());
2834
catch e

test/TestFilePure.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
{"/a/b", "/a", ".."}, ...
9393
{"/a/b/c/d", "/a/b", "../.."}, ...
9494
{"this/one", "this/two", "../two"}};
95-
% NOTE: ".." in relative_to is ambiguous including for python.pathlib, C++ <filesystem>, etc.
95+
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.
9696

9797
p_proximate_to = p_relative_to;
9898

@@ -111,7 +111,7 @@
111111
{"c:/path", "d:/path", ""}}];
112112

113113
p_proximate_to = p_relative_to;
114-
% NOTE: ".." in proximate_to is ambiguous including for python.pathlib, C++ <filesystem>, etc
114+
% NOTE: ".." in proximate_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc
115115

116116
p_proximate_to{6}{3} = "/";
117117
p_proximate_to{10}{3} = "c";
@@ -156,7 +156,7 @@
156156
{"a/b", "a", true}, ...
157157
{"a/.c", "a", true}
158158
};
159-
% NOTE: ".." in is_subidr is ambiguous
159+
% NOTE: ".." in is_subdir (either argument) is ambiguous
160160

161161
if ispc
162162
p_is_subdir{end+1} = {"c:\", "c:/", false};

0 commit comments

Comments
 (0)