changeset 1876:9b458e4484a1

truncation of serialized DICOM buffers if calcElementLength was overestimated
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Dec 2015 10:17:26 +0100
parents a5cd02894534
children 914c8f40cacb
files OrthancServer/FromDcmtkBridge.cpp
diffstat 1 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/FromDcmtkBridge.cpp	Mon Dec 07 17:23:57 2015 +0100
+++ b/OrthancServer/FromDcmtkBridge.cpp	Tue Dec 08 10:17:26 2015 +0100
@@ -1040,9 +1040,12 @@
     ff.removeInvalidGroups();
 
     // Create a memory buffer with the proper size
-    uint32_t s = ff.calcElementLength(xfer, encodingType);
-    buffer.resize(s);
-    DcmOutputBufferStream ob(&buffer[0], s);
+    {
+      const uint32_t estimatedSize = ff.calcElementLength(xfer, encodingType);  // (*)
+      buffer.resize(estimatedSize);
+    }
+
+    DcmOutputBufferStream ob(&buffer[0], buffer.size());
 
     // Fill the memory buffer with the meta-header and the dataset
     ff.transferInit();
@@ -1051,13 +1054,23 @@
                              /*opt_paddingType*/ EPD_withoutPadding);
     ff.transferEnd();
 
-    // Handle errors
     if (c.good())
     {
+      // The DICOM file is successfully written, truncate the target
+      // buffer if its size was overestimated by (*)
+      ob.flush();
+
+      size_t effectiveSize = static_cast<size_t>(ob.tell());
+      if (effectiveSize < buffer.size())
+      {
+        buffer.resize(effectiveSize);
+      }
+
       return true;
     }
     else
     {
+      // Error
       buffer.clear();
       return false;
     }