mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-30 09:36:21 +00:00
cleanup: refactor account_eval_password to use glib
Move from popen() to g_spawn_command_line_sync(). Which is nicer and gets rid of a resource leak that was present here.
This commit is contained in:
@@ -39,6 +39,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
@@ -160,53 +161,31 @@ account_eval_password(ProfAccount* account)
|
|||||||
assert(account != NULL);
|
assert(account != NULL);
|
||||||
assert(account->eval_password != NULL);
|
assert(account->eval_password != NULL);
|
||||||
|
|
||||||
errno = 0;
|
gchar* stdout_buf = NULL;
|
||||||
|
GError* error = NULL;
|
||||||
|
gint exit_status = 0;
|
||||||
|
|
||||||
FILE* stream = popen(account->eval_password, "r");
|
if (!g_spawn_command_line_sync(account->eval_password, &stdout_buf, NULL, &exit_status, &error)) {
|
||||||
if (stream == NULL) {
|
log_error("Failed to execute `eval_password` command: %s", error->message);
|
||||||
const char* errmsg = strerror(errno);
|
g_error_free(error);
|
||||||
if (errmsg) {
|
return FALSE;
|
||||||
log_error("Could not execute `eval_password` command (%s).",
|
}
|
||||||
errmsg);
|
|
||||||
} else {
|
if (WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == 0) {
|
||||||
log_error("Failed to allocate memory for `eval_password` command.");
|
account->password = stdout_buf;
|
||||||
|
g_strstrip(account->password);
|
||||||
|
if (account->password[0] == '\0') {
|
||||||
|
log_error("Empty password returned by `eval_password` command.");
|
||||||
|
g_free(account->password);
|
||||||
|
account->password = NULL;
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
return TRUE;
|
||||||
|
} else {
|
||||||
|
log_error("`eval_password` command failed with status %d", exit_status);
|
||||||
|
g_free(stdout_buf);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
account->password = g_malloc(READ_BUF_SIZE);
|
|
||||||
if (!account->password) {
|
|
||||||
log_error("Failed to allocate enough memory to read `eval_password` "
|
|
||||||
"output.");
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
account->password = fgets(account->password, READ_BUF_SIZE, stream);
|
|
||||||
if (!account->password) {
|
|
||||||
log_error("Failed to read password from stream.");
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
int exit_status = pclose(stream);
|
|
||||||
if (exit_status > 0) {
|
|
||||||
log_error("Command for `eval_password` returned error status (%s).",
|
|
||||||
exit_status);
|
|
||||||
return FALSE;
|
|
||||||
} else if (exit_status < 0) {
|
|
||||||
log_error("Failed to close stream for `eval_password` command output "
|
|
||||||
"(%s).",
|
|
||||||
strerror(errno));
|
|
||||||
return FALSE;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Remove leading and trailing whitespace from output.
|
|
||||||
g_strstrip(account->password);
|
|
||||||
if (!account->password) {
|
|
||||||
log_error("Empty password returned by `eval_password` command.");
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
Reference in New Issue
Block a user