Merge pull request #1823 from Exiv2/mergify/bp/main/pr-1818

Fix memory leak in pngimage.cpp (backport #1818)
main
Kevin Backhouse 4 years ago committed by GitHub
commit db25089c6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -317,28 +317,29 @@ namespace Exiv2 {
if( bDump ) { if( bDump ) {
DataBuf dataBuf; DataBuf dataBuf;
auto data = new byte[dataOffset + 1]; enforce(static_cast<uint64_t>(dataOffset) < static_cast<unsigned long>(std::numeric_limits<long>::max()), kerFailedToReadImageData);
data[dataOffset] = 0; DataBuf data(static_cast<long>(dataOffset) + 1);
bufRead = io_->read(data,dataOffset); data.pData_[dataOffset] = 0;
bufRead = io_->read(data.pData_, static_cast<long>(dataOffset));
enforce(bufRead == static_cast<long>(dataOffset), kerFailedToReadImageData); enforce(bufRead == static_cast<long>(dataOffset), kerFailedToReadImageData);
io_->seek(restore, BasicIo::beg); io_->seek(restore, BasicIo::beg);
uint32_t name_l = static_cast<uint32_t>(std::strlen(reinterpret_cast<const char*>(data))) + size_t name_l = std::strlen(reinterpret_cast<const char*>(data.pData_)) +
1; // leading string length 1; // leading string length
enforce(name_l <= dataOffset, kerCorruptedMetadata); enforce(name_l < dataOffset, kerCorruptedMetadata);
uint32_t start = name_l; uint32_t start = static_cast<uint32_t>(name_l);
bool bLF = false; bool bLF = false;
// decode the chunk // decode the chunk
bool bGood = false; bool bGood = false;
if ( tEXt ) { if ( tEXt ) {
bGood = tEXtToDataBuf(data+name_l,dataOffset-name_l,dataBuf); bGood = tEXtToDataBuf(data.pData_ + name_l, static_cast<unsigned long>(dataOffset - name_l), dataBuf);
} }
if ( zTXt || iCCP ) { if ( zTXt || iCCP ) {
bGood = zlibToDataBuf(data+name_l+1,dataOffset-name_l-1,dataBuf); // +1 = 'compressed' flag bGood = zlibToDataBuf(data.pData_ + name_l + 1, static_cast<unsigned long>(dataOffset - name_l - 1), dataBuf); // +1 = 'compressed' flag
} }
if ( iTXt ) { if ( iTXt ) {
bGood = (start+3) < dataOffset ; // good if not a nul chunk bGood = (3 <= dataOffset) && (start < dataOffset-3); // good if not a nul chunk
} }
if ( eXIf ) { if ( eXIf ) {
bGood = true ;// eXIf requires no pre-processing) bGood = true ;// eXIf requires no pre-processing)
@ -347,8 +348,8 @@ namespace Exiv2 {
// format is content dependent // format is content dependent
if ( bGood ) { if ( bGood ) {
if ( bXMP ) { if ( bXMP ) {
while (start < dataOffset && !data[start]) start++; // skip leading nul bytes while (start < dataOffset && !data.pData_[start]) start++; // skip leading nul bytes
out << data+start; // output the xmp out << data.pData_ + start; // output the xmp
} }
if ( bExif || bIptc ) { if ( bExif || bIptc ) {
@ -389,13 +390,12 @@ namespace Exiv2 {
} }
if ( eXIf && option == kpsRecursive ) { if ( eXIf && option == kpsRecursive ) {
// create memio object with the data, then print the structure // create memio object with the data, then print the structure
BasicIo::UniquePtr p = BasicIo::UniquePtr(new MemIo(data,dataOffset)); BasicIo::UniquePtr p = BasicIo::UniquePtr(new MemIo(data.pData_, dataOffset));
printTiffStructure(*p,out,option,depth); printTiffStructure(*p,out,option,depth);
} }
if ( bLF ) out << std::endl; if ( bLF ) out << std::endl;
} }
delete[] data;
} }
io_->seek(dataOffset+4, BasicIo::cur);// jump past checksum io_->seek(dataOffset+4, BasicIo::cur);// jump past checksum
if (io_->error()) throw Error(kerFailedToReadImageData); if (io_->error()) throw Error(kerFailedToReadImageData);

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 B

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from system_tests import CaseMeta, path
class MemoryLeakInPngImagePrintStructure(metaclass=CaseMeta):
"""
Regression test for the bug described in:
https://github.com/Exiv2/exiv2/issues/1817
Note: the test only fails in an ASAN build.
"""
url = "https://github.com/Exiv2/exiv2/issues/1817"
filename = path("$data_path/issue_1817_poc.png")
commands = ["$exiv2 -pS $filename"]
stdout = ["""STRUCTURE OF PNG FILE: $filename
address | chunk | length | data | checksum
8 | eXIf | 0 | | 0x00000000
"""]
stderr = ["""$exiv2_exception_message $filename:
$kerCorruptedMetadata
"""]
retval = [1]
Loading…
Cancel
Save