Skip to content

Commit

Permalink
clazy: Convert const iterator with non-const functions to range-for. …
Browse files Browse the repository at this point in the history
…(plugins)

The clazy "strict iterator" check pointed out a number of places where
a const iterator was declared, and then used with the results of
begin/end/find instead of constBegin/constEnd/constFind. If the
container detaches while running it in the method it will result in a
crash.

Fix these by converting the loop to a c++11 range-for loop and use the
qAsConst function to prevent the Qt data structure from detaching.
  • Loading branch information
linuxdude42 committed Sep 7, 2020
1 parent 77c952d commit cd98d76
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 53 deletions.
9 changes: 2 additions & 7 deletions mythplugins/mythgame/mythgame/gamehandler.cpp
Expand Up @@ -575,11 +575,8 @@ int GameHandler::buildFileCount(const QString& directory, GameHandler *handler)

RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList List = RomDir.entryInfoList();
for (QFileInfoList::const_iterator it = List.begin();
it != List.end(); ++it)
for (const auto & Info : qAsConst(List))
{
QFileInfo Info = *it;

if (Info.isDir())
{
filecount += buildFileCount(Info.filePath(), handler);
Expand Down Expand Up @@ -639,10 +636,8 @@ void GameHandler::buildFileList(const QString& directory, GameHandler *handler,
RomDir.setSorting( QDir:: DirsFirst | QDir::Name );
RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList List = RomDir.entryInfoList();
for (QFileInfoList::const_iterator it = List.begin();
it != List.end(); ++it)
for (const auto & Info : qAsConst(List))
{
QFileInfo Info = *it;
QString RomName = Info.fileName();
QString GameName = Info.completeBaseName();

Expand Down
13 changes: 6 additions & 7 deletions mythplugins/mythgame/mythgame/gamescan.cpp
Expand Up @@ -136,11 +136,10 @@ bool GameScannerThread::buildFileList()
SendProgressEvent(counter, (uint)m_handlers.size(),
GameScanner::tr("Searching for games..."));

for (QList<GameHandler*>::const_iterator iter = m_handlers.begin();
iter != m_handlers.end(); ++iter)
for (auto * handler : qAsConst(m_handlers))
{
QDir dir((*iter)->SystemRomPath());
QStringList extensions = (*iter)->ValidExtensions();
QDir dir(handler->SystemRomPath());
QStringList extensions = handler->ValidExtensions();
QStringList filters;
for (const auto & ext : qAsConst(extensions))
{
Expand All @@ -154,10 +153,10 @@ bool GameScannerThread::buildFileList()
for (const auto & file : qAsConst(files))
{
RomFileInfo info;
info.system = (*iter)->SystemName();
info.gametype = (*iter)->GameType();
info.system = handler->SystemName();
info.gametype = handler->GameType();
info.romfile = file;
info.rompath = (*iter)->SystemRomPath();
info.rompath = handler->SystemRomPath();
info.romname = QFileInfo(file).baseName();
info.indb = false;
m_files.append(info);
Expand Down
21 changes: 6 additions & 15 deletions mythplugins/mythgame/mythgame/gameui.cpp
Expand Up @@ -979,23 +979,14 @@ void GameUI::OnGameSearchDone(MetadataLookup *lookup)

// Imagery
ArtworkList coverartlist = lookup->GetArtwork(kArtworkCoverart);
for (ArtworkList::const_iterator p = coverartlist.begin();
p != coverartlist.end(); ++p)
{
coverart.prepend((*p).url);
}
for (const auto & art : qAsConst(coverartlist))
coverart.prepend(art.url);
ArtworkList fanartlist = lookup->GetArtwork(kArtworkFanart);
for (ArtworkList::const_iterator p = fanartlist.begin();
p != fanartlist.end(); ++p)
{
fanart.prepend((*p).url);
}
for (const auto & art : qAsConst(fanartlist))
fanart.prepend(art.url);
ArtworkList screenshotlist = lookup->GetArtwork(kArtworkScreenshot);
for (ArtworkList::const_iterator p = screenshotlist.begin();
p != screenshotlist.end(); ++p)
{
screenshot.prepend((*p).url);
}
for (const auto & art : qAsConst(screenshotlist))
screenshot.prepend(art.url);

StartGameImageSet(node, coverart, fanart, screenshot);

Expand Down
4 changes: 2 additions & 2 deletions mythplugins/mythmusic/mythmusic/avfdecoder.cpp
Expand Up @@ -586,9 +586,9 @@ bool avfDecoderFactory::supports(const QString &source) const
#else
QStringList list = extension().split("|", Qt::SkipEmptyParts);
#endif
for (QStringList::const_iterator it = list.begin(); it != list.end(); ++it)
for (const auto& str : qAsConst(list))
{
if (*it == source.right((*it).length()).toLower())
if (str == source.right(str.length()).toLower())
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions mythplugins/mythmusic/mythmusic/cddb.cpp
Expand Up @@ -480,16 +480,16 @@ bool Dbase::Search(Cddb::Matches& res, const Cddb::discid_t discID)
return true;

QFileInfoList list = QDir(GetDB()).entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
for (QFileInfoList::const_iterator it = list.begin(); it != list.end(); ++it)
for (const auto & fi1 : qAsConst(list))
{
QString genre = it->baseName();
QString genre = fi1.baseName();

QFileInfoList ids = QDir(it->canonicalFilePath()).entryInfoList(QDir::Files);
for (QFileInfoList::const_iterator it2 = ids.begin(); it2 != ids.end(); ++it2)
QFileInfoList ids = QDir(fi1.canonicalFilePath()).entryInfoList(QDir::Files);
for (const auto & fi2 : qAsConst(ids))
{
if (it2->baseName().toUInt(nullptr,16) == discID)
if (fi2.baseName().toUInt(nullptr,16) == discID)
{
QFile file(it2->canonicalFilePath());
QFile file(fi2.canonicalFilePath());
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
Cddb::Album a(QTextStream(&file).readAll());
Expand Down
9 changes: 3 additions & 6 deletions mythplugins/mythmusic/mythmusic/importmusic.cpp
Expand Up @@ -1033,13 +1033,10 @@ void ImportCoverArtDialog::scanDirectory()
if (list.isEmpty())
return;

QFileInfoList::const_iterator it = list.begin();
while (it != list.end())
for (const auto & fi : qAsConst(list))
{
const QFileInfo *fi = &(*it);
++it;
QString filename = fi->absoluteFilePath();
if (!fi->isDir())
QString filename = fi.absoluteFilePath();
if (!fi.isDir())
{
m_filelist.append(filename);
}
Expand Down
8 changes: 3 additions & 5 deletions mythplugins/mythmusic/mythmusic/main.cpp
Expand Up @@ -469,9 +469,8 @@ static QStringList BuildFileList(const QString &dir, const QStringList &filters)
if (list.isEmpty())
return ret;

for(QFileInfoList::const_iterator it = list.begin(); it != list.end(); ++it)
for (const auto & fi : qAsConst(list))
{
const QFileInfo &fi = *it;
if (fi.isDir())
{
ret += BuildFileList(fi.absoluteFilePath(), filters);
Expand Down Expand Up @@ -578,10 +577,9 @@ static void handleMedia(MythMediaDevice *cd)

// Read track metadata and add to all_music
int track = 0;
for (QStringList::const_iterator it = trackList.begin();
it != trackList.end(); ++it)
for (const auto & file : qAsConst(trackList))
{
QScopedPointer<MusicMetadata> meta(MetaIO::readMetadata(*it));
QScopedPointer<MusicMetadata> meta(MetaIO::readMetadata(file));
if (meta)
{
meta->setTrack(++track);
Expand Down
9 changes: 4 additions & 5 deletions mythplugins/mythnetvision/mythnetvision/nettree.cpp
Expand Up @@ -178,20 +178,19 @@ void NetTree::LoadData(void)
using MGTreeChildList = QList<MythGenericTree *>;
MGTreeChildList *lchildren = m_currentNode->getAllChildren();

for (MGTreeChildList::const_iterator p = lchildren->begin();
p != lchildren->end(); ++p)
for (auto * child : qAsConst(*lchildren))
{
if (*p != nullptr)
if (child != nullptr)
{
auto *item =
new MythUIButtonListItem(m_siteButtonList, QString(), nullptr,
true, MythUIButtonListItem::NotChecked);

item->SetData(QVariant::fromValue(*p));
item->SetData(QVariant::fromValue(child));

UpdateItem(item);

if (*p == selectedNode)
if (child == selectedNode)
m_siteButtonList->SetItemCurrent(item);
}
}
Expand Down

0 comments on commit cd98d76

Please sign in to comment.