diff --git a/src/jp2image.cpp b/src/jp2image.cpp
index eb49d04..db04b6d 100644
--- a/src/jp2image.cpp
+++ b/src/jp2image.cpp
@@ -209,6 +209,7 @@ namespace Exiv2
                         throw Error(kerCorruptedMetadata);
                     }
                     boxFileTypeFound = true;
+                    enforce(box.length >= sizeof(box), Exiv2::kerCorruptedMetadata);
                     std::vector<byte> boxData(box.length - boxSize);
                     io_->read(boxData.data(), static_cast<long>(boxData.size()));
                     if (!Internal::isValidBoxFileType(boxData))
@@ -476,6 +477,7 @@ namespace Exiv2
                     case kJp2BoxTypeFileTypeBox: {
                         // This box shall immediately follow the JPEG 2000 Signature box
                         /// \todo  All files shall contain one and only one File Type box.
+                        enforce(box.length >= sizeof(box), Exiv2::kerCorruptedMetadata);
                         std::vector<byte> boxData(box.length - boxSize);
                         io_->read(boxData.data(), static_cast<long>(boxData.size()));
                         if (!Internal::isValidBoxFileType(boxData))
