Skip to content

Commit 6bde8cd

Browse files
committed
Runtime: fix bug fake filesystem
1 parent c7d5f1e commit 6bde8cd

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

compiler/tests-compiler/sys_fs.ml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,36 @@ f (); Sys.chdir "/static"; f ()
275275
EXPECTED ERROR
276276
EXPECTED ERROR
277277
|}]
278+
279+
let%expect_test "check fake filesystem with path containing special regex chars" =
280+
compile_and_run
281+
{|
282+
let f () =
283+
let touch path =
284+
let oc = open_out path in
285+
close_out oc
286+
in
287+
Sys.mkdir "test.dir" 0o777;
288+
Sys.mkdir "test_dir" 0o777;
289+
touch "test.dir/dot";
290+
touch "test_dir/underscore";
291+
print_endline "test.dir:";
292+
Array.iter print_endline (Sys.readdir "test.dir");
293+
print_endline "test_dir:";
294+
Array.iter print_endline (Sys.readdir "test_dir");
295+
print_endline ""
296+
in
297+
f (); Sys.chdir "/static"; f ()
298+
|};
299+
[%expect
300+
{|
301+
test.dir:
302+
dot
303+
test_dir:
304+
underscore
305+
306+
test.dir:
307+
dot
308+
test_dir:
309+
underscore
310+
|}]

runtime/js/fs_fake.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ class MlFakeDevice {
149149

150150
rmdir(name, raise_unix) {
151151
var name_slash = name === "" ? "" : this.slash(name);
152-
var r = new RegExp("^" + name_slash + "([^/]+)");
153152
if (!this.exists(name))
154153
caml_raise_system_error(
155154
raise_unix,
@@ -167,7 +166,7 @@ class MlFakeDevice {
167166
this.nm(name),
168167
);
169168
for (var n in this.content) {
170-
if (n.match(r))
169+
if (n.startsWith(name_slash) && n !== name_slash)
171170
caml_raise_system_error(
172171
raise_unix,
173172
"ENOTEMPTY",
@@ -187,14 +186,17 @@ class MlFakeDevice {
187186
if (!this.is_dir(name)) {
188187
caml_raise_sys_error(name + ": Not a directory");
189188
}
190-
var r = new RegExp("^" + name_slash + "([^/]+)");
191189
var seen = {};
192190
var a = [];
193191
for (var n in this.content) {
194-
var m = n.match(r);
195-
if (m && !seen[m[1]]) {
196-
seen[m[1]] = true;
197-
a.push(m[1]);
192+
if (n.startsWith(name_slash) && n !== name_slash) {
193+
var last = n.indexOf("/", name_slash.length);
194+
if (last < 0) last = undefined;
195+
var m = n.slice(name_slash.length, last);
196+
if (m && !seen[m]) {
197+
seen[m] = true;
198+
a.push(m);
199+
}
198200
}
199201
}
200202
return a;

0 commit comments

Comments
 (0)