From 467668c9a47b2bbe6d927e9679dafca61ffb324b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Br=C3=BCschweiler?= Date: Wed, 29 Aug 2012 10:53:36 +0200 Subject: [PATCH] desktop-shell: don't segfault on invalid icon path Instead draw a fallback icon and proceed as normal. https://bugs.freedesktop.org/show_bug.cgi?id=53860 v2: coding style fixes v3: memory leak, draw icon instead of relying on external files --- clients/desktop-shell.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c index dc87e75e..588dc1cd 100644 --- a/clients/desktop-shell.c +++ b/clients/desktop-shell.c @@ -472,6 +472,40 @@ panel_create(struct display *display) return panel; } +static cairo_surface_t * +load_icon_or_fallback(const char *icon) +{ + cairo_surface_t *surface = cairo_image_surface_create_from_png(icon); + cairo_t *cr; + + if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS) + return surface; + + cairo_surface_destroy(surface); + fprintf(stderr, "ERROR loading icon from file '%s'\n", icon); + + /* draw fallback icon */ + surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, + 20, 20); + cr = cairo_create(surface); + + cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1); + cairo_paint(cr); + + cairo_set_source_rgba(cr, 0, 0, 0, 1); + cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND); + cairo_rectangle(cr, 0, 0, 20, 20); + cairo_move_to(cr, 4, 4); + cairo_line_to(cr, 16, 16); + cairo_move_to(cr, 4, 16); + cairo_line_to(cr, 16, 4); + cairo_stroke(cr); + + cairo_destroy(cr); + + return surface; +} + static void panel_add_launcher(struct panel *panel, const char *icon, const char *path) { @@ -481,7 +515,7 @@ panel_add_launcher(struct panel *panel, const char *icon, const char *path) launcher = malloc(sizeof *launcher); memset(launcher, 0, sizeof *launcher); - launcher->icon = cairo_image_surface_create_from_png(icon); + launcher->icon = load_icon_or_fallback(icon); launcher->path = strdup(path); wl_array_init(&launcher->envp);