1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-02-08 21:04:26 +00:00

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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 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 Path TorrentImpl::actualFilePath(const int index) const
{ {
const auto nativeIndex = m_torrentInfo.nativeIndexes().at(index); const QVector<lt::file_index_t> nativeIndexes = m_torrentInfo.nativeIndexes();
return Path(nativeTorrentInfo()->files().file_path(nativeIndex));
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 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) 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_renameCount;
m_nativeHandle.rename_file(m_torrentInfo.nativeIndexes().at(index) m_nativeHandle.rename_file(nativeIndexes[index], path.toString().toStdString());
, path.toString().toStdString());
} }
void TorrentImpl::handleStateUpdate(const lt::torrent_status &nativeStatus) void TorrentImpl::handleStateUpdate(const lt::torrent_status &nativeStatus)

View File

@ -241,6 +241,11 @@ Path TorrentInfo::filePath(const int index) const
{ {
if (!isValid()) return {}; if (!isValid()) return {};
Q_ASSERT(index >= 0);
Q_ASSERT(index < m_nativeIndexes.size());
if ((index < 0) || (index >= m_nativeIndexes.size()))
return {};
return Path(m_nativeInfo->orig_files().file_path(m_nativeIndexes[index])); return Path(m_nativeInfo->orig_files().file_path(m_nativeIndexes[index]));
} }
@ -258,6 +263,11 @@ qlonglong TorrentInfo::fileSize(const int index) const
{ {
if (!isValid()) return -1; if (!isValid()) return -1;
Q_ASSERT(index >= 0);
Q_ASSERT(index < m_nativeIndexes.size());
if ((index < 0) || (index >= m_nativeIndexes.size()))
return -1;
return m_nativeInfo->orig_files().file_size(m_nativeIndexes[index]); return m_nativeInfo->orig_files().file_size(m_nativeIndexes[index]);
} }
@ -265,6 +275,11 @@ qlonglong TorrentInfo::fileOffset(const int index) const
{ {
if (!isValid()) return -1; if (!isValid()) return -1;
Q_ASSERT(index >= 0);
Q_ASSERT(index < m_nativeIndexes.size());
if ((index < 0) || (index >= m_nativeIndexes.size()))
return -1;
return m_nativeInfo->orig_files().file_offset(m_nativeIndexes[index]); return m_nativeInfo->orig_files().file_offset(m_nativeIndexes[index]);
} }