From 8f799e1bcf169bd3ddc516c8c052715466d5d356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sun, 12 May 2019 01:23:18 +0200 Subject: [PATCH] Fixed possible crash when the cache wouldn't accept the new item --- src/map/IMG/img.cpp | 20 +++++++++----------- src/map/IMG/img.h | 2 +- src/map/IMG/subfile.cpp | 3 +-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/map/IMG/img.cpp b/src/map/IMG/img.cpp index 1f901deb..6f08eae8 100644 --- a/src/map/IMG/img.cpp +++ b/src/map/IMG/img.cpp @@ -230,20 +230,18 @@ template bool IMG::readValue(T &val) return true; } -QByteArray IMG::readBlock(int blockNum) +bool IMG::readBlock(int blockNum, QByteArray &data) { QByteArray *block = _blockCache[blockNum]; if (!block) { if (!_file.seek((qint64)blockNum * (qint64)_blockSize)) - return QByteArray(); - QByteArray *ba = new QByteArray(); - ba->resize(_blockSize); - if (read(ba->data(), _blockSize) < _blockSize) { - delete ba; - return QByteArray(); - } - _blockCache.insert(blockNum, ba); - return *ba; + return false; + data.resize(_blockSize); + if (read(data.data(), _blockSize) < _blockSize) + return false; + _blockCache.insert(blockNum, new QByteArray(data)); } else - return *block; + data = *block; + + return true; } diff --git a/src/map/IMG/img.h b/src/map/IMG/img.h index 736aa8e9..f479bd9b 100644 --- a/src/map/IMG/img.h +++ b/src/map/IMG/img.h @@ -65,7 +65,7 @@ private: typedef RTree TileTree; int blockSize() const {return _blockSize;} - QByteArray readBlock(int blockNum); + bool readBlock(int blockNum, QByteArray &data); qint64 read(char *data, qint64 maxSize); template bool readValue(T &val); diff --git a/src/map/IMG/subfile.cpp b/src/map/IMG/subfile.cpp index b8991ec1..12ccdf80 100644 --- a/src/map/IMG/subfile.cpp +++ b/src/map/IMG/subfile.cpp @@ -33,8 +33,7 @@ bool SubFile::seek(Handle &handle, quint32 pos) const if (handle.blockNum != blockNum) { if (blockNum >= _blocks.size()) return false; - handle.data = _img->readBlock(_blocks.at(blockNum)); - if (handle.data.isNull()) + if (!_img->readBlock(_blocks.at(blockNum), handle.data)) return false; handle.blockNum = blockNum; }