From 0c39953107bb336ea278ae89d9c911d90f9f1b85 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Thu, 3 Jul 2025 21:58:56 +0900 Subject: [PATCH] Fix crash when failed to open files --- src/Application.vala | 10 +++++++++- src/Backend/Checksum.vala | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 233b8a6..04b32f1 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -531,12 +531,20 @@ namespace Hashit { public void get_file_hash (string path) { this.files_uris.append_val (path); - string hash = Hashit.Backend.Checksum.calculate_hash ( + string? hash = Hashit.Backend.Checksum.calculate_hash ( this.selection_box.get_dropdown_value (), path ); GLib.Idle.add (() => { + if (hash == null) { + var toast = new Adw.Toast (_("Failed to calculate hash")); + toast.set_timeout (2); + this.toast_overlay.add_toast (toast); + + return false; + } + this.last_hash_entry.set_text (hash); TextIter text_end_iter; diff --git a/src/Backend/Checksum.vala b/src/Backend/Checksum.vala index 75f318f..f8191ef 100644 --- a/src/Backend/Checksum.vala +++ b/src/Backend/Checksum.vala @@ -5,7 +5,7 @@ namespace Hashit.Backend.Checksum { - public static string calculate_hash (string type, string file_path) { + public static string? calculate_hash (string type, string file_path) { GLib.Checksum checksum = null; switch (type) { @@ -23,7 +23,12 @@ namespace Hashit.Backend.Checksum { break; } - FileStream stream = FileStream.open (file_path, "rb"); + FileStream? stream = FileStream.open (file_path, "rb"); + if (stream == null) { + warning ("Failed to open \"%s\": %s", file_path, strerror(errno)); + return null; + } + uint8 buffer[100]; size_t size;