[dm-devel] [PATCH 31/39] kpartx/lopart.c: use fstat() when reading device nodes

Hannes Reinecke hare at suse.de
Thu Jun 16 09:47:37 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 | 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




More information about the dm-devel mailing list