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 <[email protected]>
---
 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

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

Reply via email to