about summary refs log tree commit diff stats
path: root/src/common.c
diff options
context:
space:
mode:
authornandesu-utils <70854931+nandesu-utils@users.noreply.github.com>2022-08-24 23:50:24 +0300
committernandesu-utils <70854931+nandesu-utils@users.noreply.github.com>2022-08-27 16:25:15 +0300
commit056b19eb91c03c8a8d5f261fa38450b5238986dc (patch)
tree9badc2bef39a30f1b6d404277ed7a2d7b195368e /src/common.c
parent0864bc68d7037abd41cf75901ffaa0cd8bceb04d (diff)
downloadprofani-tty-056b19eb91c03c8a8d5f261fa38450b5238986dc.tar.gz
refactored call_external code
unluckily here the code neglected the fact that glib will set an error
to a location that was pointed by the error pointer if it is not null.
but it was of an undefined value hence profanity crashed. now it is null
as it must be.

also spawn error is returned when glib could not spawn the task for
some reason like the executable file does not exist but if the exit
status was non-zero it neglected the exit error and tried to output a
spawn error instead. now we check whether the process that we
instantiated has exited successfully

also now code uses `g_spawn_check_wait_status` which
`g_spawn_check_exit_status` has been aliased to.
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/src/common.c b/src/common.c
index b2496087..f5b59612 100644
--- a/src/common.c
+++ b/src/common.c
@@ -445,9 +445,10 @@ get_mentions(gboolean whole_word, gboolean case_sensitive, const char* const mes
 gboolean
 call_external(gchar** argv, gchar** std_out, gchar** std_err)
 {
-    GError *spawn_error, *status_error;
-    gboolean spawn_result;
-    gint exit_status;
+    GError *spawn_error = NULL;
+    GError *exit_error = NULL;
+    gboolean is_successful;
+    gint wait_status;
 
     GSpawnFlags flags = G_SPAWN_SEARCH_PATH;
     if (std_out == NULL)
@@ -455,30 +456,34 @@ call_external(gchar** argv, gchar** std_out, gchar** std_err)
     if (std_err == NULL)
         flags |= G_SPAWN_STDERR_TO_DEV_NULL;
 
-    spawn_result = g_spawn_sync(NULL, // Inherit the parent PWD.
+    is_successful = g_spawn_sync(NULL, // Inherit the parent PWD.
                                 argv,
                                 NULL, // Inherit the parent environment.
                                 flags,
                                 NULL, NULL, // No func. before exec() in child.
                                 std_out, std_err,
-                                &exit_status, &spawn_error);
+                                &wait_status, &spawn_error);
 
-    if (!spawn_result || !g_spawn_check_exit_status(exit_status, &status_error)) {
+    if (!is_successful) {
         gchar* cmd = g_strjoinv(" ", argv);
-        if (spawn_error && spawn_error->message) {
-            log_error("Spawning '%s' failed with '%s'.", cmd, spawn_error->message);
-            g_error_free(spawn_error);
-        } else if (status_error && status_error->message) {
-            log_error("Spawning '%s' failed with '%s'.", cmd, status_error->message);
-            g_error_free(status_error);
-            spawn_result = FALSE;
-        } else {
-            log_error("Spawning '%s' failed with.", cmd);
-        }
+        log_error("could not spawn '%s' with error '%s'", cmd, spawn_error->message);
+
+        g_error_free(spawn_error);
         g_free(cmd);
     }
+    else {
+        is_successful = g_spawn_check_wait_status(wait_status, &exit_error);
+
+        if (!is_successful) {
+            gchar* cmd = g_strjoinv(" ", argv);
+            log_error("'%s' exited with error '%s'", cmd, exit_error->message);
+
+            g_error_free(exit_error);
+            g_free(cmd);
+        }
+    }
 
-    return spawn_result;
+    return is_successful;
 }
 
 gchar**