From 2f3af9d7d39047ed4ceb5b1b8653f0668571f5a7 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Fri, 17 Jun 2022 20:37:59 +0800 Subject: [PATCH] map nullptr check --- chunkedstorage.cc | 5 ++++- chunkedstorage.hh | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/chunkedstorage.cc b/chunkedstorage.cc index 69b155fe..26e32cde 100644 --- a/chunkedstorage.cc +++ b/chunkedstorage.cc @@ -139,6 +139,8 @@ char * Reader::getBlock( uint32_t address, vector< char > & chunk ) // file.seek( offsets[ chunkIdx ] ); Mutex::Lock _( file.lock ); auto bytes = file.map( offsets[ chunkIdx ], 8 ); + if( bytes == nullptr ) + throw mapFailed(); auto qBytes = QByteArray::fromRawData( reinterpret_cast< char * >(bytes), 8 ); QDataStream in( qBytes ); in.setByteOrder( QDataStream::LittleEndian ); @@ -153,7 +155,8 @@ char * Reader::getBlock( uint32_t address, vector< char > & chunk ) // vector< unsigned char > compressedData( compressedSize ); auto chunkDataBytes = file.map( offsets[ chunkIdx ] + 8, compressedSize ); - + if( chunkDataBytes == nullptr ) + throw mapFailed(); // file.read( &compressedData.front(), compressedData.size() ); auto autoUnmap = qScopeGuard( [ & ] { diff --git a/chunkedstorage.hh b/chunkedstorage.hh index b92c6aed..4132e30b 100644 --- a/chunkedstorage.hh +++ b/chunkedstorage.hh @@ -25,6 +25,7 @@ DEF_EX( Ex, "Chunked storage exception", std::exception ) DEF_EX( exFailedToCompressChunk, "Failed to compress a chunk", Ex ) DEF_EX( exAddressOutOfRange, "The given chunked address is out of range", Ex ) DEF_EX( exFailedToDecompressChunk, "Failed to decompress a chunk", Ex ) +DEF_EX( mapFailed, "Failed to map/unmap the file", Ex ) /// This class writes data blocks in chunks. class Writer