Skip to content

Commit cdbd06d

Browse files
committed
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`.
1 parent a4429e8 commit cdbd06d

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

include/pixbuf.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ void pixbuf_save_to_file(GdkPixbuf *pixbuf, char *file);
1010
void pixbuf_save_to_stdout(GdkPixbuf *pixbuf);
1111
void pixbuf_scale_surface_from_widget(struct swappy_state *state,
1212
GtkWidget *widget);
13+
void pixbuf_free(struct swappy_state *state);

src/application.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ void blur_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
256256

257257
void application_finish(struct swappy_state *state) {
258258
paint_free_all(state);
259+
pixbuf_free(state);
259260
cairo_surface_destroy(state->rendering_surface);
260261
cairo_surface_destroy(state->original_image_surface);
261262
if (state->temp_file_str) {
@@ -269,7 +270,7 @@ void application_finish(struct swappy_state *state) {
269270
g_free(state->geometry);
270271
g_free(state->window);
271272
g_free(state->ui);
272-
g_object_unref(state->original_image);
273+
273274
g_object_unref(state->app);
274275

275276
config_free(state);

src/pixbuf.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ GdkPixbuf *pixbuf_init_from_file(struct swappy_state *state) {
7070
GdkPixbuf *image = gdk_pixbuf_new_from_file(file, &error);
7171

7272
if (error != NULL) {
73-
g_error("unable to load file: %s - reason: %s", file, error->message);
73+
g_printerr("unable to load file: %s - reason: %s\n", file, error->message);
74+
g_error_free(error);
7475
return NULL;
7576
}
7677

@@ -136,3 +137,9 @@ void pixbuf_scale_surface_from_widget(struct swappy_state *state,
136137

137138
g_free(alloc);
138139
}
140+
141+
void pixbuf_free(struct swappy_state *state) {
142+
if (G_IS_OBJECT(state->original_image)) {
143+
g_object_unref(state->original_image);
144+
}
145+
}

0 commit comments

Comments
 (0)