Browse Source

[LibOS] Return ENOENT on open_namei(path = "")

Applications like OpenVINO sometimes do open("") and expect ENOENT.
This corner case is correctly handled by Linux, so Graphene must have
the exact same semantics. Previously, open("") under Graphene resulted
in success, and subsequent read() failed.
Dmitrii Kuvaiskii 6 years ago
parent
commit
efbd1d30a4
2 changed files with 13 additions and 2 deletions
  1. 5 0
      LibOS/shim/src/fs/shim_namei.c
  2. 8 2
      LibOS/shim/test/regression/fopen_cornercases.c

+ 5 - 0
LibOS/shim/src/fs/shim_namei.c

@@ -513,6 +513,11 @@ int open_namei (struct shim_handle * hdl, struct shim_dentry * start,
     int err = 0, newly_created = 0;
     struct shim_dentry *mydent = NULL;
 
+    if (*path == '\0') {
+        /* corner case: trying to open with empty filename */
+        return -ENOENT;
+    }
+
     lock(&dcache_lock);
 
     // lookup the path from start, passing flags

+ 8 - 2
LibOS/shim/test/regression/fopen_cornercases.c

@@ -20,9 +20,15 @@ int main(int argc, char** argv) {
 
     printf("filepath = %s (len = %lu)\n", filepath, strlen(filepath));
 
+    /* sanity check: try fopening empty filename (must fail) */
+    FILE* fp = fopen("", "r");
+    if (fp != NULL || errno != ENOENT) {
+        perror("(sanity check) fopen with empty filename did not fail with ENOENT");
+        return 1;
+    }
+
     /* sanity check: try fopening dir in write mode (must fail) */
-    errno    = 0;
-    FILE* fp = fopen(PATH, "w");
+    fp    = fopen(PATH, "w");
     if (fp != NULL || errno != EISDIR) {
         perror("(sanity check) fopen of dir with write access did not fail");
         return 1;