Avoid out-of-bounds access

This happens when the `index` is a negative number.
Added `Q_ASSERT()` to catch coding errors at debug run time.

PR #17953.
This commit is contained in:
Chocobo1
2022-10-31 12:34:20 +08:00
committed by GitHub
parent 3ee0457cfa
commit 3a2e73cc94
2 changed files with 35 additions and 5 deletions

View File

@@ -805,13 +805,22 @@ int TorrentImpl::seedingTimeLimit() const
Path TorrentImpl::filePath(const int index) const
{
return m_filePaths.at(index);
Q_ASSERT(index >= 0);
Q_ASSERT(index < m_filePaths.size());
return m_filePaths.value(index, {});
}
Path TorrentImpl::actualFilePath(const int index) const
{
const auto nativeIndex = m_torrentInfo.nativeIndexes().at(index);
return Path(nativeTorrentInfo()->files().file_path(nativeIndex));
const QVector<lt::file_index_t> nativeIndexes = m_torrentInfo.nativeIndexes();
Q_ASSERT(index >= 0);
Q_ASSERT(index < nativeIndexes.size());
if ((index < 0) || (index >= nativeIndexes.size()))
return {};
return Path(nativeTorrentInfo()->files().file_path(nativeIndexes[index]));
}
qlonglong TorrentImpl::fileSize(const int index) const
@@ -1721,9 +1730,15 @@ void TorrentImpl::moveStorage(const Path &newPath, const MoveStorageMode mode)
void TorrentImpl::renameFile(const int index, const Path &path)
{
const QVector<lt::file_index_t> nativeIndexes = m_torrentInfo.nativeIndexes();
Q_ASSERT(index >= 0);
Q_ASSERT(index < nativeIndexes.size());
if ((index < 0) || (index >= nativeIndexes.size()))
return;
++m_renameCount;
m_nativeHandle.rename_file(m_torrentInfo.nativeIndexes().at(index)
, path.toString().toStdString());
m_nativeHandle.rename_file(nativeIndexes[index], path.toString().toStdString());
}
void TorrentImpl::handleStateUpdate(const lt::torrent_status &nativeStatus)