Repository : http://git.fedorahosted.org/cgit/firewalld-recode.git
On branch : master
commit fa1248f5291a2097e7296876a0ca79760fa15fcf Author: Daniel Kopecek dkopecek@redhat.com Date: Tue Jan 13 11:05:40 2015 +0100
Don't use exceptions for flow control in the FirewallD class
src/Daemon/ConfigFile.cpp | 5 +++ src/Daemon/ConfigFile.hpp | 3 +- src/Daemon/FirewallD.cpp | 79 ++++++++++++++++++++++----------------------- 3 files changed, 46 insertions(+), 41 deletions(-)
diff --git a/src/Daemon/ConfigFile.cpp b/src/Daemon/ConfigFile.cpp index 0fe94aa..9f32340 100644 --- a/src/Daemon/ConfigFile.cpp +++ b/src/Daemon/ConfigFile.cpp @@ -91,6 +91,11 @@ namespace firewalld _dirty = true; }
+ bool ConfigFile::hasSettingValue(const String& name) const + { + return (_settings.count(name) != 0); + } + void ConfigFile::parse() { String config_line; diff --git a/src/Daemon/ConfigFile.hpp b/src/Daemon/ConfigFile.hpp index 0bf7cc9..ce3c5a9 100644 --- a/src/Daemon/ConfigFile.hpp +++ b/src/Daemon/ConfigFile.hpp @@ -34,8 +34,9 @@ namespace firewalld void write(); void close();
- const String& getSettingValue(const String& name) const; void setSettingValue(const String& name, String& value); + bool hasSettingValue(const String& name) const; + const String& getSettingValue(const String& name) const;
protected: void parse(); diff --git a/src/Daemon/FirewallD.cpp b/src/Daemon/FirewallD.cpp index a77582a..40feef2 100644 --- a/src/Daemon/FirewallD.cpp +++ b/src/Daemon/FirewallD.cpp @@ -124,17 +124,13 @@ namespace firewalld _fw.setDefaultZoneName(_config.getSettingValue("DefaultZone"));
/* Set definitions dir paths */ - try { - String definition_dirs_setting = _config.getSettingValue("StaticDefinitionDirs"); + if (_config.hasSettingValue("StaticDefinitionDirs")) { + const String definition_dirs_setting = _config.getSettingValue("StaticDefinitionDirs"); tokenizeString(definition_dirs_setting, _definition_dirs, ":", /*trim_empty=*/true); - } catch(const std::out_of_range& ex) { - /* Ignore out-of-range exception */ } - try { - String definition_dirs_setting = _config.getSettingValue("DefinitionDirs"); + if (_config.hasSettingValue("DefinitionDirs")) { + const String definition_dirs_setting = _config.getSettingValue("DefinitionDirs"); tokenizeString(definition_dirs_setting, _definition_dirs, ":", /*trim_empty=*/true); - } catch(const std::out_of_range& ex) { - /* Ignore out-of-range exception */ } return; } @@ -161,34 +157,31 @@ namespace firewalld
Pointer<Service> FirewallD::lookupService(const UString& name) const { - try { + if (_defined_services.count(name) != 0) { return _defined_services.at(name); - } catch(const std::out_of_range& ex) { + } + else { return nullptr; - } catch(...) { - throw std::runtime_error("Unexpected exception"); } }
Pointer<Zone> FirewallD::lookupZone(const UString& name) const { - try { + if (_defined_zones.count(name) != 0) { return _defined_zones.at(name); - } catch(const std::out_of_range& ex) { + } + else { return nullptr; - } catch(...) { - throw std::runtime_error("Unexpected exception"); } }
Pointer<ICMPType> FirewallD::lookupICMPType(const UString& name) const { - try { + if (_defined_icmptypes.count(name) != 0) { return _defined_icmptypes.at(name); - } catch(const std::out_of_range& ex) { + } + else { return nullptr; - } catch(...) { - throw std::runtime_error("Unexpected exception"); } }
@@ -269,7 +262,6 @@ namespace firewalld void FirewallD::dbus_method_handle(const ustring& name, const DBusParameters& parameters, DBusResultBuilder& result) { LOG(TRACE) << "DBus method call: method=" << name; - try { /* * First check whether the method call should be handled by @@ -280,33 +272,40 @@ namespace firewalld */ if (Glib::str_has_prefix(name, _fw.backend().directDBusInterface() + ".")) { _fw.backend().handleDBusMethod(name, parameters, result); - } else { + } + else { /* * Lookup the method implementation in the DBus method table * and call it. * * Method implementations are registered in the constructor. */ - const DBusMethodPointer method = _dbus_method_table.at(name); - /* - * Make sure no other DBus method will be executed during execution - * of this one. - */ - std::lock_guardstd::mutex dbus_method_lock(_dbus_method_mutex); - /* - * This will call the method implementation on our instance of the - * FirewallD class - */ - (*this.*method)(parameters, result); - /* - * Catch bugs in method implementation. - */ - /* TODO: Assert: no zone is marked as dirty */ + if (_dbus_method_table.count(name) == 0) { + LOG(WARNING) << "Requested DBus method not found in the method table: " << name; + result = DBusError("NotImplemented", "The " + name + " method not implemented"); + } + else { + const DBusMethodPointer method = _dbus_method_table[name]; + /* + * Make sure no other DBus method will be executed during execution + * of this one. + */ + std::lock_guardstd::mutex dbus_method_lock(_dbus_method_mutex); + /* + * This will call the method implementation on our instance of the + * FirewallD class + */ + (*this.*method)(parameters, result); + /* + * Catch bugs in method implementation. + */ + /* TODO: Assert: no zone is marked as dirty */ + } } } - catch(const std::out_of_range& ex) { - LOG(WARNING) << "Requested DBus method not found in the method table: " << name; - result = DBusError("NotImplemented", "The " + name + " method is currently not implented"); + catch(const FirewallError& ex) { + LOG(WARNING) << "FirewallError caught at DBus method handler"; + result = DBusError(ex); }
return;