diff --git a/src/library_fs.js b/src/library_fs.js index 68c53ad670bb5..ada447d171c18 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -182,7 +182,7 @@ FS.staticInit(); // limit max consecutive symlinks to 40 (SYMLOOP_MAX). linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) { // split the absolute path - var parts = path.split('/').filter((p) => !!p && (p !== '.')); + var parts = path.split('/').filter((p) => !!p); // start at the root var current = FS.root; @@ -195,6 +195,10 @@ FS.staticInit(); break; } + if (parts[i] === '.') { + continue; + } + if (parts[i] === '..') { current_path = PATH.dirname(current_path); current = current.parent; @@ -665,9 +669,12 @@ FS.staticInit(); var lookup = FS.lookupPath(path, { parent: true }); var parent = lookup.node; var name = PATH.basename(path); - if (!name || name === '.' || name === '..') { + if (!name) { throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); } + if (name === '.' || name === '..') { + throw new FS.ErrnoError({{{ cDefs.EEXIST }}}); + } var errCode = FS.mayCreate(parent, name); if (errCode) { throw new FS.ErrnoError(errCode); diff --git a/test/fs/test_fs_mkdir_dotdot.c b/test/fs/test_fs_mkdir_dotdot.c new file mode 100644 index 0000000000000..4bda788dd452a --- /dev/null +++ b/test/fs/test_fs_mkdir_dotdot.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() { + assert(mkdir("test", 0777) == 0); + assert(mkdir("test/a", 0777) == 0); + assert(mkdir("test/a/..", 0777) == -1); + printf("error: %s\n", strerror(errno)); + assert(errno == EEXIST); + assert(mkdir("test/a/.", 0777) == -1); + printf("error: %s\n", strerror(errno)); + assert(errno == EEXIST); + assert(mkdir("test/a/b/..", 0777) == -1); + printf("error: %s\n", strerror(errno)); + assert(errno == ENOENT); + assert(mkdir("test/a/b/.", 0777) == -1); + printf("error: %s\n", strerror(errno)); + assert(errno == ENOENT); + printf("success\n"); +} diff --git a/test/test_core.py b/test/test_core.py index 87d04cfdc1615..cbdf449ec1d0e 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -5858,6 +5858,10 @@ def test_fs_rename_on_existing(self): self.set_setting('FORCE_FILESYSTEM') self.do_runf('fs/test_fs_rename_on_existing.c', 'success') + @also_with_nodefs_both + def test_fs_mkdir_dotdot(self): + self.do_runf('fs/test_fs_mkdir_dotdot.c', 'success') + def test_sigalrm(self): self.do_runf('test_sigalrm.c', 'Received alarm!') self.do_runf('test_sigalrm.c', 'Received alarm!', emcc_args=['-sEXIT_RUNTIME'])