1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:27:46 +00:00

LibThreading: Register BackgroundAction with EventLoop

BackgroundActions are now added as a job to the event loop, therefore
they get canceled when the loop exits and their on_complete action never
runs. This fixes all UAF bugs related to BackgroundAction's use of
EventLoops, as seen with e.g. thumbnail generation.
This commit is contained in:
kleines Filmröllchen 2022-12-29 14:57:00 +01:00 committed by Linus Groh
parent 8f4d0d3797
commit cf1fa419ab
4 changed files with 72 additions and 39 deletions

View file

@ -135,7 +135,7 @@ void FileProvider::query(DeprecatedString const& query, Function<void(Vector<Non
Vector<NonnullRefPtr<Result>> results;
for (auto& path : m_full_path_cache) {
if (task.is_cancelled())
if (task.is_canceled())
return {};
auto match_result = fuzzy_match(query, path);

View file

@ -30,26 +30,25 @@ bool MonitorWidget::set_wallpaper(DeprecatedString path)
if (!is_different_to_current_wallpaper_path(path))
return false;
(void)Threading::BackgroundAction<ErrorOr<NonnullRefPtr<Gfx::Bitmap>>>::construct(
(void)Threading::BackgroundAction<NonnullRefPtr<Gfx::Bitmap>>::construct(
[path](auto&) -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
if (path.is_empty())
return Error::from_errno(ENOENT);
return Gfx::Bitmap::load_from_file(path);
},
[this, path](ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap_or_error) -> ErrorOr<void> {
[this, path](NonnullRefPtr<Gfx::Bitmap> bitmap) -> ErrorOr<void> {
// If we've been requested to change while we were loading the bitmap, don't bother spending the cost to
// move and render the now stale bitmap.
if (is_different_to_current_wallpaper_path(path))
return {};
if (bitmap_or_error.is_error())
m_wallpaper_bitmap = nullptr;
else
m_wallpaper_bitmap = bitmap_or_error.release_value();
m_wallpaper_bitmap = move(bitmap);
m_desktop_dirty = true;
update();
return bitmap_or_error.is_error() ? bitmap_or_error.release_error() : ErrorOr<void> {};
return {};
},
[this, path](Error) -> void {
m_wallpaper_bitmap = nullptr;
});
if (path.is_empty())

View file

@ -291,12 +291,12 @@ void PropertiesWindow::DirectoryStatisticsCalculator::start()
VERIFY(!m_background_action);
m_background_action = Threading::BackgroundAction<int>::construct(
[this, strong_this = NonnullRefPtr(*this)](auto& task) {
[this, strong_this = NonnullRefPtr(*this)](auto& task) -> ErrorOr<int> {
auto timer = Core::ElapsedTimer();
while (!m_work_queue.is_empty()) {
auto base_directory = m_work_queue.dequeue();
auto result = Core::Directory::for_each_entry(base_directory, Core::DirIterator::SkipParentAndBaseDir, [&](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
if (task.is_cancelled())
if (task.is_canceled())
return Error::from_errno(ECANCELED);
struct stat st = {};
@ -315,7 +315,7 @@ void PropertiesWindow::DirectoryStatisticsCalculator::start()
}
// Show the first update, then show any subsequent updates every 100ms.
if (!task.is_cancelled() && on_update && (!timer.is_valid() || timer.elapsed_time() > 100_ms)) {
if (!task.is_canceled() && on_update && (!timer.is_valid() || timer.elapsed_time() > 100_ms)) {
timer.start();
on_update(m_total_size_in_bytes, m_file_count, m_directory_count);
}
@ -323,15 +323,18 @@ void PropertiesWindow::DirectoryStatisticsCalculator::start()
return IterationDecision::Continue;
});
if (result.is_error() && result.error().code() == ECANCELED)
return ECANCELED;
return Error::from_errno(ECANCELED);
}
return ESUCCESS;
return 0;
},
[this](auto result) -> ErrorOr<void> {
if (on_update && result == ESUCCESS)
[this](auto) -> ErrorOr<void> {
if (on_update)
on_update(m_total_size_in_bytes, m_file_count, m_directory_count);
return {};
},
[](auto) {
// Ignore the error.
});
}