b326000
diff --git a/src/actions.cpp b/src/actions.cpp
b326000
index 0ebe850..3cd398e 100644
b326000
--- a/src/actions.cpp
b326000
+++ b/src/actions.cpp
b326000
@@ -59,6 +59,7 @@ EXIV2_RCSID("@(#) $Id$")
b326000
 #include <ctime>
b326000
 #include <cmath>
b326000
 #include <cassert>
b326000
+#include <stdexcept>
b326000
 #include <sys/types.h>                  // for stat()
b326000
 #include <sys/stat.h>                   // for stat()
b326000
 #ifdef EXV_HAVE_UNISTD_H
b326000
@@ -236,33 +237,43 @@ namespace Action {
b326000
     }
b326000
b326000
     int Print::run(const std::string& path)
b326000
-    try {
b326000
-        path_ = path;
b326000
-        int rc = 0;
b326000
-        Exiv2::PrintStructureOption option = Exiv2::kpsNone ;
b326000
-        switch (Params::instance().printMode_) {
b326000
-            case Params::pmSummary:   rc = printSummary();     break;
b326000
-            case Params::pmList:      rc = printList();        break;
b326000
-            case Params::pmComment:   rc = printComment();     break;
b326000
-            case Params::pmPreview:   rc = printPreviewList(); break;
b326000
-            case Params::pmStructure: rc = printStructure(std::cout,Exiv2::kpsBasic)     ; break;
b326000
-            case Params::pmRecursive: rc = printStructure(std::cout,Exiv2::kpsRecursive) ; break;
b326000
-
b326000
-            case Params::pmXMP:
b326000
-                 option = option == Exiv2::kpsNone ? Exiv2::kpsXMP        : option;  // drop
b326000
-            case Params::pmIccProfile:{
b326000
-                 option = option == Exiv2::kpsNone ? Exiv2::kpsIccProfile : option;
b326000
-                 _setmode(_fileno(stdout),O_BINARY);
b326000
-                 rc = printStructure(std::cout,option);
b326000
-            } break;
b326000
+    {
b326000
+        try {
b326000
+            path_ = path;
b326000
+            int rc = 0;
b326000
+            Exiv2::PrintStructureOption option = Exiv2::kpsNone ;
b326000
+            switch (Params::instance().printMode_) {
b326000
+                case Params::pmSummary:   rc = printSummary();     break;
b326000
+                case Params::pmList:      rc = printList();        break;
b326000
+                case Params::pmComment:   rc = printComment();     break;
b326000
+                case Params::pmPreview:   rc = printPreviewList(); break;
b326000
+                case Params::pmStructure: rc = printStructure(std::cout,Exiv2::kpsBasic)     ; break;
b326000
+                case Params::pmRecursive: rc = printStructure(std::cout,Exiv2::kpsRecursive) ; break;
b326000
+
b326000
+                case Params::pmXMP:
b326000
+                    if (option == Exiv2::kpsNone)
b326000
+                        option = Exiv2::kpsXMP;
b326000
+                    // drop
b326000
+                case Params::pmIccProfile:
b326000
+                    if (option == Exiv2::kpsNone)
b326000
+                        option = Exiv2::kpsIccProfile;
b326000
+                    _setmode(_fileno(stdout),O_BINARY);
b326000
+                    rc = printStructure(std::cout,option);
b326000
+                    break;
b326000
+            }
b326000
+            return rc;
b326000
         }
b326000
-        return rc;
b326000
-    }
b326000
-    catch(const Exiv2::AnyError& e) {
b326000
-        std::cerr << "Exiv2 exception in print action for file "
b326000
-                  << path << ":\n" << e << "\n";
b326000
-        return 1;
b326000
-    } // Print::run
b326000
+        catch(const Exiv2::AnyError& e) {
b326000
+            std::cerr << "Exiv2 exception in print action for file "
b326000
+                      << path << ":\n" << e << "\n";
b326000
+            return 1;
b326000
+        }
b326000
+        catch(const std::overflow_error& e) {
b326000
+            std::cerr << "std::overflow_error exception in print action for file "
b326000
+                      << path << ":\n" << e.what() << "\n";
b326000
+            return 1;
b326000
+        }
b326000
+    }
b326000
b326000
     int Print::printStructure(std::ostream& out, Exiv2::PrintStructureOption option)
b326000
     {
b326000
diff --git a/src/error.cpp b/src/error.cpp
b326000
index e90a9c0..5d63957 100644
b326000
--- a/src/error.cpp
b326000
+++ b/src/error.cpp
b326000
@@ -109,6 +109,8 @@ namespace {
b326000
         { 55, N_("tiff directory length is too large") },
b326000
         { 56, N_("invalid type value detected in Image::printIFDStructure") },
b326000
         { 57, N_("invalid memory allocation request") },
b326000
+        { 58, N_("corrupted image metadata") },
b326000
+        { 59, N_("Arithmetic operation overflow") },
b326000
     };
b326000
b326000
 }
b326000
diff --git a/src/nikonmn.cpp b/src/nikonmn.cpp
b326000
index 571ab80..34bf601 100644
b326000
--- a/src/nikonmn.cpp
b326000
+++ b/src/nikonmn.cpp
b326000
@@ -299,6 +299,8 @@ namespace Exiv2 {
b326000
                                                const Value& value,
b326000
                                                const ExifData* exifData)
b326000
     {
b326000
+        if ( ! exifData ) return os << "undefined" ;
b326000
+
b326000
         if ( value.count() >= 9 ) {
b326000
             ByteOrder bo = getKeyString("Exif.MakerNote.ByteOrder",exifData) == "MM" ? bigEndian : littleEndian;
b326000
             byte      p[4];
b326000
diff --git a/src/pentaxmn.cpp b/src/pentaxmn.cpp
b326000
index 4fc38be..b22cb43 100644
b326000
--- a/src/pentaxmn.cpp
b326000
+++ b/src/pentaxmn.cpp
b326000
@@ -1167,6 +1167,8 @@ namespace Exiv2 {
b326000
b326000
     std::ostream& PentaxMakerNote::printShutterCount(std::ostream& os, const Value& value, const ExifData* metadata)
b326000
     {
b326000
+        if ( ! metadata ) return os << "undefined" ;
b326000
+
b326000
         ExifData::const_iterator dateIt = metadata->findKey(
b326000
                 ExifKey("Exif.PentaxDng.Date"));
b326000
         if (dateIt == metadata->end()) {
b326000
diff --git a/src/pngchunk.cpp b/src/pngchunk.cpp
b326000
index da4ccd0..4dcca4d 100644
b326000
--- a/src/pngchunk.cpp
b326000
+++ b/src/pngchunk.cpp
b326000
@@ -68,6 +68,8 @@ namespace Exiv2 {
b326000
                                    int*           outWidth,
b326000
                                    int*           outHeight)
b326000
     {
b326000
+        assert(data.size_ >= 8);
b326000
+
b326000
         // Extract image width and height from IHDR chunk.
b326000
b326000
         *outWidth  = getLong((const byte*)data.pData_,     bigEndian);
b326000
diff --git a/src/pngimage.cpp b/src/pngimage.cpp
b326000
index 11b4198..ed7399a 100644
b326000
--- a/src/pngimage.cpp
b326000
+++ b/src/pngimage.cpp
b326000
@@ -441,7 +441,9 @@ namespace Exiv2 {
b326000
 #ifdef DEBUG
b326000
                     std::cout << "Exiv2::PngImage::readMetadata: Found IHDR chunk (length: " << dataOffset << ")\n";
b326000
 #endif
b326000
-                    PngChunk::decodeIHDRChunk(cdataBuf, &pixelWidth_, &pixelHeight_);
b326000
+                    if (cdataBuf.size_ >= 8) {
b326000
+                        PngChunk::decodeIHDRChunk(cdataBuf, &pixelWidth_, &pixelHeight_);
b326000
+                    }
b326000
                 }
b326000
                 else if (!memcmp(cheaderBuf.pData_ + 4, "tEXt", 4))
b326000
                 {
b326000
diff --git a/src/tiffvisitor.cpp b/src/tiffvisitor.cpp
b326000
index 74f8d07..fad39b6 100644
b326000
--- a/src/tiffvisitor.cpp
b326000
+++ b/src/tiffvisitor.cpp
b326000
@@ -1493,6 +1493,11 @@ namespace Exiv2 {
b326000
         }
b326000
         p += 4;
b326000
         uint32_t isize= 0; // size of Exif.Sony1.PreviewImage
b326000
+
b326000
+        if (count > std::numeric_limits<uint32_t>::max() / typeSize) {
b326000
+            throw Error(59);
b326000
+        }
b326000
+
b326000
         uint32_t size = typeSize * count;
b326000
         uint32_t offset = getLong(p, byteOrder());
b326000
         byte* pData = p;
b326000
@@ -1536,7 +1541,9 @@ namespace Exiv2 {
b326000
             }
b326000
         }
b326000
         Value::AutoPtr v = Value::create(typeId);
b326000
-        assert(v.get());
b326000
+        if (!v.get()) {
b326000
+            throw Error(58);
b326000
+        }
b326000
         if ( !isize ) {
b326000
         	v->read(pData, size, byteOrder());
b326000
         } else {