From f1ce45bf39738dc1da4863f381b08d682050d146 Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Fri, 14 Sep 2018 17:34:05 +0800 Subject: [PATCH] fix some leak bugs found by valgrind --- .gitignore | 2 ++ src/gateway.c | 25 +++++++++++++++---------- src/lan-client.c | 5 +++-- src/lan-play.h | 1 + src/proxy.c | 6 ++++-- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index e9710b5..2422658 100755 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ CMakeSettings.json .DS_Store run.sh debug.sh +memcheck.sh +valgrind_ignore diff --git a/src/gateway.c b/src/gateway.c index 849d619..ffef876 100644 --- a/src/gateway.c +++ b/src/gateway.c @@ -89,7 +89,7 @@ void conn_free(conn_t *conn) { if (conn->sclosed && conn->dclosed) { LLOG(LLOG_DEBUG, "conn_kill %p done", conn); - // free(conn); + free(conn); } } @@ -152,18 +152,14 @@ void write_cb(uvl_write_t *req, int status) void p_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) { - if (p_read_cb <= 0) { - LLOG(LLOG_DEBUG, "p_read_cb %d", nread); - printf("p_read_cb %d %s\n", nread, uv_strerror(nread)); - return; - } - uv_read_stop(handle); - conn_t *conn = handle->data; - if (nread <= 0) { + if (p_read_cb <= 0) { + LLOG(LLOG_DEBUG, "p_read_cb %d %s", nread, uv_strerror(nread)); + free(buf->base); conn_kill(conn); return; } + uv_read_stop(handle); uvl_write_t *req = &conn->uvl_req; @@ -178,6 +174,7 @@ void read_cb(uvl_tcp_t *handle, ssize_t nread, const uv_buf_t *buf) conn_t *conn = handle->data; if (nread <= 0) { LLOG(LLOG_DEBUG, "read_cb %d", nread); + free(buf->base); conn_kill(conn); return; } @@ -208,12 +205,15 @@ static void p_on_connect(uv_connect_t *req, int status) conn_t *conn = req->data; if (status) { conn_kill(conn); + free(req); return; } int ret; ret = uv_read_start((uv_stream_t *)&conn->dtcp, p_alloc_cb, p_read_cb); LLOG(LLOG_DEBUG, "p_on_connect %d", ret); + + free(req); assert(ret == 0); assert(uvl_read_start(&conn->stcp, alloc_cb, read_cb) == 0); } @@ -225,6 +225,7 @@ void on_connect(uvl_t *handle, int status) conn_t *conn = malloc(sizeof(conn_t)); uv_connect_t *req = malloc(sizeof(uv_connect_t)); uvl_tcp_t *client = &conn->stcp; + int ret; conn->stcp.data = conn; conn->dtcp.data = conn; @@ -244,7 +245,11 @@ void on_connect(uvl_t *handle, int status) PRINT_IP(&client->local_addr.sin_addr); printf(" %d ", ntohs(client->local_addr.sin_port)); putchar('\n'); - uv_tcp_connect(req, &conn->dtcp, (struct sockaddr *)&client->local_addr, p_on_connect); + ret = uv_tcp_connect(req, &conn->dtcp, (struct sockaddr *)&client->local_addr, p_on_connect); + if (ret) { + LLOG(LLOG_WARNING, "uv_tcp_connect failed %d %s", ret, uv_strerror(ret)); + free(req); + } } int gateway_uvl_output(uvl_t *handle, const uv_buf_t bufs[], unsigned int nbufs) diff --git a/src/lan-client.c b/src/lan-client.c index 5539493..05082ba 100644 --- a/src/lan-client.c +++ b/src/lan-client.c @@ -116,9 +116,10 @@ int lan_client_send(struct lan_play *lan_play, const uint8_t type, const void *p struct sockaddr *server_addr = (struct sockaddr *)&lan_play->server_addr; int ret; uv_buf_t *bufs = lan_play->client_send_buf; + lan_play->client_send_type = type; int bufs_len = 1; - bufs[0] = uv_buf_init((char *)&type, sizeof(type)); - if (packet) { + bufs[0] = uv_buf_init((char *)&lan_play->client_send_type, sizeof(type)); + if (packet && len > 0) { bufs[1] = uv_buf_init((char *)packet, len); bufs_len = 2; } diff --git a/src/lan-play.h b/src/lan-play.h index a1e960b..a1c2083 100644 --- a/src/lan-play.h +++ b/src/lan-play.h @@ -39,6 +39,7 @@ struct lan_play { uv_udp_t client; uv_timer_t client_keepalive_timer; uv_buf_t client_send_buf[2]; + uint8_t client_send_type; struct sockaddr_in server_addr; struct gateway gateway; diff --git a/src/proxy.c b/src/proxy.c index bfaaf62..38a5b6f 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -5,7 +5,7 @@ #include "ipv4/ipv4.h" #include #include -#if 1 +#if 0 #define malloc(size) ({ \ void *__ptr = malloc(size); \ LLOG(LLOG_DEBUG, "[malloc] %p %d %d", __ptr, size, __LINE__); \ @@ -32,7 +32,7 @@ static void proxy_udp_recv_cb(uv_udp_t *udp, ssize_t nread, const uv_buf_t *buf, { if (nread <= 0) { LLOG(LLOG_DEBUG, "proxy_udp_recv_cb nread: %d", nread); - return; + goto out; } struct proxy_udp_item *item = (struct proxy_udp_item *)udp->data; @@ -49,6 +49,8 @@ static void proxy_udp_recv_cb(uv_udp_t *udp, ssize_t nread, const uv_buf_t *buf, if (ret != 0) { LLOG(LLOG_ERROR, "proxy_udp_recv_cb %d", ret); } + +out: free(buf->base); }