diff --git a/src/pngchunk_int.cpp b/src/pngchunk_int.cpp index 0a7926c6..56dbd42f 100644 --- a/src/pngchunk_int.cpp +++ b/src/pngchunk_int.cpp @@ -103,13 +103,13 @@ namespace Exiv2 { DataBuf PngChunk::keyTXTChunk(const DataBuf& data, bool stripHeader) { - // From a tEXt, zTXt, or iTXt chunk, - // we get the key, it's a null terminated string at the chunk start + // From a tEXt, zTXt, or iTXt chunk, we get the keyword which is null terminated. const int offset = stripHeader ? 8 : 0; - if (data.size() <= offset) throw Error(kerFailedToReadImageData); + if (data.size() <= offset) + throw Error(kerFailedToReadImageData); const byte *key = data.c_data(offset); - // Find null string at end of key. + // Find null chatecter at end of keyword. int keysize=0; while (key[keysize] != 0) { @@ -117,11 +117,11 @@ namespace Exiv2 { // look if keysize is valid. if (keysize+offset >= data.size()) throw Error(kerFailedToReadImageData); + /// \todo move conditional out of the loop } return DataBuf(key, keysize); - - } // PngChunk::keyTXTChunk + } DataBuf PngChunk::parseTXTChunk(const DataBuf& data, int keysize, diff --git a/src/pngimage.cpp b/src/pngimage.cpp index ccdc624e..45f21ee0 100644 --- a/src/pngimage.cpp +++ b/src/pngimage.cpp @@ -712,14 +712,14 @@ namespace Exiv2 { std::cout << "Exiv2::PngImage::doWriteMetadata: strip " << szChunk << " chunk (length: " << dataOffset << ")" << std::endl; #endif - } - else + } else { #ifdef EXIV2_DEBUG_MESSAGES std::cout << "Exiv2::PngImage::doWriteMetadata: write " << szChunk << " chunk (length: " << dataOffset << ")" << std::endl; #endif - if (outIo.write(chunkBuf.c_data(), chunkBuf.size()) != chunkBuf.size()) throw Error(kerImageWriteFailed); + if (outIo.write(chunkBuf.c_data(), chunkBuf.size()) != chunkBuf.size()) + throw Error(kerImageWriteFailed); } } else { // Write all others chunk as well. diff --git a/unitTests/CMakeLists.txt b/unitTests/CMakeLists.txt index f67f6122..1933c922 100644 --- a/unitTests/CMakeLists.txt +++ b/unitTests/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(unit_tests test_futils.cpp test_helper_functions.cpp test_image_int.cpp + test_pngimage.cpp test_safe_op.cpp test_slice.cpp test_tiffheader.cpp diff --git a/unitTests/test_pngimage.cpp b/unitTests/test_pngimage.cpp new file mode 100644 index 00000000..0474bc31 --- /dev/null +++ b/unitTests/test_pngimage.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2004-2022 Exiv2 authors + * This program is part of the Exiv2 distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include "pngchunk_int.hpp" // This is not part of the public API + +#include + +#include +#include + +using namespace Exiv2; + +TEST(PngChunk, keyTxtChunkExtractsKeywordCorrectlyInPresenceOfNullChar) +{ + // The following data is: '\0\0"AzTXtRaw profile type exif\0\0x' + std::array data{0x00, 0x00, 0x22, 0x41, 0x7a, 0x54, 0x58, 0x74, + 0x52, 0x61, 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66, + 0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65, + 0x20, 0x65, 0x78, 0x69, 0x66, 0x00, 0x00, 0x78}; + + DataBuf chunkBuf(data.data(), static_cast(data.size())); + DataBuf key = Internal::PngChunk::keyTXTChunk(chunkBuf, true); + ASSERT_EQ(21, key.size()); + + ASSERT_TRUE(std::equal(key.data(), key.data()+key.size(), data.data()+8, data.data()+8+key.size())); +} + + +TEST(PngChunk, keyTxtChunkThrowsExceptionWhenThereIsNoNullChar) +{ + // The following data is: '\0\0"AzTXtRaw profile type exifx' + std::array data{0x00, 0x00, 0x22, 0x41, 0x7a, 0x54, 0x58, 0x74, + 0x52, 0x61, 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66, + 0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65, + 0x20, 0x65, 0x78, 0x69, 0x66, 0x78}; + + DataBuf chunkBuf(data.data(), static_cast(data.size())); + ASSERT_THROW(Internal::PngChunk::keyTXTChunk(chunkBuf, true), Exiv2::Error); +}