Skip to content

Commit

Permalink
libnl_2: Fix memory leaks
Browse files Browse the repository at this point in the history
BUG: b/5532633

Change-Id: I271168764e26dc465d2442f5015338a3e9a479b8
Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
  • Loading branch information
Dmitry Shmidt committed Nov 1, 2011
1 parent d4f2976 commit d99fe5e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
10 changes: 4 additions & 6 deletions libnl_2/handlers.c
Expand Up @@ -39,16 +39,14 @@ struct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
struct nl_cb *nl_cb_clone(struct nl_cb *orig) struct nl_cb *nl_cb_clone(struct nl_cb *orig)
{ {
struct nl_cb *new_cb; struct nl_cb *new_cb;
int new_refcnt;


new_cb = nl_cb_alloc(NL_CB_DEFAULT); new_cb = nl_cb_alloc(NL_CB_DEFAULT);
if (new_cb == NULL) if (new_cb == NULL)
goto fail; goto fail;


/* Preserve reference count and copy original */ /* Copy original and set refcount to 1 */
new_refcnt = new_cb->cb_refcnt;
memcpy(new_cb, orig, sizeof(*orig)); memcpy(new_cb, orig, sizeof(*orig));
new_cb->cb_refcnt = new_refcnt; new_cb->cb_refcnt = 1;


return new_cb; return new_cb;
fail: fail:
Expand Down Expand Up @@ -84,9 +82,9 @@ struct nl_cb *nl_cb_get(struct nl_cb *cb)


void nl_cb_put(struct nl_cb *cb) void nl_cb_put(struct nl_cb *cb)
{ {
if (!cb)
return;
cb->cb_refcnt--; cb->cb_refcnt--;
if (cb->cb_refcnt <= 0) if (cb->cb_refcnt <= 0)
free(cb); free(cb);

} }

16 changes: 8 additions & 8 deletions libnl_2/netlink.c
Expand Up @@ -59,15 +59,14 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, \
{ {
int rc = -1; int rc = -1;
int sk_flags; int sk_flags;
int RECV_BUF_SIZE; int RECV_BUF_SIZE = getpagesize();
int errsv; int errsv;
struct iovec recvmsg_iov; struct iovec recvmsg_iov;
struct msghdr msg; struct msghdr msg;


/* Allocate buffer */ /* Allocate buffer */
RECV_BUF_SIZE = getpagesize();
*buf = (unsigned char *) malloc(RECV_BUF_SIZE); *buf = (unsigned char *) malloc(RECV_BUF_SIZE);
if (!buf) { if (!(*buf)) {
rc = -ENOMEM; rc = -ENOMEM;
goto fail; goto fail;
} }
Expand All @@ -91,8 +90,11 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, \
errsv = errno; errsv = errno;
fcntl(sk->s_fd, F_SETFL, sk_flags); fcntl(sk->s_fd, F_SETFL, sk_flags);


if (rc < 0) if (rc < 0) {
rc = -errsv; rc = -errsv;
free(*buf);
*buf = NULL;
}


fail: fail:
return rc; return rc;
Expand All @@ -108,7 +110,6 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
int rc, cb_rc = NL_OK, done = 0; int rc, cb_rc = NL_OK, done = 0;


do { do {

unsigned char *buf; unsigned char *buf;
int i, rem, flags; int i, rem, flags;
struct nlmsghdr *nlh; struct nlmsghdr *nlh;
Expand All @@ -127,7 +128,7 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)


/* Check for callbacks */ /* 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)); memset(msg, 0, sizeof(*msg));
msg->nm_nlh = nlh; msg->nm_nlh = nlh;


Expand Down Expand Up @@ -187,7 +188,6 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
if (done) if (done)
break; break;
} }

free(buf); free(buf);
buf = NULL; buf = NULL;


Expand All @@ -197,7 +197,7 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)


success: success:
fail: fail:
return rc; return rc;
} }


/* Send raw data over netlink socket */ /* Send raw data over netlink socket */
Expand Down
35 changes: 22 additions & 13 deletions libnl_2/socket.c
Expand Up @@ -31,21 +31,21 @@ int nl_socket_add_membership(struct nl_sock *sk, int group)
} }


/* Allocate new netlink socket. */ /* Allocate new netlink socket. */
struct nl_sock *nl_socket_alloc(void) static struct nl_sock *_nl_socket_alloc(void)
{ {
struct nl_sock *sk; struct nl_sock *sk;
struct timeval tv; struct timeval tv;
struct nl_cb *cb; struct nl_cb *cb;


sk = (struct nl_sock *) malloc(sizeof(struct nl_sock)); sk = (struct nl_sock *) malloc(sizeof(struct nl_sock));
if (!sk) if (!sk)
goto fail; return NULL;
memset(sk, 0, sizeof(*sk)); memset(sk, 0, sizeof(*sk));


/* Get current time */ /* Get current time */


if (gettimeofday(&tv, NULL)) if (gettimeofday(&tv, NULL))
return NULL; goto fail;
else else
sk->s_seq_next = (int) tv.tv_sec; sk->s_seq_next = (int) tv.tv_sec;


Expand All @@ -59,32 +59,43 @@ struct nl_sock *nl_socket_alloc(void)
sk->s_peer.nl_pid = 0; /* Kernel */ sk->s_peer.nl_pid = 0; /* Kernel */
sk->s_peer.nl_groups = 0; /* No groups */ sk->s_peer.nl_groups = 0; /* No groups */


cb = (struct nl_cb *) malloc(sizeof(struct nl_cb)); return sk;
if (!cb) fail:
goto cb_fail; free(sk);
memset(cb, 0, sizeof(*cb)); return NULL;
sk->s_cb = nl_cb_alloc(NL_CB_DEFAULT); }


/* 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; return sk;
cb_fail: cb_fail:
free(sk); free(sk);
fail:
return NULL; return NULL;
} }


/* Allocate new socket with custom callbacks. */ /* Allocate new socket with custom callbacks. */
struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb) 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) if (!sk)
return NULL; return NULL;


sk->s_cb = cb; sk->s_cb = cb;
nl_cb_get(cb); nl_cb_get(cb);


return sk; return sk;

} }


/* Free a netlink socket. */ /* Free a netlink socket. */
Expand Down Expand Up @@ -116,5 +127,3 @@ int nl_socket_get_fd(struct nl_sock *sk)
{ {
return sk->s_fd; return sk->s_fd;
} }


0 comments on commit d99fe5e

Please sign in to comment.