Coverity: TOCTOU: CID 387
authorNelson Ferreira <nelson.ferreira@ieee.org>
Wed, 22 Feb 2012 23:23:46 +0000 (18:23 -0500)
committerNelson Ferreira <nelson.ferreira@ieee.org>
Wed, 22 Feb 2012 23:23:46 +0000 (18:23 -0500)
* lib-src/make-path.c (touchy_mkdir): Try to avoid check/create
race condition by first attempting to create, then check any
error. If it is EEXISTS or directory already exists, then all
good.

Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
lib-src/make-path.c

index e1fbf60..6e4ef78 100644 (file)
@@ -39,15 +39,18 @@ static int touchy_mkdir(char *path)
 {
        struct stat buf;
 
-       /* If PATH already exists and is a directory, return success.  */
-       if (stat(path, &buf) >= 0 && (buf.st_mode & S_IFMT) == S_IFDIR)
-               return 0;
-
-       /* Otherwise, try to make it.  If PATH exists but isn't a directory,
+       /* Try to make it.  If PATH exists, EEXIST will be the
+          return. Anyway on error but isn't a directory,
           this will signal an error.  */
-       if (mkdir(path, 0777) < 0) {
-               fprintf(stderr, "%s: ", prog_name);
-               perror(path);
+       if (mkdir(path, 0777) < 0 && errno != EEXIST ) {
+               int serrno = errno; /* save errno because stat may
+                                      change it... */
+
+               /* If PATH already exists and is a directory, return success.  */
+               if (stat(path, &buf) >= 0 && (buf.st_mode & S_IFMT) == S_IFDIR)
+                       return 0;
+
+               fprintf(stderr, "%s: %s", prog_name, strerror(serrno));
                return 1;
        }