Patchwork [3/3] libmtd: fix segmentation fault on lib->mtd

login
register
mail settings
Submitter Brian Norris
Date Feb. 8, 2012, 9:26 p.m.
Message ID <1328736382-5798-3-git-send-email-computersforpeace@gmail.com>
Download mbox | patch
Permalink /patch/140264/
State Accepted
Commit 6571ef822aa7d782a8168d39a27fa3e65a3032be
Headers show

Comments

Brian Norris - Feb. 8, 2012, 9:26 p.m.
Legacy systems do not initialize lib->mtd, so we shouldn't perform
strlen(lib->mtd); this produces a segmentation fault. As this code isn't
used in the legacy codepath, we can just move it down to an 'else' branch.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
---
 lib/libmtd.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

Patch

diff --git a/lib/libmtd.c b/lib/libmtd.c
index 2bb4e57..fb4586c 100644
--- a/lib/libmtd.c
+++ b/lib/libmtd.c
@@ -644,13 +644,15 @@  void libmtd_close(libmtd_t desc)
 int mtd_dev_present(libmtd_t desc, int mtd_num) {
 	struct stat st;
 	struct libmtd *lib = (struct libmtd *)desc;
-	char file[strlen(lib->mtd) + 10];
 
 	if (!lib->sysfs_supported)
 		return legacy_dev_present(mtd_num);
+	else {
+		char file[strlen(lib->mtd) + 10];
 
-	sprintf(file, lib->mtd, mtd_num);
-	return !stat(file, &st);
+		sprintf(file, lib->mtd, mtd_num);
+		return !stat(file, &st);
+	}
 }
 
 int mtd_get_info(libmtd_t desc, struct mtd_info *info)