mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-13 14:42:29 +02:00
Fix random segfault on GTK icon functions
The segfaults were happening on GTK icon theme functions, which are called via the C++ interface functions such as Gtk::IconTheme::has_icon. There are multiple modules and threads using this functions on the default icon theme by calling Gtk::IconTheme::get_default(), which returns the same object for all callers, and was causing concurrent access to the same internal data structures on the GTK lib. Even a seemingly read-only function such as has_icon can cause writes due to the internal icon cache being updated. To avoid this issues, a program wide global mutex must be used to ensure a single thread is accessing the default icon theme instance. This commit implements wrappers for the existing IconTheme function calls, ensuring the global lock is held while calling the underling GTK functions.
This commit is contained in:
26
src/util/gtk_icon.cpp
Normal file
26
src/util/gtk_icon.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include "util/gtk_icon.hpp"
|
||||
|
||||
/* We need a global mutex for accessing the object returned by Gtk::IconTheme::get_default()
|
||||
* because it always returns the same object across different threads, and concurrent
|
||||
* access can cause data corruption and lead to invalid memory access and crashes.
|
||||
* Even concurrent calls that seem read only such as has_icon can cause issues because
|
||||
* the GTK lib may update the internal icon cache on this calls.
|
||||
*/
|
||||
|
||||
std::mutex DefaultGtkIconThemeWrapper::default_theme_mutex;
|
||||
|
||||
bool DefaultGtkIconThemeWrapper::has_icon(const std::string& value) {
|
||||
|
||||
const std::lock_guard<std::mutex> lock(default_theme_mutex);
|
||||
|
||||
return Gtk::IconTheme::get_default()->has_icon(value);
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gdk::Pixbuf> DefaultGtkIconThemeWrapper::load_icon(const char *name, int tmp_size, Gtk::IconLookupFlags flags) {
|
||||
|
||||
const std::lock_guard<std::mutex> lock(default_theme_mutex);
|
||||
|
||||
auto default_theme = Gtk::IconTheme::get_default();
|
||||
default_theme->rescan_if_needed();
|
||||
return default_theme->load_icon(name, tmp_size, flags);
|
||||
}
|
Reference in New Issue
Block a user