From 777519226d033fdea9bcad043c01aa6539c8b71d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 6 Aug 2013 10:29:04 +0200 Subject: 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 --- sys-utils/blkdiscard.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'sys-utils/blkdiscard.c') diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c index 0e15c01b0..af9ed6630 100644 --- a/sys-utils/blkdiscard.c +++ b/sys-utils/blkdiscard.c @@ -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); -- cgit v1.2.3-55-g7522