blkdiscard: use fstat() rather than stat() [coverity scan]

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>
This commit is contained in:
Karel Zak 2013-08-06 10:29:04 +02:00
parent 21ce3f3a4e
commit 777519226d
1 changed files with 5 additions and 6 deletions

View 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);