[PATCH 1/8] fix error handling in get_bsg_major() return error code if the device file in /sys/class/bsg failed to open; use fscanf to read an integer major number directly (instead of reading and parsing the string), return error and print appropriate messages when scanf fails.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Alexander Nezhinsky <alexandern@xxxxxxxxxxxx>


Signed-off-by: Alexander Nezhinsky <alexandern@xxxxxxxxxxxx>
---
 usr/bs_sg.c |   18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/usr/bs_sg.c b/usr/bs_sg.c
index 4f78def..fe7dbec 100644
--- a/usr/bs_sg.c
+++ b/usr/bs_sg.c
@@ -301,19 +301,27 @@ static void bs_sg_cmd_complete(int fd, int events, void *data)
 static int get_bsg_major(char *path)
 {
 	FILE *devfd;
-	char majorno[8];
+	int majorno, n;
 	char dev[64];
 	char tmp[16];
 
 	sscanf(path, "/dev/bsg/%s", tmp);
 	sprintf(dev, "/sys/class/bsg/%s/dev", tmp);
 	devfd = fopen(dev, "r");
-	if (!devfd)
+	if (!devfd) {
+		eprintf("%s open failed errno: %d\n", dev, errno);
 		return -1;
-	fscanf(devfd, "%s:", majorno);
+	}
+	n = fscanf(devfd, "%d:", &majorno);
 	fclose(devfd);
-
-	return atoi(majorno);
+	if (n != 1) {
+		if (n < 0)
+			eprintf("reading major from %s failed errno: %d\n", dev, errno);
+		else
+			eprintf("reading major from %s failed: invalid input\n", dev);
+		return -1;
+	}
+	return majorno;
 }
 
 static int chk_sg_device(char *path)
-- 
1.7.9.6

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux