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 | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/kpartx/lopart.c b/kpartx/lopart.c
index d4a2ab4..f29cfc1 100644
--- a/kpartx/lopart.c
+++ b/kpartx/lopart.c
@@ -169,19 +169,18 @@ find_unused_loop_device (void)
 
                sprintf(dev, "/dev/loop%d", next_loop);
 
-               if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
-                       somedev++;
-                       fd = open (dev, O_RDONLY);
-
-                       if (fd >= 0) {
-
+               fd = open (dev, O_RDONLY);
+               if (fd >= 0) {
+                       if (fstat (fd, &statbuf) == 0 &&
+                           S_ISBLK(statbuf.st_mode)) {
+                               somedev++;
                                if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
                                        someloop++;             /* in use */
                                else if (errno == ENXIO)
                                        next_loop_dev = xstrdup(dev);
 
-                               close (fd);
                        }
+                       close (fd);
 
                        /* continue trying as long as devices exist */
                        continue;
-- 
2.6.6

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

Reply via email to