]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
blkdiscard: use fstat() rather than stat() [coverity scan]
authorKarel Zak <kzak@redhat.com>
Tue, 6 Aug 2013 08:29:04 +0000 (10:29 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 6 Aug 2013 08:29:04 +0000 (10:29 +0200)
 Calling function "open(char const *, int, ...)" that uses "path" after
 a check function. This can cause a time-of-check, time-of-use race
 condition.

.. well, in blkdiscard context it's mostly cosmetic change.

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/blkdiscard.c

index 0e15c01b0883ad09de6a67492c22f312d9b2fb06..af9ed663059e5767a4bd50937203ee304710baa5 100644 (file)
@@ -130,18 +130,17 @@ int main(int argc, char **argv)
                usage(stderr);
        }
 
-       if (stat(path, &sb) == -1)
-               err(EXIT_FAILURE, _("stat failed %s"), path);
-       if (!S_ISBLK(sb.st_mode))
-               errx(EXIT_FAILURE, _("%s: not a block device"), path);
-
        fd = open(path, O_WRONLY);
        if (fd < 0)
                err(EXIT_FAILURE, _("cannot open %s"), path);
 
+       if (fstat(fd, &sb) == -1)
+               err(EXIT_FAILURE, _("stat failed %s"), path);
+       if (!S_ISBLK(sb.st_mode))
+               errx(EXIT_FAILURE, _("%s: not a block device"), path);
+
        if (ioctl(fd, BLKGETSIZE64, &blksize))
                err(EXIT_FAILURE, _("%s: BLKGETSIZE64 ioctl failed"), path);
-
        if (ioctl(fd, BLKSSZGET, &secsize))
                err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path);