Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [patch] Avoid race in zf_mkdir



On Thu, Oct 15, 2020 at 5:36 PM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> On Thu, Oct 15, 2020 at 5:29 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> >
> > - I'm paranoid about tests like "while (n--)", I prefer "while (n-- > 0)".
>
> Changed. (Not publishing the new patch yet. Will do so once this
> discussion concludes.)

Here's the fixed version. Will commit soon.

Roman.
diff --git a/Src/Modules/files.c b/Src/Modules/files.c
index 6d20e38a8..7ebacba6c 100644
--- a/Src/Modules/files.c
+++ b/Src/Modules/files.c
@@ -122,19 +122,29 @@ domkdir(char *nam, char *path, mode_t mode, int p)
 {
     int err;
     mode_t oumask;
+    struct stat st;
+    int n = 8;
     char const *rpath = unmeta(path);
 
-    if(p) {
-	struct stat st;
-
-	if(!stat(rpath, &st) && S_ISDIR(st.st_mode))
+    while(n-- > 0) {
+	oumask = umask(0);
+	err = mkdir(rpath, mode) ? errno : 0;
+	umask(oumask);
+	if (!err)
+	    return 0;
+	if(!p || err != EEXIST)
+	    break;
+	if(stat(rpath, &st)) {
+	    if(errno == ENOENT)
+		continue;
+	    err = errno;
+	    break;
+	}
+	if(S_ISDIR(st.st_mode))
 	    return 0;
+	break;
     }
-    oumask = umask(0);
-    err = mkdir(rpath, mode) ? errno : 0;
-    umask(oumask);
-    if(!err)
-	return 0;
+
     zwarnnam(nam, "cannot make directory `%s': %e", path, err);
     return 1;
 }


Messages sorted by: Reverse Date, Date, Thread, Author