From 8fc0d16e853d99ef5ffb7596103446159b900761 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 12 Feb 2019 17:47:14 +0100 Subject: [PATCH] Fix naive ctx allocation --- src/algo_naive.c | 12 ++++++++---- src/net_tools.c | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/algo_naive.c b/src/algo_naive.c index c27b110..cdaddaa 100644 --- a/src/algo_naive.c +++ b/src/algo_naive.c @@ -115,6 +115,8 @@ void algo_naive(struct algo_skel* as) { as->on_tcp_read.free_app_ctx = free_naive; as->on_tcp_read.cb = tcp_to_udp; as->on_tcp_read.socklist = NULL; + if (as->on_tcp_read.app_ctx == NULL) goto init_err; + memset(as->on_tcp_read.app_ctx, 0, sizeof(struct naive_ctx)); as->on_udp_read.name = "udp-read"; as->on_udp_read.flags = EPOLLIN | EPOLLET; @@ -122,6 +124,8 @@ void algo_naive(struct algo_skel* as) { as->on_udp_read.free_app_ctx = free_naive; as->on_udp_read.cb = tcp_to_udp; as->on_udp_read.socklist = NULL; + if (as->on_udp_read.app_ctx == NULL) goto init_err; + memset(as->on_udp_read.app_ctx, 0, sizeof(struct naive_ctx)); as->on_tcp_write.name = "tcp-write"; as->on_tcp_write.flags = EPOLLOUT | EPOLLET; @@ -137,8 +141,8 @@ void algo_naive(struct algo_skel* as) { as->on_udp_write.cb = tcp_to_udp; as->on_udp_write.socklist = NULL; - if (as->on_tcp_read.app_ctx == NULL || as->on_udp_read.app_ctx == NULL) { - fprintf(stderr, "Failed to malloc naive_ctx\n"); - exit(EXIT_FAILURE); - } + return; +init_err: + fprintf(stderr, "Failed to init algo naive\n"); + exit(EXIT_FAILURE); } diff --git a/src/net_tools.c b/src/net_tools.c index 78dbdd5..8fdbf6c 100644 --- a/src/net_tools.c +++ b/src/net_tools.c @@ -66,11 +66,12 @@ int create_tcp_server(char* service) { sock = socket(cursor->ai_family, cursor->ai_socktype, cursor->ai_protocol); if (sock == -1) continue; if (bind(sock, cursor->ai_addr, cursor->ai_addrlen) != -1) break; + perror("bind failed"); close(sock); } if (cursor == NULL) { - fprintf(stderr, "We failed to bind\n"); + fprintf(stderr, "We failed to create socket or bind\n"); exit(EXIT_FAILURE); }