changeset 6790:f45d29cd02df

added try/catch safeguards
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 17 May 2026 17:51:50 +0200
parents 261a3e91c870
children ebd42c3fb06a
files OrthancFramework/Sources/DataSource/DataSourceReader.cpp OrthancFramework/Sources/MultiThreading/ThreadPool.cpp OrthancServer/Resources/RunCppCheck-2.20.0.sh
diffstat 3 files changed, 43 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.cpp	Sun May 17 17:34:18 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceReader.cpp	Sun May 17 17:51:50 2026 +0200
@@ -70,48 +70,52 @@
       boost::shared_ptr<IDynamicObject> value;
       std::unique_ptr<OrthancException> error;
 
-      std::string cacheKey;
-      bool hasCacheKey = id_->GetCacheKey(cacheKey);
+      size_t size = 0;
 
-      if (cache_ && hasCacheKey)
+      try
       {
-        value = cache_->GetCachedValue(cacheKey);
-      }
+        std::string cacheKey;
+        bool hasCacheKey = id_->GetCacheKey(cacheKey);
 
-      if (!value)
-      {
-        try
+        if (cache_ && hasCacheKey)
+        {
+          value = cache_->GetCachedValue(cacheKey);
+        }
+
+        if (!value)
         {
           value.reset(source_.Load(*id_));
-        }
-        catch (OrthancException& e)
-        {
-          error.reset(new OrthancException(e));
-        }
-        catch (...)
-        {
-          error.reset(new OrthancException(ErrorCode_InternalError));
+
+          if (!error && !value)
+          {
+            error.reset(new OrthancException(ErrorCode_NullPointer));
+          }
+
+          if (!error && cache_ && hasCacheKey)
+          {
+            cache_->Store(cacheKey, value, source_.GetValueSize(*value));
+          }
         }
 
-        if (!error && !value)
+        if (!error)
         {
-          error.reset(new OrthancException(ErrorCode_NullPointer));
+          size = source_.GetValueSize(*value);
         }
-
-        if (!error && cache_ && hasCacheKey)
-        {
-          cache_->Store(cacheKey, value, source_.GetValueSize(*value));
-        }
+      }
+      catch (OrthancException& e)
+      {
+        error.reset(new OrthancException(e));
+      }
+      catch (...)
+      {
+        error.reset(new OrthancException(ErrorCode_InternalError));
       }
 
       // Phase 2: Acquire budget WITHOUT holding a strong reference to "DataSourceAnswer".
       // If the "DataSourceAnswer" has been dropped, "answer_.lock()" below will return NULL
       // and we will immediately "Release()" to revert this.
-      size_t size = 0;
-
       if (!error)
       {
-        size = source_.GetValueSize(*value);
         budget_->Acquire(size);   // may block; "DataSourceAnswer" CAN be destroyed here
       }
 
--- a/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Sun May 17 17:34:18 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Sun May 17 17:51:50 2026 +0200
@@ -233,7 +233,18 @@
 
       if (task.get() != NULL)
       {
-        dynamic_cast<ITask&>(*task).Execute();
+        try
+        {
+          dynamic_cast<ITask&>(*task).Execute();
+        }
+        catch (const OrthancException& e)
+        {
+          LOG(ERROR) << "Exception while executing a task: " << e.What();
+        }
+        catch (...)
+        {
+          LOG(ERROR) << "Native exception while executing a task";
+        }
       }
     }
   }
--- a/OrthancServer/Resources/RunCppCheck-2.20.0.sh	Sun May 17 17:34:18 2026 +0200
+++ b/OrthancServer/Resources/RunCppCheck-2.20.0.sh	Sun May 17 17:51:50 2026 +0200
@@ -10,7 +10,7 @@
 fi
 
 cat <<EOF > /tmp/cppcheck-suppressions.txt
-assertWithSideEffect:../../OrthancFramework/Sources/DataSource/DataSourceReader.cpp:208
+assertWithSideEffect:../../OrthancFramework/Sources/DataSource/DataSourceReader.cpp:212
 assertWithSideEffect:../../OrthancServer/Sources/Database/Compatibility/DatabaseLookup.cpp:292
 assertWithSideEffect:../../OrthancServer/Sources/Database/Compatibility/DatabaseLookup.cpp:391
 assertWithSideEffect:../../OrthancServer/Sources/ServerJobs/ResourceModificationJob.cpp:287