Fix a crash with empty exiftool stderr output

This commit is contained in:
Přemysl Eric Janouch 2022-07-31 03:54:20 +02:00
parent 8437164dec
commit ea75579b33
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 17 additions and 12 deletions

View File

@ -1224,6 +1224,16 @@ get_target_path(GFile *file, GCancellable *cancellable)
return path; return path;
} }
static gchar *
bytes_to_utf8(GBytes *bytes)
{
gsize length = 0;
gconstpointer data = g_bytes_get_data(bytes, &length);
gchar *utf8 = data ? g_utf8_make_valid(data, length) : g_strdup("");
g_bytes_unref(bytes);
return utf8;
}
static void static void
info(FivView *self) info(FivView *self)
{ {
@ -1238,14 +1248,14 @@ info(FivView *self)
gchar *path = g_file_get_path(file); gchar *path = g_file_get_path(file);
GError *error = NULL; GError *error = NULL;
GBytes *inbytes = NULL, *outbytes = NULL, *errbytes = NULL; GBytes *bytes_in = NULL, *bytes_out = NULL, *bytes_err = NULL;
gchar *file_data = NULL; gchar *file_data = NULL;
gsize file_len = 0; gsize file_len = 0;
if (!path && !(path = get_target_path(file, NULL)) && if (!path && !(path = get_target_path(file, NULL)) &&
g_file_load_contents(file, NULL, &file_data, &file_len, NULL, &error)) { g_file_load_contents(file, NULL, &file_data, &file_len, NULL, &error)) {
flags |= G_SUBPROCESS_FLAGS_STDIN_PIPE; flags |= G_SUBPROCESS_FLAGS_STDIN_PIPE;
path = g_strdup("-"); path = g_strdup("-");
inbytes = g_bytes_new_take(file_data, file_len); bytes_in = g_bytes_new_take(file_data, file_len);
} }
g_object_unref(file); g_object_unref(file);
@ -1257,22 +1267,17 @@ info(FivView *self)
"-quiet", "--", path, NULL); "-quiet", "--", path, NULL);
g_free(path); g_free(path);
if (error || !g_subprocess_communicate( if (error || !g_subprocess_communicate(
subprocess, inbytes, NULL, &outbytes, &errbytes, &error)) subprocess, bytes_in, NULL, &bytes_out, &bytes_err, &error))
goto out; goto out;
gchar *out = bytes_to_utf8(bytes_out);
gchar *err = bytes_to_utf8(bytes_err);
GtkWidget *dialog = gtk_widget_new(GTK_TYPE_DIALOG, GtkWidget *dialog = gtk_widget_new(GTK_TYPE_DIALOG,
"use-header-bar", TRUE, "use-header-bar", TRUE,
"title", "Information", "title", "Information",
"transient-for", window, "transient-for", window,
"destroy-with-parent", TRUE, NULL); "destroy-with-parent", TRUE, NULL);
gchar *out = g_utf8_make_valid(
g_bytes_get_data(outbytes, NULL), g_bytes_get_size(outbytes));
gchar *err = g_utf8_make_valid(
g_bytes_get_data(errbytes, NULL), g_bytes_get_size(errbytes));
g_bytes_unref(outbytes);
g_bytes_unref(errbytes);
GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
if (*err) { if (*err) {
GtkWidget *info = gtk_info_bar_new(); GtkWidget *info = gtk_info_bar_new();
@ -1309,8 +1314,8 @@ info(FivView *self)
out: out:
if (error) if (error)
show_error_dialog(window, error); show_error_dialog(window, error);
if (inbytes) if (bytes_in)
g_bytes_unref(inbytes); g_bytes_unref(bytes_in);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -