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

@@ -241,6 +241,11 @@ Path TorrentInfo::filePath(const int index) const
{
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]));
}
@@ -258,6 +263,11 @@ qlonglong TorrentInfo::fileSize(const int index) const
{
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]);
}
@@ -265,6 +275,11 @@ qlonglong TorrentInfo::fileOffset(const int index) const
{
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]);
}