All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [Powertop] [PATCH] catch fstream exceptions in lib routines (v2)
@ 2012-05-18 11:18 Sergey Senozhatsky
  0 siblings, 0 replies; 3+ messages in thread
From: Sergey Senozhatsky @ 2012-05-18 11:18 UTC (permalink / raw)
  To: powertop

[-- Attachment #1: Type: text/plain, Size: 2163 bytes --]


[PATCH] catch fstream exceptions in lib routines (v2)

Catch possible fstream and traits_type exceptions in lib.

V2: Thanks to Peter, he pointed to silly typo I've done in V1.

Reported-and-tested-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..0c2c1f1 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;
 }
 


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [Powertop] [PATCH] catch fstream exceptions in lib routines (v2)
@ 2012-05-21 19:20 Chris Ferron
  0 siblings, 0 replies; 3+ messages in thread
From: Chris Ferron @ 2012-05-21 19:20 UTC (permalink / raw)
  To: powertop

[-- Attachment #1: Type: text/plain, Size: 2473 bytes --]

patch accepted by hand merge from v1.
Thanks
-Chris


On 05/18/2012 04:18 AM, Sergey Senozhatsky wrote:
> [PATCH] catch fstream exceptions in lib routines (v2)
>
> Catch possible fstream and traits_type exceptions in lib.
>
> V2: Thanks to Peter, he pointed to silly typo I've done in V1.
>
> Reported-and-tested-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..0c2c1f1 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;
>   }
>
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Powertop] [PATCH] catch fstream exceptions in lib routines (v2)
@ 2012-05-21 21:05 Sergey Senozhatsky
  0 siblings, 0 replies; 3+ messages in thread
From: Sergey Senozhatsky @ 2012-05-21 21:05 UTC (permalink / raw)
  To: powertop

[-- Attachment #1: Type: text/plain, Size: 2669 bytes --]

On (05/21/12 12:20), Chris Ferron wrote:
> 
> patch accepted by hand merge from v1.
> Thanks
> -Chris
>

Thank you.

	-ss

> 
> On 05/18/2012 04:18 AM, Sergey Senozhatsky wrote:
> >[PATCH] catch fstream exceptions in lib routines (v2)
> >
> >Catch possible fstream and traits_type exceptions in lib.
> >
> >V2: Thanks to Peter, he pointed to silly typo I've done in V1.
> >
> >Reported-and-tested-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..0c2c1f1 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;
> >  }
> >
> >
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-05-21 21:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-18 11:18 [Powertop] [PATCH] catch fstream exceptions in lib routines (v2) Sergey Senozhatsky
  -- strict thread matches above, loose matches on Subject: below --
2012-05-21 19:20 Chris Ferron
2012-05-21 21:05 Sergey Senozhatsky

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.