summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Shmidt2011-11-01 17:24:02 -0500
committerDmitry Shmidt2011-11-01 17:56:22 -0500
commitd99fe5ec960b5eae96585d5ceb59fe287fe0c5e7 (patch)
tree791ba9e6d24431c250d4f764b6934cdf41947aaa
parentd4f29767c8cd5a0d8b52b77bed117ca3746af8f3 (diff)
downloadplatform-system-core-d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7.tar.gz
platform-system-core-d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7.tar.xz
platform-system-core-d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7.zip
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.c10
-rw-r--r--libnl_2/netlink.c16
-rw-r--r--libnl_2/socket.c35
3 files changed, 34 insertions, 27 deletions
diff --git a/libnl_2/handlers.c b/libnl_2/handlers.c
index ec8d51270..48dcab4a8 100644
--- a/libnl_2/handlers.c
+++ b/libnl_2/handlers.c
@@ -39,16 +39,14 @@ fail:
39struct nl_cb *nl_cb_clone(struct nl_cb *orig) 39struct nl_cb *nl_cb_clone(struct nl_cb *orig)
40{ 40{
41 struct nl_cb *new_cb; 41 struct nl_cb *new_cb;
42 int new_refcnt;
43 42
44 new_cb = nl_cb_alloc(NL_CB_DEFAULT); 43 new_cb = nl_cb_alloc(NL_CB_DEFAULT);
45 if (new_cb == NULL) 44 if (new_cb == NULL)
46 goto fail; 45 goto fail;
47 46
48 /* Preserve reference count and copy original */ 47 /* Copy original and set refcount to 1 */
49 new_refcnt = new_cb->cb_refcnt;
50 memcpy(new_cb, orig, sizeof(*orig)); 48 memcpy(new_cb, orig, sizeof(*orig));
51 new_cb->cb_refcnt = new_refcnt; 49 new_cb->cb_refcnt = 1;
52 50
53 return new_cb; 51 return new_cb;
54fail: 52fail:
@@ -84,9 +82,9 @@ struct nl_cb *nl_cb_get(struct nl_cb *cb)
84 82
85void nl_cb_put(struct nl_cb *cb) 83void nl_cb_put(struct nl_cb *cb)
86{ 84{
85 if (!cb)
86 return;
87 cb->cb_refcnt--; 87 cb->cb_refcnt--;
88 if (cb->cb_refcnt <= 0) 88 if (cb->cb_refcnt <= 0)
89 free(cb); 89 free(cb);
90
91} 90}
92
diff --git a/libnl_2/netlink.c b/libnl_2/netlink.c
index cc2f88e68..ee3d600f7 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, \
59{ 59{
60 int rc = -1; 60 int rc = -1;
61 int sk_flags; 61 int sk_flags;
62 int RECV_BUF_SIZE; 62 int RECV_BUF_SIZE = getpagesize();
63 int errsv; 63 int errsv;
64 struct iovec recvmsg_iov; 64 struct iovec recvmsg_iov;
65 struct msghdr msg; 65 struct msghdr msg;
66 66
67 /* Allocate buffer */ 67 /* Allocate buffer */
68 RECV_BUF_SIZE = getpagesize();
69 *buf = (unsigned char *) malloc(RECV_BUF_SIZE); 68 *buf = (unsigned char *) malloc(RECV_BUF_SIZE);
70 if (!buf) { 69 if (!(*buf)) {
71 rc = -ENOMEM; 70 rc = -ENOMEM;
72 goto fail; 71 goto fail;
73 } 72 }
@@ -91,8 +90,11 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, \
91 errsv = errno; 90 errsv = errno;
92 fcntl(sk->s_fd, F_SETFL, sk_flags); 91 fcntl(sk->s_fd, F_SETFL, sk_flags);
93 92
94 if (rc < 0) 93 if (rc < 0) {
95 rc = -errsv; 94 rc = -errsv;
95 free(*buf);
96 *buf = NULL;
97 }
96 98
97fail: 99fail:
98 return rc; 100 return rc;
@@ -108,7 +110,6 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
108 int rc, cb_rc = NL_OK, done = 0; 110 int rc, cb_rc = NL_OK, done = 0;
109 111
110 do { 112 do {
111
112 unsigned char *buf; 113 unsigned char *buf;
113 int i, rem, flags; 114 int i, rem, flags;
114 struct nlmsghdr *nlh; 115 struct nlmsghdr *nlh;
@@ -127,7 +128,7 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
127 128
128 /* Check for callbacks */ 129 /* Check for callbacks */
129 130
130 msg = (struct nl_msg *)malloc(sizeof(struct nl_msg)); 131 msg = (struct nl_msg *) malloc(sizeof(struct nl_msg));
131 memset(msg, 0, sizeof(*msg)); 132 memset(msg, 0, sizeof(*msg));
132 msg->nm_nlh = nlh; 133 msg->nm_nlh = nlh;
133 134
@@ -187,7 +188,6 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
187 if (done) 188 if (done)
188 break; 189 break;
189 } 190 }
190
191 free(buf); 191 free(buf);
192 buf = NULL; 192 buf = NULL;
193 193
@@ -197,7 +197,7 @@ int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
197 197
198success: 198success:
199fail: 199fail:
200 return rc; 200 return rc;
201} 201}
202 202
203/* Send raw data over netlink socket */ 203/* Send raw data over netlink socket */
diff --git a/libnl_2/socket.c b/libnl_2/socket.c
index ce54f19ba..d906cac27 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)
31} 31}
32 32
33/* Allocate new netlink socket. */ 33/* Allocate new netlink socket. */
34struct nl_sock *nl_socket_alloc(void) 34static struct nl_sock *_nl_socket_alloc(void)
35{ 35{
36 struct nl_sock *sk; 36 struct nl_sock *sk;
37 struct timeval tv; 37 struct timeval tv;
@@ -39,13 +39,13 @@ struct nl_sock *nl_socket_alloc(void)
39 39
40 sk = (struct nl_sock *) malloc(sizeof(struct nl_sock)); 40 sk = (struct nl_sock *) malloc(sizeof(struct nl_sock));
41 if (!sk) 41 if (!sk)
42 goto fail; 42 return NULL;
43 memset(sk, 0, sizeof(*sk)); 43 memset(sk, 0, sizeof(*sk));
44 44
45 /* Get current time */ 45 /* Get current time */
46 46
47 if (gettimeofday(&tv, NULL)) 47 if (gettimeofday(&tv, NULL))
48 return NULL; 48 goto fail;
49 else 49 else
50 sk->s_seq_next = (int) tv.tv_sec; 50 sk->s_seq_next = (int) tv.tv_sec;
51 51
@@ -59,24 +59,36 @@ struct nl_sock *nl_socket_alloc(void)
59 sk->s_peer.nl_pid = 0; /* Kernel */ 59 sk->s_peer.nl_pid = 0; /* Kernel */
60 sk->s_peer.nl_groups = 0; /* No groups */ 60 sk->s_peer.nl_groups = 0; /* No groups */
61 61
62 cb = (struct nl_cb *) malloc(sizeof(struct nl_cb)); 62 return sk;
63 if (!cb) 63fail:
64 goto cb_fail; 64 free(sk);
65 memset(cb, 0, sizeof(*cb)); 65 return NULL;
66 sk->s_cb = nl_cb_alloc(NL_CB_DEFAULT); 66}
67 67
68/* Allocate new netlink socket. */
69struct nl_sock *nl_socket_alloc(void)
70{
71 struct nl_sock *sk = _nl_socket_alloc();
72 struct nl_cb *cb;
68 73
74 if (!sk)
75 return NULL;
76
77 cb = nl_cb_alloc(NL_CB_DEFAULT);
78 if (!cb)
79 goto cb_fail;
80 sk->s_cb = cb;
69 return sk; 81 return sk;
70cb_fail: 82cb_fail:
71 free(sk); 83 free(sk);
72fail:
73 return NULL; 84 return NULL;
74} 85}
75 86
76/* Allocate new socket with custom callbacks. */ 87/* Allocate new socket with custom callbacks. */
77struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb) 88struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb)
78{ 89{
79 struct nl_sock *sk = nl_socket_alloc(); 90 struct nl_sock *sk = _nl_socket_alloc();
91
80 if (!sk) 92 if (!sk)
81 return NULL; 93 return NULL;
82 94
@@ -84,7 +96,6 @@ struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb)
84 nl_cb_get(cb); 96 nl_cb_get(cb);
85 97
86 return sk; 98 return sk;
87
88} 99}
89 100
90/* Free a netlink socket. */ 101/* Free a netlink socket. */
@@ -116,5 +127,3 @@ int nl_socket_get_fd(struct nl_sock *sk)
116{ 127{
117 return sk->s_fd; 128 return sk->s_fd;
118} 129}
119
120