if (getpgid(pid) == 0)
continue;
- candidate = g_new(struct memory_killer_candidate, 1);
+ /* Using the `try` version here is mostly wishful thinking because there's a call
+ * to `g_slist_prepend` below which can call `g_malloc` (the non-try version)
+ * internally and will most likely crash anyway in a low memory environment with
+ * no way to control it. Sometimes we can get lucky and avoid a crash though. */
+ candidate = g_try_new(struct memory_killer_candidate, 1);
if (!candidate) {
_E("Not enough memory");
g_slist_free_full(*candidates, g_free);
}
closedir(dp);
- return 0;
+
+ /* I don't think it is possible for no elements to be on the list at this point.
+ * Consider that at least resourced itself should be present. That said, it's
+ * good to be future-proof in case the candidate list gets some smart filtering
+ * in the future or something like that. */
+ return *candidates ? 0 : -ENOENT;
}
/**