[dm-devel] [PATCH 30/39] kpartx: use fstat() when reading sysfs attributes
Hannes Reinecke
hare at suse.de
Thu Jun 16 09:47:36 UTC 2016
Coverity pointed out that we shouldn't be using stat()/open(),
as the file might have vanished or changed in between those
two calls. So modify it to open()/fstat() instead.
Signed-off-by: Hannes Reinecke <hare at suse.com>
---
kpartx/lopart.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/kpartx/lopart.c b/kpartx/lopart.c
index 5495e27..d4a2ab4 100644
--- a/kpartx/lopart.c
+++ b/kpartx/lopart.c
@@ -117,14 +117,16 @@ find_loop_by_file (const char * filename)
continue;
sprintf(dev, "/dev/%s", dent->d_name);
- if (stat (dev, &statbuf) != 0 ||
- !S_ISBLK(statbuf.st_mode))
- continue;
-
fd = open (dev, O_RDONLY);
if (fd < 0)
break;
+ if (fstat (fd, &statbuf) != 0 ||
+ !S_ISBLK(statbuf.st_mode)) {
+ close (fd);
+ continue;
+ }
+
if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
close (fd);
continue;
--
2.6.6
More information about the dm-devel
mailing list