Skip to contentSkip to conversation

client: Fix memory leak connected to params

PATCH In review Bartosz Szatkowski 2 participants 4 messages Nov 21, 2011 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 client: Fix memory leak connected to params

obc_transfer_params (created in obc_session_get) are reused to store params received in the response. But actual parameter data were not freed before g_memdup in get_buf_xfer_progress.

---
 client/transfer.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
client/transfer.c +3 -0
index b6994d1..e072a42 100644
@@ -356,6 +356,9 @@ static void get_buf_xfer_progress(GObex *obex, GError *err, GObexPacket *rsp,
 	if (hdr) {
 		g_obex_header_get_bytes(hdr, &buf, &len);
 		if (len != 0) {
+			if (transfer->params->data != NULL)
+				g_free(transfer->params->data);
+
 			transfer->params->data = g_memdup(buf, len);
 			transfer->params->size = len;
 		}
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH 2/2 client: Fix invalid write in get_buf_xfer_progress

Segmentation fault occurred when there were no (NULL) params in obc_session_get, but actual params were returned in response, as corresponding structure is reused - but not created in the first place.

---
 client/session.c  |    2 +-
 client/transfer.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
client/session.c +1 -1
index f288ecd..e51faf1 100644
@@ -1231,8 +1231,8 @@ int obc_session_get(struct obc_session *session, const char *type,
 	if (session->obex == NULL)
 		return -ENOTCONN;
 
+	params = g_new0(struct obc_transfer_params, 1);
 	if (apparam != NULL) {
-		params = g_new0(struct obc_transfer_params, 1);
 		params->data = g_new(guint8, apparam_size);
 		memcpy(params->data, apparam, apparam_size);
 		params->size = apparam_size;
client/transfer.c +1 -1
index e072a42..8340cc3 100644
@@ -536,7 +536,7 @@ int obc_transfer_get(struct obc_transfer *transfer, transfer_callback_t func,
 		g_obex_packet_add_bytes(req, G_OBEX_HDR_TYPE, transfer->type,
 						strlen(transfer->type) + 1);
 
-	if (transfer->params != NULL)
+	if (transfer->params != NULL && transfer->params->size != 0)
 		g_obex_packet_add_bytes(req, G_OBEX_HDR_APPARAM,
 						transfer->params->data,
 						transfer->params->size);
Signature · 1 line
1.7.4.1
#2 lore raw

Re: [PATCH obexd 2/2] client: Fix invalid write in get_buf_xfer_progress

Hi Bartosz,

Quoted 36 lines · Bartosz Szatkowski

Segmentation fault occurred when there were no (NULL) params in obc_session_get, but actual params were returned in response, as corresponding structure is reused - but not created in the first place.
---
 client/session.c  |    2 +-
 client/transfer.c |    2 +-

 2 files changed, 2 insertions(+), 2 deletions(-)
client/session.c +1 -1
index f288ecd..e51faf1 100644
@@ -1231,8 +1231,8 @@ int obc_session_get(struct obc_session *session, const char *type,
        if (session->obex == NULL)
                return -ENOTCONN;
 
+       params = g_new0(struct obc_transfer_params, 1);
        if (apparam != NULL) {
-               params = g_new0(struct obc_transfer_params, 1);
                params->data = g_new(guint8, apparam_size);
                memcpy(params->data, apparam, apparam_size);
                params->size = apparam_size;
client/transfer.c +1 -2
index e072a42..8340cc3 100644
@@ -536,7 +536,7 @@ int obc_transfer_get(struct obc_transfer *transfer, transfer_callback_t func,
                g_obex_packet_add_bytes(req, G_OBEX_HDR_TYPE, transfer->type,
                                                strlen(transfer->type) + 1);
 
-       if (transfer->params != NULL)
+       if (transfer->params != NULL && transfer->params->size != 0)
                g_obex_packet_add_bytes(req, G_OBEX_HDR_APPARAM,
                                                transfer->params->data,
                                                transfer->params->size);
--
Quoted 2 lines

1.7.4.1

After some offline discussion with Johan we think it is better to have this allocation completely on demand, so it means we should not allocate it before hand if the request doesn't have any apparam and then if the response has it then we allocate on get_buf_xfer_progress.

Signature · 1 line
Luiz Augusto von Dentz
#3 lore raw

Re: [PATCH obexd 2/2] client: Fix invalid write in get_buf_xfer_progress

Quoted 47 lines · Luiz Augusto von Dentz

Hi Bartosz,

Quoted 36 lines · Bartosz Szatkowski

Segmentation fault occurred when there were no (NULL) params in obc_session_get, but actual params were returned in response, as corresponding structure is reused - but not created in the first place.
---
 client/session.c  |    2 +-
 client/transfer.c |    2 +-

 2 files changed, 2 insertions(+), 2 deletions(-)
client/session.c +1 -1
index f288ecd..e51faf1 100644
@@ -1231,8 +1231,8 @@ int obc_session_get(struct obc_session *session, const char *type,
        if (session->obex == NULL)
                return -ENOTCONN;
 
+       params = g_new0(struct obc_transfer_params, 1);
        if (apparam != NULL) {
-               params = g_new0(struct obc_transfer_params, 1);
                params->data = g_new(guint8, apparam_size);
                memcpy(params->data, apparam, apparam_size);
                params->size = apparam_size;
client/transfer.c +1 -2
index e072a42..8340cc3 100644
@@ -536,7 +536,7 @@ int obc_transfer_get(struct obc_transfer *transfer, transfer_callback_t func,
                g_obex_packet_add_bytes(req, G_OBEX_HDR_TYPE, transfer->type,
                                                strlen(transfer->type) + 1);
 
-       if (transfer->params != NULL)
+       if (transfer->params != NULL && transfer->params->size != 0)
                g_obex_packet_add_bytes(req, G_OBEX_HDR_APPARAM,
                                                transfer->params->data,
                                                transfer->params->size);
--
Quoted 2 lines

1.7.4.1

After some offline discussion with Johan we think it is better to have this allocation completely on demand, so it means we should not allocate it before hand if the request doesn't have any apparam and then if the response has it then we allocate on get_buf_xfer_progress.

Quoted 3 lines

--
Luiz Augusto von Dentz

Ok,
I'll send patch later this day.

Signature · 2 lines
Pozdrowienia - Cheers,
Bartosz Szatkowski