This is an archive of the discontinued LLVM Phabricator instance.

Fix BroadcasterManager::RemoveListener to really remove the listener
ClosedPublic

Authored by rnk on Feb 4 2020, 4:53 PM.

Details

Summary

This appears to be a real bug caught by -Wunused-value. std::find_if
doesn't modify the underlying collection, it just returns an iterator
pointing to the matching element.

Diff Detail

Event Timeline

rnk created this revision.Feb 4 2020, 4:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 4 2020, 4:53 PM

Unit tests: unknown.

clang-tidy: fail. clang-tidy found 1 errors and 0 warnings. 0 of them are added as review comments below (why?).

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

JDevlieghere accepted this revision.Feb 4 2020, 5:08 PM

Thanks for fixing this, Reid!

This revision is now accepted and ready to land.Feb 4 2020, 5:08 PM
This revision was automatically updated to reflect the committed changes.

Wow, cool bug.

It's too bad the original code re-used an iterator variable instead of make a new name (which would have helped the compiler find the problem). Note that the one they used is shadowed just a couple lines later.

It's too bad the original code feels it's necessary to create iter and end_iter up front. I know raw loops require this trick to avoid re-computing the end iterator on each iteration through the loop, but that shouldn't be necessary on algorithms like find_if.

It's too bad that erase with an end iterator isn't just a safe no-op, so that zillions of callers aren't required to check find's return value. Without the visual noise, it would be easier to write exactly what you want.

It's too bad the compiler cannot recognize that find_if has no side effects and thus ignoring its return value makes the statement a no-op.

It's too bad the std::erase(std::remove_if(...)) idiom is so cumbersome. I realize that would likely be overkill here, since you apparently want to erase just the first one that matches the predicate. Nonetheless, it would be harder to make this kind of bug.