diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-05-13 10:58:41 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-05-13 11:37:46 +0200 |
commit | cd917f48454875ad6b7fc69455281d72760c44ee (patch) | |
tree | 70a7a43fe43b7f893468f9120def5513774a242c /src/command.c | |
parent | add command module to handle request-response communications (diff) | |
download | cimple-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 '')
-rw-r--r-- | src/command.c | 20 |
1 files changed, 9 insertions, 11 deletions
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); |