瀏覽代碼

Merge pull request #750 from pieper/749-tag-cache-on-bad-data

BUG: consistent return for value of uninitialized data
Jean-Christophe Fillion-Robin 7 年之前
父節點
當前提交
dc3ac0d186
共有 2 個文件被更改,包括 15 次插入10 次删除
  1. 10 2
      Libs/DICOM/Core/Testing/Cpp/ctkDICOMDatabaseTest7.cpp
  2. 5 8
      Libs/DICOM/Core/ctkDICOMDatabase.cpp

+ 10 - 2
Libs/DICOM/Core/Testing/Cpp/ctkDICOMDatabaseTest7.cpp

@@ -51,10 +51,18 @@ int ctkDICOMDatabaseTest7( int argc, char * argv [] )
     }
   std::cerr << "Database is in " << databaseDirectory.path().toStdString() << std::endl;
 
-  // try to call fileValue with bogus data - should not trigger and exception
+  // try to call fileValue with bogus data: invalid filename and arbitrary tag
+  // - should not trigger and exception
   // (see https://github.com/commontk/CTK/issues/706)
+  // - result should be empty string
+  // (see https://github.com/commontk/CTK/issues/749)
 
-  database.fileValue("/tmp/file-that-does-not-exist", "00ff,eeee");
+  QString result = database.fileValue("/tmp/file-that-does-not-exist", "00ff,eeee");
+  if (result != "")
+    {
+    std::cerr << "ctkDICOMDatabase::fileValue() failed for file that doesn't exist." << std::endl;
+    return EXIT_FAILURE;
+    }
 
   //
   // Close and clean up

+ 5 - 8
Libs/DICOM/Core/ctkDICOMDatabase.cpp

@@ -998,17 +998,14 @@ QString ctkDICOMDatabase::fileValue(const QString fileName, const unsigned short
 
   ctkDICOMItem dataset;
   dataset.InitializeFromFile(fileName);
-
-  if (dataset.IsInitialized())
-    {
-    DcmTagKey tagKey(group, element);
-    value = dataset.GetAllElementValuesAsString(tagKey);
-    }
-  else
+  if (!dataset.IsInitialized())
     {
-    value = TagNotInInstance;
+    logger.error( "File " + fileName + " could not be initialized.");
+    return "";
     }
 
+  DcmTagKey tagKey(group, element);
+  value = dataset.GetAllElementValuesAsString(tagKey);
   this->cacheTag(sopInstanceUID, tag, value);
   return value;
 }