aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/net.c
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/net.c
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 '')
-rw-r--r--src/net.c23
1 files changed, 10 insertions, 13 deletions
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;