Blame SOURCES/exiv2-CVE-2018-10958.patch

240d3a
diff --git a/include/exiv2/error.hpp b/include/exiv2/error.hpp
240d3a
index 24a70bf6..cc67725b 100644
240d3a
--- a/include/exiv2/error.hpp
240d3a
+++ b/include/exiv2/error.hpp
240d3a
@@ -192,6 +192,74 @@ namespace Exiv2 {
240d3a
         return os << error.what();
240d3a
     }
240d3a
240d3a
+    //! Complete list of all Exiv2 error codes
240d3a
+    enum ErrorCode {
240d3a
+        kerGeneralError = -1,
240d3a
+        kerSuccess = 0,
240d3a
+        kerErrorMessage,
240d3a
+        kerCallFailed,
240d3a
+        kerNotAnImage,
240d3a
+        kerInvalidDataset,
240d3a
+        kerInvalidRecord,
240d3a
+        kerInvalidKey,
240d3a
+        kerInvalidTag,
240d3a
+        kerValueNotSet,
240d3a
+        kerDataSourceOpenFailed,
240d3a
+        kerFileOpenFailed,
240d3a
+        kerFileContainsUnknownImageType,
240d3a
+        kerMemoryContainsUnknownImageType,
240d3a
+        kerUnsupportedImageType,
240d3a
+        kerFailedToReadImageData,
240d3a
+        kerNotAJpeg,
240d3a
+        kerFailedToMapFileForReadWrite,
240d3a
+        kerFileRenameFailed,
240d3a
+        kerTransferFailed,
240d3a
+        kerMemoryTransferFailed,
240d3a
+        kerInputDataReadFailed,
240d3a
+        kerImageWriteFailed,
240d3a
+        kerNoImageInInputData,
240d3a
+        kerInvalidIfdId,
240d3a
+        //! Entry::setValue: Value too large
240d3a
+        kerValueTooLarge,
240d3a
+        //! Entry::setDataArea: Value too large
240d3a
+        kerDataAreaValueTooLarge,
240d3a
+        kerOffsetOutOfRange,
240d3a
+        kerUnsupportedDataAreaOffsetType,
240d3a
+        kerInvalidCharset,
240d3a
+        kerUnsupportedDateFormat,
240d3a
+        kerUnsupportedTimeFormat,
240d3a
+        kerWritingImageFormatUnsupported,
240d3a
+        kerInvalidSettingForImage,
240d3a
+        kerNotACrwImage,
240d3a
+        kerFunctionNotSupported,
240d3a
+        kerNoNamespaceInfoForXmpPrefix,
240d3a
+        kerNoPrefixForNamespace,
240d3a
+        kerTooLargeJpegSegment,
240d3a
+        kerUnhandledXmpdatum,
240d3a
+        kerUnhandledXmpNode,
240d3a
+        kerXMPToolkitError,
240d3a
+        kerDecodeLangAltPropertyFailed,
240d3a
+        kerDecodeLangAltQualifierFailed,
240d3a
+        kerEncodeLangAltPropertyFailed,
240d3a
+        kerPropertyNameIdentificationFailed,
240d3a
+        kerSchemaNamespaceNotRegistered,
240d3a
+        kerNoNamespaceForPrefix,
240d3a
+        kerAliasesNotSupported,
240d3a
+        kerInvalidXmpText,
240d3a
+        kerTooManyTiffDirectoryEntries,
240d3a
+        kerMultipleTiffArrayElementTagsInDirectory,
240d3a
+        kerWrongTiffArrayElementTagType,
240d3a
+        kerInvalidKeyXmpValue,
240d3a
+        kerInvalidIccProfile,
240d3a
+        kerInvalidXMP,
240d3a
+        kerTiffDirectoryTooLarge,
240d3a
+        kerInvalidTypeValue,
240d3a
+        kerInvalidMalloc,
240d3a
+        kerCorruptedMetadata,
240d3a
+        kerArithmeticOverflow,
240d3a
+        kerMallocFailed,
240d3a
+    };
240d3a
+
240d3a
     /*!
240d3a
       @brief Simple error class used for exceptions. An output operator is
240d3a
              provided to print errors to a stream.
240d3a
240d3a
diff --git a/src/enforce.hpp b/src/enforce.hpp
240d3a
new file mode 100644
240d3a
index 00000000..b2d77eea
240d3a
--- /dev/null
240d3a
+++ b/src/enforce.hpp
240d3a
@@ -0,0 +1,96 @@
240d3a
+// ********************************************************* -*- C++ -*-
240d3a
+/*
240d3a
+ * Copyright (C) 2004-2018 Exiv2 maintainers
240d3a
+ *
240d3a
+ * This program is part of the Exiv2 distribution.
240d3a
+ *
240d3a
+ * This program is free software; you can redistribute it and/or
240d3a
+ * modify it under the terms of the GNU General Public License
240d3a
+ * as published by the Free Software Foundation; either version 2
240d3a
+ * of the License, or (at your option) any later version.
240d3a
+ *
240d3a
+ * This program is distributed in the hope that it will be useful,
240d3a
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
240d3a
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
240d3a
+ * GNU General Public License for more details.
240d3a
+ *
240d3a
+ * You should have received a copy of the GNU General Public License
240d3a
+ * along with this program; if not, write to the Free Software
240d3a
+ * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
240d3a
+ */
240d3a
+/*!
240d3a
+  @file    enforce.hpp
240d3a
+  @brief   Port of D's enforce() to C++ & Exiv2
240d3a
+  @author  Dan Čermák (D4N)
240d3a
+           dan.cermak@cgc-instruments.com
240d3a
+  @date    11-March-18, D4N: created
240d3a
+ */
240d3a
+
240d3a
+#include <string>
240d3a
+
240d3a
+#include "error.hpp"
240d3a
+
240d3a
+/*!
240d3a
+ * @brief Ensure that condition is true, otherwise throw an exception of the
240d3a
+ * type exception_t
240d3a
+ *
240d3a
+ * @tparam exception_t  Exception type that is thrown, must provide a
240d3a
+ * constructor that accepts a single argument to which arg1 is forwarded.
240d3a
+ *
240d3a
+ * @todo once we have C++>=11 use variadic templates and std::forward to remove
240d3a
+ * all overloads of enforce
240d3a
+ */
240d3a
+template <typename exception_t, typename T>
240d3a
+inline void enforce(bool condition, const T& arg1)
240d3a
+{
240d3a
+    if (!condition) {
240d3a
+        throw exception_t(arg1);
240d3a
+    }
240d3a
+}
240d3a
+
240d3a
+/*!
240d3a
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
240d3a
+ * the given error_code.
240d3a
+ */
240d3a
+inline void enforce(bool condition, Exiv2::ErrorCode err_code)
240d3a
+{
240d3a
+    if (!condition) {
240d3a
+        throw Exiv2::Error(err_code);
240d3a
+    }
240d3a
+}
240d3a
+
240d3a
+/*!
240d3a
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
240d3a
+ * the given error_code & arg1.
240d3a
+ */
240d3a
+template <typename T>
240d3a
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1)
240d3a
+{
240d3a
+    if (!condition) {
240d3a
+        throw Exiv2::Error(err_code, arg1);
240d3a
+    }
240d3a
+}
240d3a
+
240d3a
+/*!
240d3a
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
240d3a
+ * the given error_code, arg1 & arg2.
240d3a
+ */
240d3a
+template <typename T, typename U>
240d3a
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2)
240d3a
+{
240d3a
+    if (!condition) {
240d3a
+        throw Exiv2::Error(err_code, arg1, arg2);
240d3a
+    }
240d3a
+}
240d3a
+
240d3a
+/*!
240d3a
+ * @brief Ensure that condition is true, otherwise throw an Exiv2::Error with
240d3a
+ * the given error_code, arg1, arg2 & arg3.
240d3a
+ */
240d3a
+template <typename T, typename U, typename V>
240d3a
+inline void enforce(bool condition, Exiv2::ErrorCode err_code, const T& arg1, const U& arg2, const V& arg3)
240d3a
+{
240d3a
+    if (!condition) {
240d3a
+        throw Exiv2::Error(err_code, arg1, arg2, arg3);
240d3a
+    }
240d3a
+}
240d3a
240d3a
diff --git a/src/pngchunk.cpp b/src/pngchunk.cpp
240d3a
index 4dcca4d..aae0f5f 100644
240d3a
--- a/src/pngchunk.cpp
240d3a
+++ b/src/pngchunk.cpp
240d3a
@@ -37,6 +37,7 @@ EXIV2_RCSID("@(#) $Id$")
240d3a
 #include "iptc.hpp"
240d3a
 #include "image.hpp"
240d3a
 #include "error.hpp"
240d3a
+#include "enforce.hpp"
240d3a
240d3a
 // + standard includes
240d3a
 #include <sstream>
240d3a
@@ -46,6 +47,7 @@ EXIV2_RCSID("@(#) $Id$")
240d3a
 #include <iostream>
240d3a
 #include <cassert>
240d3a
 #include <cstdio>
240d3a
+#include <algorithm>
240d3a
240d3a
 #include <zlib.h>     // To uncompress or compress text chunk
240d3a
240d3a
@@ -86,7 +88,7 @@ namespace Exiv2 {
240d3a
240d3a
 #ifdef DEBUG
240d3a
         std::cout << "Exiv2::PngChunk::decodeTXTChunk: TXT chunk data: "
240d3a
-                  << std::string((const char*)arr.pData_, arr.size_) << "\n";
240d3a
+                  << std::string((const char*)arr.pData_, arr.size_) << std::endl;
240d3a
 #endif
240d3a
         parseChunkContent(pImage, key.pData_, key.size_, arr);
240d3a
240d3a
@@ -99,7 +101,7 @@ namespace Exiv2 {
240d3a
240d3a
 #ifdef DEBUG
240d3a
         std::cout << "Exiv2::PngChunk::decodeTXTChunk: TXT chunk key: "
240d3a
-                  << std::string((const char*)key.pData_, key.size_) << "\n";
240d3a
+                  << std::string((const char*)key.pData_, key.size_) << std::endl;
240d3a
 #endif
240d3a
         return parseTXTChunk(data, key.size_, type);
240d3a
240d3a
@@ -164,12 +166,18 @@ namespace Exiv2 {
240d3a
         }
240d3a
         else if(type == iTXt_Chunk)
240d3a
         {
240d3a
+            const int nullSeparators = std::count(&data.pData_[keysize+3], &data.pData_[data.size_], '\0');
240d3a
+
240d3a
+            enforce(nullSeparators >= 2, Exiv2::kerCorruptedMetadata);
240d3a
+
240d3a
             // Extract a deflate compressed or uncompressed UTF-8 text chunk
240d3a
240d3a
             // we get the compression flag after the key
240d3a
-            const byte* compressionFlag   = data.pData_ + keysize + 1;
240d3a
+            const byte compressionFlag   = data.pData_[keysize + 1];
240d3a
             // we get the compression method after the compression flag
240d3a
-            const byte* compressionMethod = data.pData_ + keysize + 2;
240d3a
+            const byte compressionMethod = data.pData_[keysize + 2];
240d3a
+            enforce(compressionFlag == 0x00 || compressionFlag == 0x01, Exiv2::kerCorruptedMetadata);
240d3a
+            enforce(compressionMethod == 0x00, Exiv2::kerCorruptedMetadata);
240d3a
             // language description string after the compression technique spec
240d3a
             std::string languageText((const char*)(data.pData_ + keysize + 3));
240d3a
             unsigned int languageTextSize = static_cast<unsigned int>(languageText.size());
240d3a
@@ -177,7 +185,7 @@ namespace Exiv2 {
240d3a
             std::string translatedKeyText((const char*)(data.pData_ + keysize + 3 + languageTextSize +1));
240d3a
             unsigned int translatedKeyTextSize = static_cast<unsigned int>(translatedKeyText.size());
240d3a
240d3a
-            if ( compressionFlag[0] == 0x00 )
240d3a
+            if ( compressionFlag == 0x00 )
240d3a
             {
240d3a
                 // then it's an uncompressed iTXt chunk
240d3a
 #ifdef DEBUG
240d3a
@@ -191,7 +199,7 @@ namespace Exiv2 {
240d3a
                 arr.alloc(textsize);
240d3a
                 arr = DataBuf(text, textsize);
240d3a
             }
240d3a
-            else if ( compressionFlag[0] == 0x01 && compressionMethod[0] == 0x00 )
240d3a
+            else if ( compressionFlag == 0x01 && compressionMethod == 0x00 )
240d3a
             {
240d3a
                 // then it's a zlib compressed iTXt chunk
240d3a
 #ifdef DEBUG
240d3a
diff --git a/src/pngimage.cpp b/src/pngimage.cpp
240d3a
index ed7399a..991da6c 100644
240d3a
--- a/src/pngimage.cpp
240d3a
+++ b/src/pngimage.cpp
240d3a
@@ -375,7 +375,7 @@ namespace Exiv2 {
240d3a
     void PngImage::readMetadata()
240d3a
     {
240d3a
 #ifdef DEBUG
240d3a
-        std::cerr << "Exiv2::PngImage::readMetadata: Reading PNG file " << io_->path() << "\n";
240d3a
+        std::cerr << "Exiv2::PngImage::readMetadata: Reading PNG file " << io_->path() << std::endl;
240d3a
 #endif
240d3a
         if (io_->open() != 0)
240d3a
         {
240d3a
@@ -398,7 +398,7 @@ namespace Exiv2 {
240d3a
             // Read chunk header.
240d3a
240d3a
 #ifdef DEBUG
240d3a
-            std::cout << "Exiv2::PngImage::readMetadata: Position: " << io_->tell() << "\n";
240d3a
+            std::cout << "Exiv2::PngImage::readMetadata: Position: " << io_->tell() << std::endl;
240d3a
 #endif
240d3a
             std::memset(cheaderBuf.pData_, 0x0, cheaderBuf.size_);
240d3a
             long bufRead = io_->read(cheaderBuf.pData_, cheaderBuf.size_);
240d3a
@@ -432,14 +432,14 @@ namespace Exiv2 {
240d3a
                 {
240d3a
                     // Last chunk found: we stop parsing.
240d3a
 #ifdef DEBUG
240d3a
-                    std::cout << "Exiv2::PngImage::readMetadata: Found IEND chunk (length: " << dataOffset << ")\n";
240d3a
+                    std::cout << "Exiv2::PngImage::readMetadata: Found IEND chunk with length: " << dataOffset << std::endl;
240d3a
 #endif
240d3a
                     return;
240d3a
                 }
240d3a
                 else if (!memcmp(cheaderBuf.pData_ + 4, "IHDR", 4))
240d3a
                 {
240d3a
 #ifdef DEBUG
240d3a
-                    std::cout << "Exiv2::PngImage::readMetadata: Found IHDR chunk (length: " << dataOffset << ")\n";
240d3a
+                    std::cout << "Exiv2::PngImage::readMetadata: Found IHDR chunk with length: " << dataOffset << std::endl;
240d3a
 #endif
240d3a
                     if (cdataBuf.size_ >= 8) {
240d3a
                         PngChunk::decodeIHDRChunk(cdataBuf, &pixelWidth_, &pixelHeight_);
240d3a
@@ -448,21 +448,21 @@ namespace Exiv2 {
240d3a
                 else if (!memcmp(cheaderBuf.pData_ + 4, "tEXt", 4))
240d3a
                 {
240d3a
 #ifdef DEBUG
240d3a
-                    std::cout << "Exiv2::PngImage::readMetadata: Found tEXt chunk (length: " << dataOffset << ")\n";
240d3a
+                    std::cout << "Exiv2::PngImage::readMetadata: Found tEXt chunk with length: " << dataOffset << std::endl;
240d3a
 #endif
240d3a
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::tEXt_Chunk);
240d3a
                 }
240d3a
                 else if (!memcmp(cheaderBuf.pData_ + 4, "zTXt", 4))
240d3a
                 {
240d3a
 #ifdef DEBUG
240d3a
-                    std::cout << "Exiv2::PngImage::readMetadata: Found zTXt chunk (length: " << dataOffset << ")\n";
240d3a
+                    std::cout << "Exiv2::PngImage::readMetadata: Found zTXt chunk with length: " << dataOffset << std::endl;
240d3a
 #endif
240d3a
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::zTXt_Chunk);
240d3a
                 }
240d3a
                 else if (!memcmp(cheaderBuf.pData_ + 4, "iTXt", 4))
240d3a
                 {
240d3a
 #ifdef DEBUG
240d3a
-                    std::cout << "Exiv2::PngImage::readMetadata: Found iTXt chunk (length: " << dataOffset << ")\n";
240d3a
+                    std::cout << "Exiv2::PngImage::readMetadata: Found iTXt chunk with length: " << dataOffset << std::endl;
240d3a
 #endif
240d3a
                     PngChunk::decodeTXTChunk(this, cdataBuf, PngChunk::iTXt_Chunk);
240d3a
                 }
240d3a
@@ -481,7 +481,7 @@ namespace Exiv2 {
240d3a
240d3a
             // Move to the next chunk: chunk data size + 4 CRC bytes.
240d3a
 #ifdef DEBUG
240d3a
-            std::cout << "Exiv2::PngImage::readMetadata: Seek to offset: " << dataOffset + 4 << "\n";
240d3a
+            std::cout << "Exiv2::PngImage::readMetadata: Seek to offset: " << dataOffset + 4 << std::endl;
240d3a
 #endif
240d3a
             io_->seek(dataOffset + 4 , BasicIo::cur);
240d3a
             if (io_->error() || io_->eof()) throw Error(14);
240d3a
@@ -511,8 +511,8 @@ namespace Exiv2 {
240d3a
         if (!outIo.isopen()) throw Error(21);
240d3a
240d3a
 #ifdef DEBUG
240d3a
-        std::cout << "Exiv2::PngImage::doWriteMetadata: Writing PNG file " << io_->path() << "\n";
240d3a
-        std::cout << "Exiv2::PngImage::doWriteMetadata: tmp file created " << outIo.path() << "\n";
240d3a
+        std::cout << "Exiv2::PngImage::doWriteMetadata: Writing PNG file " << io_->path() << std::endl;
240d3a
+        std::cout << "Exiv2::PngImage::doWriteMetadata: tmp file created " << outIo.path() << std::endl;
240d3a
 #endif
240d3a
240d3a
         // Ensure that this is the correct image type