# HG changeset patch # User Sebastien Jodogne # Date 1449566246 -3600 # Node ID 9b458e4484a12d43d638180ffc570c85881981c5 # Parent a5cd02894534d2899e08556f1cb87c5508aeb645 truncation of serialized DICOM buffers if calcElementLength was overestimated diff -r a5cd02894534 -r 9b458e4484a1 OrthancServer/FromDcmtkBridge.cpp --- 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(ob.tell()); + if (effectiveSize < buffer.size()) + { + buffer.resize(effectiveSize); + } + return true; } else { + // Error buffer.clear(); return false; }