aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-05-13 10:58:41 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-05-13 11:37:46 +0200
commitcd917f48454875ad6b7fc69455281d72760c44ee (patch)
tree70a7a43fe43b7f893468f9120def5513774a242c /src
parentadd command module to handle request-response communications (diff)
downloadcimple-cd917f48454875ad6b7fc69455281d72760c44ee.tar.gz
cimple-cd917f48454875ad6b7fc69455281d72760c44ee.zip
best practices & coding style fixes
* I don't really need to declare all variables at the top of the function anymore. * Default-initialize variables more. * Don't set the output parameter until the object is completely constructed.
Diffstat (limited to 'src')
-rw-r--r--src/ci.c4
-rw-r--r--src/ci_queue.c24
-rw-r--r--src/client.c13
-rw-r--r--src/client_main.c3
-rw-r--r--src/cmd_line.c15
-rw-r--r--src/command.c20
-rw-r--r--src/file.c23
-rw-r--r--src/msg.c27
-rw-r--r--src/net.c23
-rw-r--r--src/server.c11
-rw-r--r--src/server_main.c2
-rw-r--r--src/sqlite.c45
-rw-r--r--src/storage_sqlite.c30
-rw-r--r--src/tcp_server.c37
-rw-r--r--src/worker.c11
-rw-r--r--src/worker_main.c2
16 files changed, 129 insertions, 161 deletions
diff --git a/src/ci.c b/src/ci.c
index f5498b2..ee5fd50 100644
--- a/src/ci.c
+++ b/src/ci.c
@@ -77,8 +77,8 @@ cleanup_repo:
int ci_run_git_repo(const char *url, const char *rev, struct proc_output *output)
{
- char *oldpwd;
- git_repository *repo;
+ char *oldpwd = NULL;
+ git_repository *repo = NULL;
int ret = 0;
ret = ci_prepare_git_repo(&repo, url, rev);
diff --git a/src/ci_queue.c b/src/ci_queue.c
index 0d86f59..0928ff9 100644
--- a/src/ci_queue.c
+++ b/src/ci_queue.c
@@ -20,23 +20,19 @@ struct ci_queue_entry {
int ci_queue_entry_create(struct ci_queue_entry **_entry, const char *_url, const char *_rev)
{
- struct ci_queue_entry *entry;
- char *url, *rev;
-
- *_entry = malloc(sizeof(struct ci_queue_entry));
- if (!*_entry) {
+ struct ci_queue_entry *entry = malloc(sizeof(struct ci_queue_entry));
+ if (!entry) {
log_errno("malloc");
goto fail;
}
- entry = *_entry;
- url = strdup(_url);
+ char *url = strdup(_url);
if (!url) {
log_errno("strdup");
goto free_entry;
}
- rev = strdup(_rev);
+ char *rev = strdup(_rev);
if (!rev) {
log_errno("strdup");
goto free_url;
@@ -45,6 +41,7 @@ int ci_queue_entry_create(struct ci_queue_entry **_entry, const char *_url, cons
entry->url = url;
entry->rev = rev;
+ *_entry = entry;
return 0;
free_url:
@@ -81,11 +78,9 @@ void ci_queue_create(struct ci_queue *queue)
void ci_queue_destroy(struct ci_queue *queue)
{
- struct ci_queue_entry *entry1, *entry2;
-
- entry1 = STAILQ_FIRST(queue);
+ struct ci_queue_entry *entry1 = STAILQ_FIRST(queue);
while (entry1) {
- entry2 = STAILQ_NEXT(entry1, entries);
+ struct ci_queue_entry *entry2 = STAILQ_NEXT(entry1, entries);
ci_queue_entry_destroy(entry1);
entry1 = entry2;
}
@@ -109,10 +104,7 @@ void ci_queue_add_first(struct ci_queue *queue, struct ci_queue_entry *entry)
struct ci_queue_entry *ci_queue_remove_first(struct ci_queue *queue)
{
- struct ci_queue_entry *entry;
-
- entry = STAILQ_FIRST(queue);
+ struct ci_queue_entry *entry = STAILQ_FIRST(queue);
STAILQ_REMOVE_HEAD(queue, entries);
-
return entry;
}
diff --git a/src/client.c b/src/client.c
index 14eab14..b321b91 100644
--- a/src/client.c
+++ b/src/client.c
@@ -18,21 +18,20 @@ struct client {
int client_create(struct client **_client, const struct settings *settings)
{
- struct client *client;
int ret = 0;
- *_client = malloc(sizeof(struct client));
- if (!*_client) {
+ struct client *client = malloc(sizeof(struct client));
+ if (!client) {
log_errno("malloc");
return -1;
}
- client = *_client;
ret = net_connect(settings->host, settings->port);
if (ret < 0)
goto free;
client->fd = ret;
+ *_client = client;
return ret;
free:
@@ -49,8 +48,7 @@ void client_destroy(struct client *client)
int client_main(const struct client *client, const char **argv)
{
- struct msg *request;
- struct msg *response;
+ struct msg *request = NULL, *response = NULL;
int ret = 0;
ret = msg_from_argv(&request, argv);
@@ -61,8 +59,9 @@ int client_main(const struct client *client, const char **argv)
if (ret < 0)
goto free_request;
- if (msg_is_error(response)) {
+ if (!msg_is_success(response)) {
log_err("Server failed to process the request\n");
+ msg_dump(response);
ret = -1;
goto free_response;
}
diff --git a/src/client_main.c b/src/client_main.c
index dd22508..c402b20 100644
--- a/src/client_main.c
+++ b/src/client_main.c
@@ -10,6 +10,7 @@
#include "const.h"
#include <getopt.h>
+#include <stdlib.h>
static struct settings default_settings()
{
@@ -62,7 +63,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
int main(int argc, char *argv[])
{
struct settings settings;
- struct client *client;
+ struct client *client = NULL;
int ret = 0;
ret = parse_settings(&settings, argc, argv);
diff --git a/src/cmd_line.c b/src/cmd_line.c
index fee18d4..8651831 100644
--- a/src/cmd_line.c
+++ b/src/cmd_line.c
@@ -20,15 +20,13 @@ static char *get_current_binary_path()
static char *get_current_binary_name()
{
- char *path, *name, *result;
-
- path = get_current_binary_path();
+ char *path = get_current_binary_path();
if (!path)
return NULL;
- name = basename(path);
+ char *name = basename(path);
- result = strdup(name);
+ char *result = strdup(name);
if (!result) {
log_errno("strdup");
goto free_path;
@@ -42,14 +40,11 @@ free_path:
void exit_with_usage(int ec)
{
- char *binary;
- FILE *dest;
-
- dest = stdout;
+ FILE *dest = stdout;
if (ec)
dest = stderr;
- binary = get_current_binary_name();
+ char *binary = get_current_binary_name();
fprintf(dest, "usage: %s %s\n", binary ?: "prog", get_usage_string());
free(binary);
diff --git a/src/command.c b/src/command.c
index b1204a9..b469ef6 100644
--- a/src/command.c
+++ b/src/command.c
@@ -36,7 +36,7 @@ static void free_def(struct command_def *def)
static int copy_defs(struct command_def *dest, const struct command_def *src, size_t numof_defs)
{
- size_t numof_copied, i;
+ size_t numof_copied = 0;
int ret = 0;
for (numof_copied = 0; numof_copied < numof_defs; ++numof_copied) {
@@ -48,7 +48,7 @@ static int copy_defs(struct command_def *dest, const struct command_def *src, si
return 0;
free:
- for (i = 0; i < numof_copied; ++i)
+ for (size_t i = 0; i < numof_copied; ++i)
free_def(&dest[numof_copied]);
return -1;
@@ -56,24 +56,22 @@ free:
static void free_defs(struct command_def *defs, size_t numof_defs)
{
- size_t i;
-
- for (i = 0; i < numof_defs; ++i)
+ for (size_t i = 0; i < numof_defs; ++i)
free_def(&defs[i]);
}
int command_dispatcher_create(struct command_dispatcher **_dispatcher, struct command_def *defs,
size_t numof_defs, void *ctx)
{
- struct command_dispatcher *dispatcher;
int ret = 0;
- *_dispatcher = malloc(sizeof(struct command_dispatcher));
- if (!*_dispatcher) {
+ struct command_dispatcher *dispatcher = malloc(sizeof(struct command_dispatcher));
+ if (!dispatcher) {
log_errno("malloc");
return -1;
}
- dispatcher = *_dispatcher;
+
+ dispatcher->ctx = ctx;
dispatcher->defs = malloc(sizeof(struct command_def) * numof_defs);
if (!dispatcher->defs) {
@@ -86,7 +84,7 @@ int command_dispatcher_create(struct command_dispatcher **_dispatcher, struct co
if (ret < 0)
goto free_defs;
- dispatcher->ctx = ctx;
+ *_dispatcher = dispatcher;
return 0;
free_defs:
@@ -144,7 +142,7 @@ exit:
int command_dispatcher_conn_handler(int conn_fd, void *_dispatcher)
{
struct command_dispatcher *dispatcher = (struct command_dispatcher *)_dispatcher;
- struct msg *request;
+ struct msg *request = NULL;
int ret = 0;
ret = msg_recv(conn_fd, &request);
diff --git a/src/file.c b/src/file.c
index bc7af8e..cea6794 100644
--- a/src/file.c
+++ b/src/file.c
@@ -104,14 +104,14 @@ int file_exists(const char *path)
return !ret && S_ISREG(stat.st_mode);
}
-int file_read(int fd, char **output, size_t *len)
+int file_read(int fd, char **_contents, size_t *_len)
{
char buf[128];
size_t buf_len = sizeof(buf) / sizeof(buf[0]);
int ret = 0;
- *output = NULL;
- *len = 0;
+ char *contents = NULL;
+ size_t len = 0;
while (1) {
ssize_t read_now = read(fd, buf, buf_len);
@@ -122,21 +122,24 @@ int file_read(int fd, char **output, size_t *len)
goto free_output;
}
- if (!read_now)
+ if (!read_now) {
+ *_contents = contents;
+ *_len = len;
goto exit;
+ }
- *output = realloc(*output, *len + read_now + 1);
- if (!*output) {
+ contents = realloc(contents, len + read_now + 1);
+ if (!contents) {
log_errno("realloc");
return -1;
}
- memcpy(*output + *len, buf, read_now);
- *len += read_now;
- *(*output + *len) = '\0';
+ memcpy(contents + len, buf, read_now);
+ len += read_now;
+ contents[len] = '\0';
}
free_output:
- free(*output);
+ free(contents);
exit:
return ret;
diff --git a/src/msg.c b/src/msg.c
index 18e4c25..583c5e5 100644
--- a/src/msg.c
+++ b/src/msg.c
@@ -30,13 +30,13 @@ const char **msg_get_words(const struct msg *msg)
int msg_success(struct msg **msg)
{
- const char *argv[] = {"success", NULL};
+ static const char *argv[] = {"success", NULL};
return msg_from_argv(msg, argv);
}
int msg_error(struct msg **msg)
{
- const char *argv[] = {"error", NULL};
+ static const char *argv[] = {"error", NULL};
return msg_from_argv(msg, argv);
}
@@ -82,15 +82,13 @@ free_copied:
int msg_copy(struct msg **_dest, const struct msg *src)
{
- struct msg *dest = NULL;
int ret = 0;
- *_dest = malloc(sizeof(struct msg));
- if (!_dest) {
+ struct msg *dest = malloc(sizeof(struct msg));
+ if (!dest) {
log_errno("malloc");
return -1;
}
- dest = *_dest;
dest->argc = src->argc;
@@ -98,6 +96,7 @@ int msg_copy(struct msg **_dest, const struct msg *src)
if (ret < 0)
goto free;
+ *_dest = dest;
return 0;
free:
@@ -116,15 +115,13 @@ void msg_free(struct msg *msg)
int msg_from_argv(struct msg **_msg, const char **argv)
{
- struct msg *msg = NULL;
int ret = 0;
- *_msg = malloc(sizeof(struct msg));
- if (!*_msg) {
+ struct msg *msg = malloc(sizeof(struct msg));
+ if (!msg) {
log_errno("malloc");
return -1;
}
- msg = *_msg;
msg->argc = 0;
for (const char **s = argv; *s; ++s)
@@ -134,6 +131,7 @@ int msg_from_argv(struct msg **_msg, const char **argv)
if (ret < 0)
goto free;
+ *_msg = msg;
return 0;
free:
@@ -200,7 +198,7 @@ free:
int msg_send(int fd, const struct msg *msg)
{
- struct buf *buf;
+ struct buf *buf = NULL;
int ret = 0;
uint32_t size = calc_buf_size(msg);
@@ -245,7 +243,6 @@ int msg_send_and_wait(int fd, const struct msg *request, struct msg **response)
int msg_recv(int fd, struct msg **_msg)
{
- struct msg *msg = NULL;
struct buf *buf = NULL;
int ret = 0;
@@ -253,13 +250,12 @@ int msg_recv(int fd, struct msg **_msg)
if (ret < 0)
return ret;
- *_msg = malloc(sizeof(struct msg));
- if (!*_msg) {
+ struct msg *msg = malloc(sizeof(struct msg));
+ if (!msg) {
log_errno("malloc");
ret = -1;
goto destroy_buf;
}
- msg = *_msg;
msg->argc = calc_argv_len(buf_get_data(buf), buf_get_size(buf));
@@ -267,6 +263,7 @@ int msg_recv(int fd, struct msg **_msg)
if (ret < 0)
goto free_msg;
+ *_msg = msg;
goto destroy_buf;
free_msg:
diff --git a/src/net.c b/src/net.c
index 08cc763..0a6ef85 100644
--- a/src/net.c
+++ b/src/net.c
@@ -20,9 +20,9 @@
int net_bind(const char *port)
{
- struct addrinfo *result, *it = NULL;
+ struct addrinfo *result = NULL, *it = NULL;
struct addrinfo hints;
- int socket_fd, ret = 0;
+ int socket_fd = -1, ret = 0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
@@ -104,9 +104,9 @@ int net_accept(int fd)
int net_connect(const char *host, const char *port)
{
- struct addrinfo *result, *it = NULL;
+ struct addrinfo *result = NULL, *it = NULL;
struct addrinfo hints;
- int socket_fd, ret = 0;
+ int socket_fd = -1, ret = 0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
@@ -205,15 +205,13 @@ struct buf {
int buf_create(struct buf **_buf, const void *data, uint32_t size)
{
- struct buf *buf;
int ret = 0;
- *_buf = malloc(sizeof(struct buf));
- if (!*_buf) {
+ struct buf *buf = malloc(sizeof(struct buf));
+ if (!buf) {
log_errno("malloc");
return -1;
}
- buf = *_buf;
buf->data = malloc(size);
if (!buf->data) {
@@ -224,6 +222,7 @@ int buf_create(struct buf **_buf, const void *data, uint32_t size)
buf->size = size;
memcpy(buf->data, data, size);
+ *_buf = buf;
return ret;
free:
@@ -250,10 +249,9 @@ void *buf_get_data(const struct buf *buf)
int net_send_buf(int fd, const struct buf *buf)
{
- uint32_t size;
int ret = 0;
- size = htonl(buf->size);
+ uint32_t size = htonl(buf->size);
ret = net_send_all(fd, &size, sizeof(size));
if (ret < 0)
return ret;
@@ -267,8 +265,7 @@ int net_send_buf(int fd, const struct buf *buf)
int net_recv_buf(int fd, struct buf **buf)
{
- void *data;
- uint32_t size;
+ uint32_t size = 0;
int ret = 0;
ret = net_recv_all(fd, &size, sizeof(size));
@@ -278,7 +275,7 @@ int net_recv_buf(int fd, struct buf **buf)
}
size = ntohl(size);
- data = malloc(size);
+ void *data = malloc(size);
if (!data) {
log_errno("malloc");
goto fail;
diff --git a/src/server.c b/src/server.c
index 806da11..1efbbb5 100644
--- a/src/server.c
+++ b/src/server.c
@@ -35,16 +35,14 @@ struct server {
int server_create(struct server **_server, const struct settings *settings)
{
- struct server *server;
struct storage_settings storage_settings;
int ret = 0;
- *_server = malloc(sizeof(struct server));
- if (!*_server) {
+ struct server *server = malloc(sizeof(struct server));
+ if (!server) {
log_errno("malloc");
return -1;
}
- server = *_server;
ret = pthread_mutex_init(&server->server_mtx, NULL);
if (ret) {
@@ -75,6 +73,7 @@ int server_create(struct server **_server, const struct settings *settings)
ci_queue_create(&server->ci_queue);
+ *_server = server;
return ret;
destroy_storage:
@@ -111,7 +110,7 @@ static int server_has_runs(const struct server *server)
static int worker_ci_run(int fd, const struct ci_queue_entry *ci_run)
{
- struct msg *request, *response;
+ struct msg *request = NULL, *response = NULL;
int ret = 0;
const char *argv[] = {CMD_CI_RUN, ci_queue_entry_get_url(ci_run),
@@ -242,7 +241,7 @@ static int msg_new_worker_handler(int client_fd, UNUSED const struct msg *reques
static int msg_ci_run_queue(struct server *server, const char *url, const char *rev)
{
- struct ci_queue_entry *entry;
+ struct ci_queue_entry *entry = NULL;
int ret = 0;
ret = pthread_mutex_lock(&server->server_mtx);
diff --git a/src/server_main.c b/src/server_main.c
index 332ae17..0e06a67 100644
--- a/src/server_main.c
+++ b/src/server_main.c
@@ -62,7 +62,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
int main(int argc, char *argv[])
{
struct settings settings;
- struct server *server;
+ struct server *server = NULL;
int ret = 0;
ret = parse_settings(&settings, argc, argv);
diff --git a/src/sqlite.c b/src/sqlite.c
index 0c58b0e..9134528 100644
--- a/src/sqlite.c
+++ b/src/sqlite.c
@@ -138,13 +138,11 @@ int sqlite_column_int(sqlite3_stmt *stmt, int index)
return sqlite3_column_int(stmt, index);
}
-int sqlite_column_text(sqlite3_stmt *stmt, int index, char **result)
+int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result)
{
- const unsigned char *value;
- size_t nb;
int ret = 0;
- value = sqlite3_column_text(stmt, index);
+ const unsigned char *value = sqlite3_column_text(stmt, index);
if (!value) {
ret = sqlite3_errcode(sqlite3_db_handle(stmt));
if (ret) {
@@ -152,30 +150,29 @@ int sqlite_column_text(sqlite3_stmt *stmt, int index, char **result)
return ret;
}
- *result = NULL;
+ *_result = NULL;
return 0;
}
ret = sqlite3_column_bytes(stmt, index);
- nb = (size_t)ret;
+ size_t nb = (size_t)ret;
- *result = calloc(nb + 1, 1);
- if (!*result) {
+ char *result = calloc(nb + 1, 1);
+ if (!result) {
log_errno("calloc");
return -1;
}
+ memcpy(result, value, nb);
- memcpy(*result, value, nb);
+ *_result = result;
return 0;
}
-int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **result)
+int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result)
{
- const unsigned char *value;
- size_t nb;
int ret = 0;
- value = sqlite3_column_blob(stmt, index);
+ const unsigned char *value = sqlite3_column_blob(stmt, index);
if (!value) {
ret = sqlite3_errcode(sqlite3_db_handle(stmt));
if (ret) {
@@ -183,36 +180,34 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **result)
return ret;
}
- *result = NULL;
+ *_result = NULL;
return 0;
}
ret = sqlite3_column_bytes(stmt, index);
- nb = (size_t)ret;
+ size_t nb = (size_t)ret;
- *result = malloc(nb);
- if (!*result) {
+ unsigned char *result = malloc(nb);
+ if (!result) {
log_errno("malloc");
return -1;
}
+ memcpy(result, value, nb);
- memcpy(*result, value, nb);
+ *_result = result;
return 0;
}
int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt)
{
static const char *const FMT = "BEGIN; %s COMMIT;";
-
- char *full_stmt;
- size_t nb;
int ret = 0;
ret = snprintf(NULL, 0, FMT, stmt);
- nb = (size_t)ret + 1;
+ size_t nb = (size_t)ret + 1;
ret = 0;
- full_stmt = malloc(nb);
+ char *full_stmt = malloc(nb);
if (!full_stmt) {
log_errno("malloc");
return -1;
@@ -230,8 +225,8 @@ free:
int sqlite_get_user_version(sqlite3 *db, unsigned int *output)
{
- sqlite3_stmt *stmt;
- int result, ret = 0;
+ sqlite3_stmt *stmt = NULL;
+ int result = -1, ret = 0;
ret = sqlite_prepare(db, "PRAGMA user_version;", &stmt);
if (ret < 0)
diff --git a/src/storage_sqlite.c b/src/storage_sqlite.c
index c56aef5..0bd2e88 100644
--- a/src/storage_sqlite.c
+++ b/src/storage_sqlite.c
@@ -23,9 +23,7 @@ struct storage_settings_sqlite {
int storage_settings_create_sqlite(struct storage_settings *settings, const char *path)
{
- struct storage_settings_sqlite *sqlite;
-
- sqlite = malloc(sizeof(struct storage_settings_sqlite));
+ struct storage_settings_sqlite *sqlite = malloc(sizeof(struct storage_settings_sqlite));
if (!sqlite) {
log_errno("malloc");
return -1;
@@ -61,18 +59,14 @@ static int storage_upgrade_sqlite_to(struct storage_sqlite *storage, size_t vers
{
static const char *const FMT = "%s PRAGMA user_version = %zu;";
- const char *script;
- char *full_script;
- size_t nb;
+ const char *script = sql_sqlite_files[version];
int ret = 0;
- script = sql_sqlite_files[version];
-
ret = snprintf(NULL, 0, FMT, script, version + 1);
- nb = (size_t)ret + 1;
+ size_t nb = (size_t)ret + 1;
ret = 0;
- full_script = malloc(nb);
+ char *full_script = malloc(nb);
if (!full_script) {
log_errno("malloc");
return -1;
@@ -106,8 +100,7 @@ static int storage_upgrade_sqlite_from_to(struct storage_sqlite *storage, size_t
static int storage_upgrade_sqlite(struct storage_sqlite *storage)
{
- size_t newest_version;
- unsigned int current_version;
+ unsigned int current_version = 0;
int ret = 0;
ret = sqlite_get_user_version(storage->db, &current_version);
@@ -115,7 +108,7 @@ static int storage_upgrade_sqlite(struct storage_sqlite *storage)
return ret;
log("SQLite database version: %u\n", current_version);
- newest_version = sizeof(sql_sqlite_files) / sizeof(sql_sqlite_files[0]);
+ size_t newest_version = sizeof(sql_sqlite_files) / sizeof(sql_sqlite_files[0]);
log("Newest database version: %zu\n", newest_version);
if (current_version > newest_version) {
@@ -152,8 +145,8 @@ int storage_create_sqlite(struct storage *storage, const struct storage_settings
log("Using SQLite database at %s\n", settings->sqlite->path);
- storage->sqlite = malloc(sizeof(storage->sqlite));
- if (!storage->sqlite) {
+ struct storage_sqlite *sqlite = malloc(sizeof(struct storage_sqlite));
+ if (!sqlite) {
log_errno("malloc");
return -1;
}
@@ -161,13 +154,14 @@ int storage_create_sqlite(struct storage *storage, const struct storage_settings
ret = sqlite_init();
if (ret < 0)
goto free;
- ret = sqlite_open_rw(settings->sqlite->path, &storage->sqlite->db);
+ ret = sqlite_open_rw(settings->sqlite->path, &sqlite->db);
if (ret < 0)
goto destroy;
- ret = storage_prepare_sqlite(storage->sqlite);
+ ret = storage_prepare_sqlite(sqlite);
if (ret < 0)
goto close;
+ storage->sqlite = sqlite;
return ret;
close:
@@ -175,7 +169,7 @@ close:
destroy:
sqlite_destroy();
free:
- free(storage->sqlite);
+ free(sqlite);
return ret;
}
diff --git a/src/tcp_server.c b/src/tcp_server.c
index acd3320..25a74a7 100644
--- a/src/tcp_server.c
+++ b/src/tcp_server.c
@@ -21,21 +21,20 @@ struct tcp_server {
int tcp_server_create(struct tcp_server **_server, const char *port)
{
- struct tcp_server *server;
int ret = 0;
- *_server = malloc(sizeof(struct tcp_server));
- if (!*_server) {
+ struct tcp_server *server = malloc(sizeof(struct tcp_server));
+ if (!server) {
log_errno("malloc");
return -1;
}
- server = *_server;
ret = net_bind(port);
if (ret < 0)
goto free;
server->fd = ret;
+ *_server = server;
return ret;
free:
@@ -75,29 +74,29 @@ close:
int tcp_server_accept(const struct tcp_server *server, tcp_server_conn_handler handler, void *arg)
{
- struct child_context *ctx;
pthread_attr_t child_attr;
sigset_t old_mask;
pthread_t child;
- int conn_fd, ret = 0;
-
- ret = net_accept(server->fd);
- if (ret < 0)
- return ret;
- conn_fd = ret;
+ int ret = 0;
- ctx = malloc(sizeof(*ctx));
+ struct child_context *ctx = calloc(1, sizeof(*ctx));
if (!ctx) {
log_errno("malloc");
- ret = -1;
- goto close_conn;
+ return -1;
}
- *ctx = (struct child_context){conn_fd, handler, arg};
+
+ ctx->handler = handler;
+ ctx->arg = arg;
+
+ ret = net_accept(server->fd);
+ if (ret < 0)
+ goto free_ctx;
+ ctx->fd = ret;
ret = pthread_attr_init(&child_attr);
if (ret) {
pthread_errno(ret, "pthread_attr_init");
- goto free_ctx;
+ goto close_conn;
}
ret = pthread_attr_setdetachstate(&child_attr, PTHREAD_CREATE_DETACHED);
@@ -128,11 +127,11 @@ restore_mask:
destroy_attr:
pthread_errno_if(pthread_attr_destroy(&child_attr), "pthread_attr_destroy");
+close_conn:
+ log_errno_if(close(ctx->fd), "close");
+
free_ctx:
free(ctx);
-close_conn:
- log_errno_if(close(conn_fd), "close");
-
return ret;
}
diff --git a/src/worker.c b/src/worker.c
index ae9a578..8c731f2 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -32,15 +32,13 @@ struct worker {
int worker_create(struct worker **_worker, const struct settings *settings)
{
- struct worker *worker;
int ret = 0;
- *_worker = malloc(sizeof(struct worker));
- if (!*_worker) {
+ struct worker *worker = malloc(sizeof(struct worker));
+ if (!worker) {
log_errno("malloc");
return -1;
}
- worker = *_worker;
ret = libgit_init();
if (ret < 0)
@@ -51,6 +49,7 @@ int worker_create(struct worker **_worker, const struct settings *settings)
goto git_shutdown;
worker->fd = ret;
+ *_worker = worker;
return ret;
git_shutdown:
@@ -74,7 +73,7 @@ void worker_destroy(struct worker *worker)
static int msg_send_new_worker(const struct worker *worker)
{
static const char *argv[] = {CMD_NEW_WORKER, NULL};
- struct msg *msg;
+ struct msg *msg = NULL;
int ret = 0;
ret = msg_from_argv(&msg, argv);
@@ -152,7 +151,7 @@ int worker_main(struct worker *worker, UNUSED int argc, UNUSED char *argv[])
return ret;
while (!global_stop_flag) {
- struct msg *request;
+ struct msg *request = NULL;
log("Waiting for a new command\n");
diff --git a/src/worker_main.c b/src/worker_main.c
index fc3d0a2..01f87b6 100644
--- a/src/worker_main.c
+++ b/src/worker_main.c
@@ -62,7 +62,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
int main(int argc, char *argv[])
{
struct settings settings;
- struct worker *worker;
+ struct worker *worker = NULL;
int ret = 0;
ret = parse_settings(&settings, argc, argv);