From 6d4584dbfe675b6ff975184a6411fd047992d0c7 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Fri, 10 Mar 2017 20:14:53 +0100
Subject: [PATCH] New round to kill coverity deficiencies.

---
 DDCond/src/ConditionsTextRepository.cpp           | 6 ++++--
 DDCond/src/plugins/ConditionsRepositoryParser.cpp | 3 ++-
 DDCore/include/DD4hep/Plugins.h                   | 2 +-
 DDCore/src/Plugins.cpp                            | 4 ++--
 DDDB/src/DDDBFileReader.cpp                       | 9 +++++----
 DDG4/src/Geant4Random.cpp                         | 2 +-
 DDG4/tpython/DDPython.cpp                         | 2 +-
 7 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/DDCond/src/ConditionsTextRepository.cpp b/DDCond/src/ConditionsTextRepository.cpp
index 1889c8c97..d9369c155 100644
--- a/DDCond/src/ConditionsTextRepository.cpp
+++ b/DDCond/src/ConditionsTextRepository.cpp
@@ -154,6 +154,8 @@ namespace {
       text[0] = 0;
       in.getline(text,sizeof(text),'\n');
       if ( in.good() )  {
+        size_t idx_nam = 9+siz_nam<sizeof(text)-1 ? 9+siz_nam : 0;
+        size_t idx_add = 10+siz_nam+siz_add<sizeof(text)-1 ? 10+siz_nam+siz_add : 0;
         if ( 9+siz_nam >= sizeof(text) )
           except("ConditionsTextRepository","Inconsistent input data in %s: %s -> (%lld,%lld,%lld)",
                  __FILE__, input.c_str(), siz_nam, siz_add, siz_tot);
@@ -162,9 +164,9 @@ namespace {
                  __FILE__, input.c_str(), siz_nam, siz_add, siz_tot);
         else if ( siz_tot )  {
           // Direct access mode with fixed record size
-          text[8]   = text[9+siz_nam] = text[10+siz_nam+siz_add] = 0;
+          text[8]   = text[idx_nam] = text[idx_add] = 0;
           e.name    = text+9;
-          e.address = text+10+siz_nam;  
+          e.address = text+idx_nam+1;  
           if ( (idx=e.name.find(' ')) != string::npos )
             e.name[idx]=0;
           if ( (idx=e.address.find(' ')) != string::npos )
diff --git a/DDCond/src/plugins/ConditionsRepositoryParser.cpp b/DDCond/src/plugins/ConditionsRepositoryParser.cpp
index fee0e8ca8..cefca3880 100644
--- a/DDCond/src/plugins/ConditionsRepositoryParser.cpp
+++ b/DDCond/src/plugins/ConditionsRepositoryParser.cpp
@@ -30,6 +30,7 @@
 
 // C/C++ include files
 #include <stdexcept>
+#include <climits>
 
 ///   DD4hep namespace declaration
 namespace DD4hep  {
@@ -209,7 +210,7 @@ namespace DD4hep {
   template <> void Converter<iov_type>::operator()(xml_h element) const {
     xml_dim_t e   = element;
     string    nam = e.nameStr();
-    size_t    id  = e.id();
+    size_t    id  = e.id() > 0 ? e.id() : INT_MAX;
     ConversionArg* arg  = _param<ConversionArg>();
     printout(s_parseLevel,"XMLConditions","++ Registering IOV type: [%d]: %s",int(id),nam.c_str());
     const IOVType* iov_type = arg->manager.registerIOVType(id,nam).second;
diff --git a/DDCore/include/DD4hep/Plugins.h b/DDCore/include/DD4hep/Plugins.h
index 4da6f87b3..588dad6da 100644
--- a/DDCore/include/DD4hep/Plugins.h
+++ b/DDCore/include/DD4hep/Plugins.h
@@ -77,7 +77,7 @@ namespace DD4hep {
   struct PluginDebug {
     int m_debug;
     /// Default constructor
-    PluginDebug(int dbg = 2);
+    PluginDebug(int dbg = 2)  noexcept(false);
     /// Default destructor
     ~PluginDebug();
     /// Helper to check factory existence
diff --git a/DDCore/src/Plugins.cpp b/DDCore/src/Plugins.cpp
index 5b69d6218..2251e754d 100644
--- a/DDCore/src/Plugins.cpp
+++ b/DDCore/src/Plugins.cpp
@@ -135,12 +135,12 @@ namespace   {
 }
 
 /// Default constructor
-PluginDebug::PluginDebug(int dbg) : m_debug(0) {
+PluginDebug::PluginDebug(int dbg)  noexcept(false) : m_debug(0) {
   m_debug = PluginInterface::instance().setDebug(dbg);
 }
 
 /// Default destructor
-PluginDebug::~PluginDebug() {
+PluginDebug::~PluginDebug()  {
   PluginInterface::instance().setDebug(m_debug);
 }
 
diff --git a/DDDB/src/DDDBFileReader.cpp b/DDDB/src/DDDBFileReader.cpp
index c8bef7ae4..3499c8da0 100644
--- a/DDDB/src/DDDBFileReader.cpp
+++ b/DDDB/src/DDDBFileReader.cpp
@@ -68,10 +68,10 @@ int DD4hep::DDDB::DDDBFileReader::getObject(const std::string& system_id,
                                             std::string& buffer)
 {
   std::string path = m_directory+system_id;
-  struct stat buff;
-  if ( 0 == ::stat(path.c_str(), &buff) )  {
-    int fid  = ::open(path.c_str(), O_RDONLY);
-    if ( fid > 0 )   {
+  int fid  = ::open(path.c_str(), O_RDONLY);
+  if ( fid != -1 )   {
+    struct stat buff;
+    if ( 0 == ::fstat(fid, &buff) )  {
       int done = 0, len = buff.st_size;
       char* b  = new char[len+1];
       b[0] = 0;
@@ -88,6 +88,7 @@ int DD4hep::DDDB::DDDBFileReader::getObject(const std::string& system_id,
         return 1;
       }
     }
+    ::close(fid);
   }
   return 0;
 }
diff --git a/DDG4/src/Geant4Random.cpp b/DDG4/src/Geant4Random.cpp
index 9e48e44b5..040f9d002 100644
--- a/DDG4/src/Geant4Random.cpp
+++ b/DDG4/src/Geant4Random.cpp
@@ -251,7 +251,7 @@ void Geant4Random::showStatus() const    {
   if ( gRandom != m_rootRandom )   {
     printP2("      Local TRandom: 0x%p  gRandom: 0x%p",m_rootRandom,gRandom);
   }
-  else if ( !m_engine )   {
+  if ( 0 == m_engine )   {
     error("   Geant4Random instance has not engine attached!");
     return;
   }
diff --git a/DDG4/tpython/DDPython.cpp b/DDG4/tpython/DDPython.cpp
index 980e06371..db7bf812b 100644
--- a/DDG4/tpython/DDPython.cpp
+++ b/DDG4/tpython/DDPython.cpp
@@ -100,7 +100,7 @@ namespace {
   }
 }
 
-DDPython::GILState::GILState(int)  {
+DDPython::GILState::GILState(int) : state(0) {
   if ( ::Py_IsInitialized() )  {
     PyGILState_STATE st = (PyGILState_STATE)::PyGILState_Ensure();
     state = (int)st;
-- 
GitLab