[Powertop] [PATCH] catch fstream exceptions in lib routines

Sergey Senozhatsky <sergey.senozhatsky at gmail.com>
Newsgroups dev.linux.lists.powertop
Message-ID <20120518101739.GB3074@swordfish>
[PATCH] catch fstream exceptions in lib routines

Catch possible fstream and traits_type exceptions in lib.

Reported-by: Lekensteyn <lekensteyn(a)gmail.com>
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/lib.cpp |   54 +++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 15 deletions(-)

diff --git a/src/lib.cpp b/src/lib.cpp
index 53638dd..bcd809f 100644
--- a/src/lib.cpp
+++ b/src/lib.cpp
@@ -172,8 +172,13 @@ void write_sysfs(const string &filename, const string &value)
 	file.open(filename.c_str(), ios::out);
 	if (!file)
 		return;
-	file << value;
-	file.close();
+	try 
+	{
+		file << value;
+		file.close();
+	} catch (std::exception &exc) {
+		return;
+	}
 }
 
 int read_sysfs(const string &filename, bool *ok)
@@ -187,10 +192,17 @@ int read_sysfs(const string &filename, bool *ok)
 			*ok = false;
 		return 0;
 	}
-	file >> i;
+	try
+	{
+		file >> i;
+		if (ok)
+			*ok = true;
+	} catch (std::exception &exc) {
+		if (ok)
+			*ok = "false";
+		i = 0;
+	}
 	file.close();
-	if (ok)
-		*ok = true;
 	return i;
 }
 
@@ -203,11 +215,17 @@ string read_sysfs_string(const string &filename)
 	file.open(filename.c_str(), ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception &exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }
 
@@ -224,11 +242,17 @@ string read_sysfs_string(const char *format, const char *param)
 	file.open(filename, ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception &exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.