Catch fstream exceptions

Resolves: rhbz#832497
This commit is contained in:
Jaroslav Škarvada 2012-07-04 10:09:35 +02:00
parent 19a3e71c14
commit 89070cf48e
2 changed files with 106 additions and 1 deletions

View File

@ -0,0 +1,98 @@
commit e3a0dce60a1ff1663dab0f91cc4dacf89b565dea
Author: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Date: Fri May 18 13:17:39 2012 +0300
catch fstream exceptions in lib routines
Catch possible fstream and traits_type exceptions in lib.
Reported-by: Lekensteyn <lekensteyn@gmail.com>
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
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;
}

View File

@ -1,6 +1,6 @@
Name: powertop
Version: 2.0
Release: 2%{?dist}
Release: 3%{?dist}
Summary: Power consumption monitor
Group: Applications/System
@ -17,6 +17,8 @@ Patch2: powertop-2.0-show-watts-only-if-discharging.patch
Patch3: powertop-2.0-valid-html-output.patch
# Patch for rhbz#823502, backported from upstream
Patch4: powertop-2.0-factor-out-powertop-init.patch
# Patch for rhbz#832497, backported from upstream
Patch5: powertop-2.0-catch-fstream-errors.patch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires: gettext, ncurses-devel, pciutils-devel, zlib-devel, libnl-devel
@ -31,6 +33,7 @@ computer use more power than necessary while it is idle.
%patch2 -p1 -b .show-watts-only-if-discharging
%patch3 -p1 -b .valid-html-output
%patch4 -p1 -b .factor-out-powertop-init
%patch5 -p1 -b .catch-fstream-errors
%build
%configure
@ -62,6 +65,10 @@ rm -rf %{buildroot}
#%{_mandir}/man8/powertop.8*
%changelog
* Wed Jul 4 2012 Jaroslav Škarvada <jskarvad@redhat.com> - 2.0-3
- Catch fstream exceptions
Resolves: rhbz#832497
* Mon May 21 2012 Jaroslav Škarvada <jskarvad@redhat.com> - 2.0-2
- Fixed segfault during calibration
Resolves: rhbz#823502