From 557f30bdf11c5e2f1eb330f3c95e693aa7c4c716 Mon Sep 17 00:00:00 2001
From: Markus FRANK <Markus.Frank@cern.ch>
Date: Sun, 14 Mar 2021 14:27:32 +0100
Subject: [PATCH] Another Coverity battle

---
 DDCond/src/ConditionsRepository.cpp | 11 ++++++++---
 DDCore/src/Plugins.cpp              |  7 +++++--
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/DDCond/src/ConditionsRepository.cpp b/DDCond/src/ConditionsRepository.cpp
index a248a7bbb..49e2309f6 100644
--- a/DDCond/src/ConditionsRepository.cpp
+++ b/DDCond/src/ConditionsRepository.cpp
@@ -149,24 +149,29 @@ namespace {
 
   /// Load the repository from file and fill user passed data structory
   int readText(const string& input, ConditionsRepository::Data& data)    {
-    ConditionsRepository::Entry e;
     size_t   idx, siz_nam, siz_add, siz_tot;
-    char     sep, c, text[2*PATH_MAX+64];
+    char     sep, c, text[2*PATH_MAX];
+    ConditionsRepository::Entry e;
     ifstream in(input);
+
     in >> c >> c >> c >> c >> c >> c >> c >> sep 
        >> c >> siz_nam
        >> c >> siz_add
        >> c >> siz_tot;
     text[0] = 0;
+    siz_nam = std::min(siz_nam, 1024UL);
+    siz_add = std::min(siz_add, 1024UL);
     in.getline(text,sizeof(text),'\n');
+    text[sizeof(text)-1] = 0;
     do {
       text[0] = 0;
       in.getline(text,sizeof(text),'\n');
       if ( in.good() )  {
+	text[sizeof(text)-1] = 0;
         if ( siz_tot )  {
 	  text[8] = 0;
           // Direct access mode with fixed record size
-          if ( 9+siz_nam < (long)sizeof(text) )  {
+          if ( 9+siz_nam < sizeof(text) )  {
             text[9+siz_nam] = 0;
             e.name = text+9;
 	  }
diff --git a/DDCore/src/Plugins.cpp b/DDCore/src/Plugins.cpp
index 12ec54b76..d2a3d7f3e 100644
--- a/DDCore/src/Plugins.cpp
+++ b/DDCore/src/Plugins.cpp
@@ -105,7 +105,10 @@ namespace   {
 #endif
 #if defined(DD4HEP_PARSERS_NO_ROOT)
     handle = ::dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
-    if ( handle )   {
+    if ( !handle )   {
+      throw runtime_error("Failed to load plugin manager library: "+string(plugin_name));
+    }
+    else   {
       struct handle_guard  {
 	void* _handle {nullptr};
 	handle_guard(void* hdl) : _handle(hdl) {}
@@ -117,7 +120,7 @@ namespace   {
     if ( 0 != gSystem->Load(plugin_name) ) {}
 #endif
     getDebug = get_func< int (*) ()>(handle, plugin_name,MAKE_FUNC(getdebug,DD4HEP_PLUGINSVC_VERSION));
-  setDebug = get_func< int (*) (int)>(handle, plugin_name,MAKE_FUNC(setdebug,DD4HEP_PLUGINSVC_VERSION));
+    setDebug = get_func< int (*) (int)>(handle, plugin_name,MAKE_FUNC(setdebug,DD4HEP_PLUGINSVC_VERSION));
     create   = get_func< PluginService::stub_t (*)(const char*,
                                                    const char*)>(handle, plugin_name,MAKE_FUNC(create,DD4HEP_PLUGINSVC_VERSION));
     add      = get_func< void (*) (const char* identifier, 
-- 
GitLab