From: Karel Zak Date: Tue, 6 Aug 2013 08:29:04 +0000 (+0200) Subject: blkdiscard: use fstat() rather than stat() [coverity scan] X-Git-Tag: v2.24-rc1~398 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=777519226d033fdea9bcad043c01aa6539c8b71d;p=thirdparty%2Futil-linux.git 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 --- diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c index 0e15c01b08..af9ed66305 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);