diff options
author | Dmitry Shmidt <dimitrysh@google.com> | 2011-11-01 15:24:02 -0700 |
---|---|---|
committer | Dmitry Shmidt <dimitrysh@google.com> | 2011-11-01 15:56:22 -0700 |
commit | d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7 (patch) | |
tree | 791ba9e6d24431c250d4f764b6934cdf41947aaa | |
parent | d4f29767c8cd5a0d8b52b77bed117ca3746af8f3 (diff) | |
download | system_core-d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7.zip system_core-d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7.tar.gz system_core-d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7.tar.bz2 |
libnl_2: Fix memory leaks
BUG: b/5532633
Change-Id: I271168764e26dc465d2442f5015338a3e9a479b8
Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
-rw-r--r-- | libnl_2/handlers.c | 10 | ||||
-rw-r--r-- | libnl_2/netlink.c | 16 | ||||
-rw-r--r-- | libnl_2/socket.c | 35 |
3 files changed, 34 insertions, 27 deletions
diff --git a/libnl_2/handlers.c b/libnl_2/handlers.c index ec8d512..48dcab4 100644 --- a/libnl_2/handlers.c +++ b/libnl_2/handlers.c @@ -39,16 +39,14 @@ fail: struct nl_cb *nl_cb_clone(struct nl_cb *orig) { struct nl_cb *new_cb; - int new_refcnt; new_cb = nl_cb_alloc(NL_CB_DEFAULT); if (new_cb == NULL) goto fail; - /* Preserve reference count and copy original */ - new_refcnt = new_cb->cb_refcnt; + /* Copy original and set refcount to 1 */ memcpy(new_cb, orig, sizeof(*orig)); - new_cb->cb_refcnt = new_refcnt; + new_cb->cb_refcnt = 1; return new_cb; fail: @@ -84,9 +82,9 @@ struct nl_cb *nl_cb_get(struct nl_cb *cb) void nl_cb_put(struct nl_cb *cb) { + if (!cb) + return; cb->cb_refcnt--; if (cb->cb_refcnt <= 0) free(cb); - } - diff --git a/libnl_2/netlink.c b/libnl_2/netlink.c index cc2f88e..ee3d600 100644 --- a/libnl_2/netlink.c +++ b/libnl_2/netlink.c @@ -59,15 +59,14 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, \ { int rc = -1; int sk_flags; - int RECV_BUF_SIZE; + int RECV_BUF_SIZE = getpagesize(); int errsv; struct iovec recvmsg_iov; struct msghdr msg; /* Allocate buffer */ - RECV_BUF_SIZE = getpagesize(); *buf = (unsigned char *) malloc(RECV_BUF_SIZE); - if (!buf) { + if (!(*buf)) { rc = -ENOMEM; goto fail; } @@ -91,8 +90,11 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, \ errsv = errno; fcntl(sk->s_fd, F_SETFL, sk_flags); - if (rc < 0) + if (rc < 0) { rc = -errsv; + free(*buf); + *buf = NULL; + } fail: return rc; @@ -108,7 +110,6 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb) int rc, cb_rc = NL_OK, done = 0; do { - unsigned char *buf; int i, rem, flags; struct nlmsghdr *nlh; @@ -127,7 +128,7 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb) /* Check for callbacks */ - msg = (struct nl_msg *)malloc(sizeof(struct nl_msg)); + msg = (struct nl_msg *) malloc(sizeof(struct nl_msg)); memset(msg, 0, sizeof(*msg)); msg->nm_nlh = nlh; @@ -187,7 +188,6 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb) if (done) break; } - free(buf); buf = NULL; @@ -197,7 +197,7 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb) success: fail: - return rc; + return rc; } /* Send raw data over netlink socket */ diff --git a/libnl_2/socket.c b/libnl_2/socket.c index ce54f19..d906cac 100644 --- a/libnl_2/socket.c +++ b/libnl_2/socket.c @@ -31,7 +31,7 @@ int nl_socket_add_membership(struct nl_sock *sk, int group) } /* Allocate new netlink socket. */ -struct nl_sock *nl_socket_alloc(void) +static struct nl_sock *_nl_socket_alloc(void) { struct nl_sock *sk; struct timeval tv; @@ -39,13 +39,13 @@ struct nl_sock *nl_socket_alloc(void) sk = (struct nl_sock *) malloc(sizeof(struct nl_sock)); if (!sk) - goto fail; + return NULL; memset(sk, 0, sizeof(*sk)); /* Get current time */ if (gettimeofday(&tv, NULL)) - return NULL; + goto fail; else sk->s_seq_next = (int) tv.tv_sec; @@ -59,24 +59,36 @@ struct nl_sock *nl_socket_alloc(void) sk->s_peer.nl_pid = 0; /* Kernel */ sk->s_peer.nl_groups = 0; /* No groups */ - cb = (struct nl_cb *) malloc(sizeof(struct nl_cb)); - if (!cb) - goto cb_fail; - memset(cb, 0, sizeof(*cb)); - sk->s_cb = nl_cb_alloc(NL_CB_DEFAULT); + return sk; +fail: + free(sk); + return NULL; +} +/* Allocate new netlink socket. */ +struct nl_sock *nl_socket_alloc(void) +{ + struct nl_sock *sk = _nl_socket_alloc(); + struct nl_cb *cb; + if (!sk) + return NULL; + + cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) + goto cb_fail; + sk->s_cb = cb; return sk; cb_fail: free(sk); -fail: return NULL; } /* Allocate new socket with custom callbacks. */ struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb) { - struct nl_sock *sk = nl_socket_alloc(); + struct nl_sock *sk = _nl_socket_alloc(); + if (!sk) return NULL; @@ -84,7 +96,6 @@ struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb) nl_cb_get(cb); return sk; - } /* Free a netlink socket. */ @@ -116,5 +127,3 @@ int nl_socket_get_fd(struct nl_sock *sk) { return sk->s_fd; } - - |