From 061df50cea0ecbae30832a5a0b6f09d4b335d5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20D=C3=ADaz=20M=C3=A1s?= Date: Thu, 6 Jan 2022 16:55:14 +0100 Subject: [PATCH] keyTXTChunk improvement --- src/pngchunk_int.cpp | 20 +++++++++----------- unitTests/test_pngimage.cpp | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/pngchunk_int.cpp b/src/pngchunk_int.cpp index 56dbd42f..c3c2a1b4 100644 --- a/src/pngchunk_int.cpp +++ b/src/pngchunk_int.cpp @@ -107,20 +107,18 @@ namespace Exiv2 { const int offset = stripHeader ? 8 : 0; if (data.size() <= offset) throw Error(kerFailedToReadImageData); - const byte *key = data.c_data(offset); - // Find null chatecter at end of keyword. - int keysize=0; - while (key[keysize] != 0) - { - keysize++; - // look if keysize is valid. - if (keysize+offset >= data.size()) - throw Error(kerFailedToReadImageData); - /// \todo move conditional out of the loop + // Search for null char until the end of the DataBuf + const byte* dataPtr = data.c_data(); + int keysize=offset; + while (dataPtr[keysize] != 0 && keysize < data.size()) { + keysize++; } - return DataBuf(key, keysize); + if (keysize == data.size()) + throw Error(kerFailedToReadImageData); + + return DataBuf(dataPtr+offset, keysize-offset); } DataBuf PngChunk::parseTXTChunk(const DataBuf& data, diff --git a/unitTests/test_pngimage.cpp b/unitTests/test_pngimage.cpp index 0474bc31..82fec574 100644 --- a/unitTests/test_pngimage.cpp +++ b/unitTests/test_pngimage.cpp @@ -39,7 +39,7 @@ TEST(PngChunk, keyTxtChunkExtractsKeywordCorrectlyInPresenceOfNullChar) 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())); + ASSERT_TRUE(std::equal(key.data(), key.data()+key.size(), data.data()+8)); }