Skip to contentSkip to conversation

lib/libf2fs: Added missing statements related to error checking.

PATCH In review Sotirios-Efstathios Maneas 1 participant 1 message Oct 11, 2018 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 lib/libf2fs: Added missing statements related to error checking.

The following patch adds a few missing statements related to error checking.

---
 lib/libf2fs.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
lib/libf2fs.c +15 -7
index a1f8beb..4ef9d7f 100644
@@ -541,9 +541,8 @@ const char *get_rootdev()
 #else
 	struct stat sb;
 	int fd, ret;
-	char buf[32];
+	char buf[32] = {0};
 	char *uevent, *ptr;
-
 	static char rootdev[PATH_MAX + 1];
 
 	if (stat("/", &sb) == -1)
@@ -553,7 +552,6 @@ const char *get_rootdev()
 		major(sb.st_dev), minor(sb.st_dev));
 
 	fd = open(buf, O_RDONLY);
-
 	if (fd < 0)
 		return NULL;
 
@@ -565,18 +563,29 @@ const char *get_rootdev()
 		return NULL;
 	}
 
-	uevent = malloc(ret + 1);
+	uevent = calloc(ret + 1, 1);
+	if (!uevent) {
+		close(fd);
+		return NULL;
+	}
- 	uevent[ret] = '\0';
 
 	ret = read(fd, uevent, ret);
 	close(fd);
+	if (ret <= 0) {
+		free(uevent);
+		return NULL;
+	}
 
 	ptr = strstr(uevent, "DEVNAME");
-	if (!ptr)
+	if (!ptr) {
+		free(uevent);
 		return NULL;
+	}
 
-	ret = sscanf(ptr, "DEVNAME=%s\n", buf);
+	sscanf(ptr, "DEVNAME=%s\n", buf);
 	snprintf(rootdev, PATH_MAX + 1, "/dev/%s", buf);
+	free(uevent);
 
 	return rootdev;
 #endif
Signature · 4 lines
2.10.1

Kind regards,
Stathis Maneas