Skip to content

Commit

Permalink
fix(pixbuf): handle invalid input file
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
jtheoof committed Feb 27, 2021
1 parent a4429e8 commit cdbd06d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/pixbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
3 changes: 2 additions & 1 deletion src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/pixbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -136,3 +137,9 @@ void pixbuf_scale_surface_from_widget(struct swappy_state *state,

g_free(alloc);
}

void pixbuf_free(struct swappy_state *state) {
if (G_IS_OBJECT(state->original_image)) {
g_object_unref(state->original_image);
}
}

0 comments on commit cdbd06d

Please sign in to comment.