diff --git a/gtk/gtkmountoperation-x11.c b/gtk/gtkmountoperation-x11.c index 1b7bff6aa1..268354f31b 100644 --- a/gtk/gtkmountoperation-x11.c +++ b/gtk/gtkmountoperation-x11.c @@ -736,25 +736,32 @@ pid_get_command_line (GPid pid) static GPid pid_get_parent (GPid pid) { - struct kinfo_proc *kp; + struct kinfo_proc *kp = NULL; size_t len; - GPid ppid; + GPid ppid = 0; + + /* fail if trying to get the parent of the init process (no such thing) */ + if (pid == 1) + goto out; int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid, sizeof(struct kinfo_proc), 0 }; if (sysctl (mib, G_N_ELEMENTS (mib), NULL, &len, NULL, 0) == -1) - return (-1); + goto out; + mib[5] = (len / sizeof(struct kinfo_proc)); kp = g_malloc0 (len); if (sysctl (mib, G_N_ELEMENTS (mib), kp, &len, NULL, 0) < 0) - return -1; + goto out; ppid = kp->p_ppid; - g_free (kp); +out: + if (kp) + g_free (kp); return ppid; }