fix(pixbuf): handle invalid input file

If the input file is invalid (example: `grim` did not work due to
invalid geometry), handle the error gracefully.

Previously we had a coredump due to `g_error`.
This commit is contained in:
Jeremy Attali 2021-02-27 14:28:55 -05:00 committed by Jeremy Attali
parent a4429e8b7b
commit cdbd06d7af
3 changed files with 11 additions and 2 deletions

View File

@ -10,3 +10,4 @@ void pixbuf_save_to_file(GdkPixbuf *pixbuf, char *file);
void pixbuf_save_to_stdout(GdkPixbuf *pixbuf);
void pixbuf_scale_surface_from_widget(struct swappy_state *state,
GtkWidget *widget);
void pixbuf_free(struct swappy_state *state);

View File

@ -256,6 +256,7 @@ void blur_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
void application_finish(struct swappy_state *state) {
paint_free_all(state);
pixbuf_free(state);
cairo_surface_destroy(state->rendering_surface);
cairo_surface_destroy(state->original_image_surface);
if (state->temp_file_str) {
@ -269,7 +270,7 @@ void application_finish(struct swappy_state *state) {
g_free(state->geometry);
g_free(state->window);
g_free(state->ui);
g_object_unref(state->original_image);
g_object_unref(state->app);
config_free(state);

View File

@ -70,7 +70,8 @@ GdkPixbuf *pixbuf_init_from_file(struct swappy_state *state) {
GdkPixbuf *image = gdk_pixbuf_new_from_file(file, &error);
if (error != NULL) {
g_error("unable to load file: %s - reason: %s", file, error->message);
g_printerr("unable to load file: %s - reason: %s\n", file, error->message);
g_error_free(error);
return NULL;
}
@ -136,3 +137,9 @@ finish:
g_free(alloc);
}
void pixbuf_free(struct swappy_state *state) {
if (G_IS_OBJECT(state->original_image)) {
g_object_unref(state->original_image);
}
}