Fix time of check/use Coverity report (and ignore another) in device utils.
authorMilan Broz <gmazyland@gmail.com>
Mon, 17 Dec 2012 14:19:57 +0000 (15:19 +0100)
committerMilan Broz <gmazyland@gmail.com>
Mon, 17 Dec 2012 14:19:57 +0000 (15:19 +0100)
lib/utils_device.c

index 2e13721..d3e848a 100644 (file)
@@ -44,20 +44,16 @@ static int device_ready(const char *device)
        int devfd, r = 0;
        struct stat st;
 
-       if(stat(device, &st) < 0) {
-               log_err(NULL, _("Device %s doesn't exist or access denied.\n"), device);
-               return -EINVAL;
-       }
-
-       if (!S_ISBLK(st.st_mode))
-               return S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
-
        log_dbg("Trying to open and read device %s.", device);
        devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
-       if(devfd < 0) {
-               log_err(NULL, _("Cannot open device %s.\n"), device);
+       if (devfd < 0) {
+               log_err(NULL, _("Device %s doesn't exist or access denied.\n"), device);
                return -EINVAL;
        }
+       if (fstat(devfd, &st) < 0)
+               r = -EINVAL;
+       else if (!S_ISBLK(st.st_mode))
+               r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
 
        close(devfd);
        return r;
@@ -282,6 +278,7 @@ static int device_info(struct device *device,
                flags |= O_EXCL;
 
        /* Try to open read-write to check whether it is a read-only device */
+       /* coverity[toctou] */
        fd = open(device->path, O_RDWR | flags);
        if (fd == -1 && errno == EROFS) {
                *readonly = 1;