Skip to content

Commit

Permalink
fix some leak bugs found by valgrind
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemeowx2 committed Sep 14, 2018
1 parent c7c9514 commit f1ce45b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ CMakeSettings.json
.DS_Store
run.sh
debug.sh
memcheck.sh
valgrind_ignore
25 changes: 15 additions & 10 deletions src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/lan-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/lan-play.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "ipv4/ipv4.h"
#include <assert.h>
#include <base/llog.h>
#if 1
#if 0
#define malloc(size) ({ \
void *__ptr = malloc(size); \
LLOG(LLOG_DEBUG, "[malloc] %p %d %d", __ptr, size, __LINE__); \
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down

0 comments on commit f1ce45b

Please sign in to comment.