From cdb30491b8c9781fb53cea4fdc4f316a9a53015b Mon Sep 17 00:00:00 2001
From: Gerardo Tauriello <gerardo.tauriello@unibas.ch>
Date: Mon, 13 Aug 2018 15:10:27 +0200
Subject: [PATCH] Ensure that RuleBasedProcessor is never created with a
 None/NULL library.

---
 modules/conop/doc/compoundlib.rst            |  2 +-
 modules/conop/src/rule_based.cc              | 10 +++++--
 modules/conop/src/rule_based.hh              | 14 ++++++++--
 modules/conop/tests/test_rule_based_conop.cc | 29 +++++++++++++++-----
 4 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/modules/conop/doc/compoundlib.rst b/modules/conop/doc/compoundlib.rst
index caca8445b..41f859e7a 100644
--- a/modules/conop/doc/compoundlib.rst
+++ b/modules/conop/doc/compoundlib.rst
@@ -45,7 +45,7 @@ build the compound library manually.
       write mode, the programs can deadlock.
     :type readonly: :class:`bool`
     
-    :returns: The loaded compound lib
+    :returns: The loaded compound lib or None if it failed.
     
   .. staticmethod:: Create(database)
     
diff --git a/modules/conop/src/rule_based.cc b/modules/conop/src/rule_based.cc
index 6e8b712fe..fe2013e25 100644
--- a/modules/conop/src/rule_based.cc
+++ b/modules/conop/src/rule_based.cc
@@ -19,6 +19,7 @@
 #include <limits>
 #include <ost/log.hh>
 #include <ost/profile.hh>
+#include <ost/message.hh>
 #include <ost/mol/xcs_editor.hh>
 #include <ost/mol/bond_handle.hh>
 #include <ost/mol/torsion_handle.hh>
@@ -30,8 +31,6 @@
 
 namespace ost { namespace conop {
 
-
-
 void RuleBasedProcessor::DoProcess(DiagnosticsPtr diags, 
                                    mol::EntityHandle ent) const
 {
@@ -199,6 +198,11 @@ String RuleBasedProcessor::ToString() const {
   return ss.str();
 }
 
-
+void RuleBasedProcessor::_CheckLib() const {
+  if (!lib_) {
+    throw Error("Cannot initialize RuleBasedProcessor without a valid "
+                "CompoundLib object!");
+  }
+}
 
 }}
diff --git a/modules/conop/src/rule_based.hh b/modules/conop/src/rule_based.hh
index 3b91771a2..719ba7803 100644
--- a/modules/conop/src/rule_based.hh
+++ b/modules/conop/src/rule_based.hh
@@ -40,13 +40,19 @@ public:
     lib_(compound_lib), fix_element_(true), strict_hydrogens_(false), 
     unk_res_treatment_(CONOP_WARN), unk_atom_treatment_(CONOP_WARN)
   {
+    _CheckLib();
   }
 
-  RuleBasedProcessor(CompoundLibPtr compound_lib, bool fe, bool sh, ConopAction ur, 
-                     ConopAction ua, bool bf, bool at, bool cn, bool aa, ConopAction zo): 
+  RuleBasedProcessor(CompoundLibPtr compound_lib, bool fe, bool sh,
+                     ConopAction ur, ConopAction ua, bool bf, bool at, bool cn,
+                     bool aa, ConopAction zo): 
     Processor(bf, at, cn, aa, zo), lib_(compound_lib), fix_element_(fe), 
     strict_hydrogens_(sh), unk_res_treatment_(ur), 
-    unk_atom_treatment_(ua) {}
+    unk_atom_treatment_(ua)
+  {
+    _CheckLib();
+  }
+
   ConopAction GetUnkResidueTreatment() const {
     return unk_res_treatment_;
   }
@@ -91,6 +97,8 @@ protected:
   virtual void DoProcess(DiagnosticsPtr diags, 
                          mol::EntityHandle ent) const;
 private:
+  void _CheckLib() const;
+
   CompoundLibPtr lib_;
   bool fix_element_;
   bool strict_hydrogens_;
diff --git a/modules/conop/tests/test_rule_based_conop.cc b/modules/conop/tests/test_rule_based_conop.cc
index 6444695d9..1ce4ebe24 100644
--- a/modules/conop/tests/test_rule_based_conop.cc
+++ b/modules/conop/tests/test_rule_based_conop.cc
@@ -50,9 +50,24 @@ CompoundLibPtr load_lib()
 
 BOOST_AUTO_TEST_SUITE(conop);
 
+BOOST_AUTO_TEST_CASE(rule_based_init_check)
+{
+  CompoundLibPtr lib; // null ptr is return value when library loading failed
+  BOOST_CHECK_THROW(RuleBasedProcessor rbc1(lib), ost::Error);
+  BOOST_CHECK_THROW(RuleBasedProcessor rbc2(lib, true, false, CONOP_WARN,
+                                            CONOP_WARN, false, true, true, true,
+                                            CONOP_WARN), ost::Error);
+  lib = load_lib();
+  if (!lib) { return; }
+  BOOST_CHECK_NO_THROW(RuleBasedProcessor rbc3(lib));
+  BOOST_CHECK_NO_THROW(RuleBasedProcessor rbc4(lib, true, false, CONOP_WARN,
+                                               CONOP_WARN, false, true, true,
+                                               true, CONOP_WARN));
+}
+
 BOOST_AUTO_TEST_CASE(rule_based_set_get_flags)
 {
-   CompoundLibPtr lib=load_lib(); 
+   CompoundLibPtr lib = load_lib();
    if (!lib) { return; }
    RuleBasedProcessor rbc(lib);
    // check the defaults
@@ -76,7 +91,7 @@ BOOST_AUTO_TEST_CASE(rule_based_set_get_flags)
 
 BOOST_AUTO_TEST_CASE(rule_based_connect)
 {
-  CompoundLibPtr lib=load_lib();
+  CompoundLibPtr lib = load_lib();
   if (!lib) { return; }
   RuleBasedProcessor rbc(lib);
   EntityHandle ent = CreateEntity();
@@ -94,7 +109,7 @@ BOOST_AUTO_TEST_CASE(rule_based_connect)
 
 BOOST_AUTO_TEST_CASE(rule_based_unk_atoms)
 {
-  CompoundLibPtr lib = load_lib(); 
+  CompoundLibPtr lib = load_lib();
   if (!lib) { return; }
   RuleBasedProcessor rbc(lib);
   EntityHandle ent = CreateEntity();
@@ -125,7 +140,7 @@ BOOST_AUTO_TEST_CASE(rule_based_unk_atoms)
 
 BOOST_AUTO_TEST_CASE(guesses_elements_of_unknown_atoms)
 {
-  CompoundLibPtr lib = load_lib(); 
+  CompoundLibPtr lib = load_lib();
   if (!lib) { return; }
   RuleBasedProcessor rbc(lib);
   EntityHandle ent = CreateEntity();
@@ -142,7 +157,7 @@ BOOST_AUTO_TEST_CASE(guesses_elements_of_unknown_atoms)
 
 BOOST_AUTO_TEST_CASE(fills_properties_of_unknown_residues)
 {
-  CompoundLibPtr lib = load_lib(); 
+  CompoundLibPtr lib = load_lib();
   if (!lib) { return; }
   RuleBasedProcessor rbc(lib);
   EntityHandle ent = CreateEntity();
@@ -160,7 +175,7 @@ BOOST_AUTO_TEST_CASE(fills_properties_of_unknown_residues)
 BOOST_AUTO_TEST_CASE(connects_atoms_of_unknown_residues_based_on_distance) 
 {
 
-  CompoundLibPtr lib = load_lib(); 
+  CompoundLibPtr lib = load_lib();
   if (!lib) { return; }
   RuleBasedProcessor rbc(lib);
   EntityHandle ent = CreateEntity();
@@ -177,7 +192,7 @@ BOOST_AUTO_TEST_CASE(connects_atoms_of_unknown_residues_based_on_distance)
 
 BOOST_AUTO_TEST_CASE(rule_based_unk_res)
 {
-  CompoundLibPtr lib = load_lib(); 
+  CompoundLibPtr lib = load_lib();
   if (!lib) { return; }
   RuleBasedProcessor rbc(lib);
   EntityHandle ent = CreateEntity();
-- 
GitLab