b326000
diff --git a/src/preview.cpp b/src/preview.cpp
b326000
index c34c8bd..69f8e01 100644
b326000
--- a/src/preview.cpp
b326000
+++ b/src/preview.cpp
b326000
@@ -36,6 +36,7 @@ EXIV2_RCSID("@(#) $Id$")
b326000
 
b326000
 #include "preview.hpp"
b326000
 #include "futils.hpp"
b326000
+#include "enforce.hpp"
b326000
 
b326000
 #include "image.hpp"
b326000
 #include "cr2image.hpp"
b326000
@@ -807,13 +808,14 @@ namespace {
b326000
                 else {
b326000
                     // FIXME: the buffer is probably copied twice, it should be optimized
b326000
                     DataBuf buf(size_);
b326000
-                    Exiv2::byte* pos = buf.pData_;
b326000
+                    uint32_t idxBuf = 0;
b326000
                     for (int i = 0; i < sizes.count(); i++) {
b326000
                         uint32_t offset = dataValue.toLong(i);
b326000
                         uint32_t size = sizes.toLong(i);
b326000
-                        if (offset + size <= static_cast<uint32_t>(io.size()))
b326000
-                            memcpy(pos, base + offset, size);
b326000
-                        pos += size;
b326000
+                        enforce(idxBuf + size < size_, kerCorruptedMetadata);
b326000
+                        if (size!=0 && offset + size <= static_cast<uint32_t>(io.size()))
b326000
+                            memcpy(&buf.pData_[idxBuf], base + offset, size);
b326000
+                        idxBuf += size;
b326000
                     }
b326000
                     dataValue.setDataArea(buf.pData_, buf.size_);
b326000
                 }