|
From: Peter K. <pk...@us...> - 2001-03-23 16:19:38
|
The following files were modified in apps/bluetooth/sdp_server:
Name Old version New version Comment
---- ----------- ----------- -------
sdp_parser.c 1.13 1.14=20=20=20=20=20=20=20=20=20=20=20=20
sdp_parser.h 1.4 1.5=20=20=20=20=20=20=20=20=20=20=20=20=20
The accompanying log:
* Removed two unnecessary malloc()'s.
* Fail with an error if one of the remaining malloc()'s fails.
* Use writev() in send2stack() to avoid malloc().
* Use sizeof instead of constants where appropriate.
The diff of the modified file(s):
--- sdp_parser.c 2001/03/23 16:06:18 1.13
+++ sdp_parser.c 2001/03/23 16:19:36 1.14
@@ -52,6 +52,7 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
+#include <sys/uio.h>
=20
#include "sdp_server.h"
#include "sdp_parser.h"
@@ -163,19 +164,22 @@
int sdp_con_id;
data_struct *db_hdl;
=20
+ PRINTPKT(FNC, data, len);=20=20
+
db_hdl =3D (data_struct*) data;
+ data +=3D sizeof *db_hdl;
+ len -=3D sizeof *db_hdl;
=20
- PRINTPKT(FNC, data, len);=20=20
sdp_con_id =3D db_hdl->sdp_con_id;
pkt_len =3D db_hdl->len;
=20
- id =3D db_hdl->data[0];
- trans_id =3D CHAR2INT16(db_hdl->data[1], db_hdl->data[2]);
- par_len =3D CHAR2INT16(db_hdl->data[3], db_hdl->data[4]);
+ id =3D data[0];
+ trans_id =3D CHAR2INT16(data[1], data[2]);
+ par_len =3D CHAR2INT16(data[3], data[4]);
=20
- if (par_len > (len - SDP_HDR_SIZE - sizeof(data_struct)))
+ if (par_len > (len - SDP_HDR_SIZE))
{
- D_XMIT(FNC"Packetlength don't match recievd data length par_len%d, pkt=
_len %d\n", par_len, len);
+ D_XMIT(FNC"Packet length does not match received data length par_len %=
d, pkt_len %d\n", par_len, len);
send_error_rsp(sdp_con_id, trans_id, SDP_INVALID_PDU_SIZE);
return;
}
@@ -185,7 +189,7 @@
switch (id)
{
case SDP_ERROR_RSP:
- switch (CHAR2INT16(db_hdl->data[5], db_hdl->data[6]))
+ switch (CHAR2INT16(data[5], data[6]))
{
case SDP_INVALID_SDP_VERSION:
D_ERR(FNC"Got Invalid/unsupported SDP version\n");
@@ -207,42 +211,39 @@
break;
default:
D_ERR(FNC"Error code 0x%04x is not specified\n",
- CHAR2INT16(db_hdl->data[5], db_hdl->data[6]));
+ CHAR2INT16(data[5], data[6]));
break;
}
break;
=20=09=09
case SDP_SERVICESEARCH_REQ:
D_REC(FNC"SDP_SERVICESEARCH_REQ %d bytes\n", par_len);
- process_service_search_req(sdp_con_id, db_hdl->data + 5, par_len,
- trans_id);
+ process_service_search_req(sdp_con_id, data + 5, par_len, trans_id);
break;
=20=20=20=20=20
case SDP_SERVICESEARCH_RSP:
D_REC(FNC"SDP_SERVICESEARCH_RSP\n");
- process_service_search_rsp(sdp_con_id, db_hdl->data + 5);
+ process_service_search_rsp(sdp_con_id, data + 5);
break;
=20=20=20=20=20
case SDP_SERVICEATTR_REQ:
D_REC(FNC"SDP_SERVICEATTR_REQ\n");
- process_service_attr_req(sdp_con_id, db_hdl->data + 5, par_len,
- trans_id);
+ process_service_attr_req(sdp_con_id, data + 5, par_len, trans_id);
break;
=20=20=20=20=20
case SDP_SERVICEATTR_RSP:
D_REC(FNC"SDP_SERVICEATTR_RSP\n");
- process_service_attr_rsp(sdp_con_id, db_hdl->data + 5);
+ process_service_attr_rsp(sdp_con_id, data + 5);
break;
=20=20=20=20=20
case SDP_SERVICESEARCHATTR_REQ:
D_REC(FNC"SDP_SERVICESEARCHATTR_REQ\n");
- process_service_search_attr_req(sdp_con_id, db_hdl->data + 5, par_len,
- trans_id);
+ process_service_search_attr_req(sdp_con_id, data + 5, par_len, trans_i=
d);
break;
=20=20=20=20=20
case SDP_SERVICESEARCHATTR_RSP:
D_REC(FNC"SDP_SERVICESEARCHATTR_RSP\n");
- process_service_search_attr_rsp(sdp_con_id, db_hdl->data + 5);
+ process_service_search_attr_rsp(sdp_con_id, data + 5);
break;
=20=20=20=20=20
default:
@@ -258,8 +259,6 @@
process_service_search_req(int sdp_con_id, unsigned char *data, unsigned s=
hort len, unsigned int trans_id)
{
#define FNC "process_service_search_req: "
- service_search_struct *db_hdl;
-=09
unsigned int service_search_uuid[12];
int service_search_uuid_cnt;
unsigned char des_len;
@@ -375,28 +374,20 @@
}
else
{
- len =3D sizeof(service_search_struct);
-=20=20=20=20
- db_hdl =3D (service_search_struct*) malloc(len);
- D_MEM("---> malloc%d %d bytes at 0x%08x\n", malloc_dbg++,len, (int)db_=
hdl);
+ service_search_struct db_hdl;
=20=20=20=20=20
- db_hdl->db.sdp_con_id =3D sdp_con_id;
- db_hdl->db.trans_id =3D trans_id;
- db_hdl->db.pkt_type =3D SDP_SERVICESEARCH_REQ;
+ db_hdl.db.sdp_con_id =3D sdp_con_id;
+ db_hdl.db.trans_id =3D trans_id;
+ db_hdl.db.pkt_type =3D SDP_SERVICESEARCH_REQ;
=20=20=20=20=20
- db_hdl->max_rec_cnt =3D max_rec_cnt;
- db_hdl->service_class_cnt =3D service_search_uuid_cnt;
- memcpy(db_hdl->service_class_list, service_search_uuid,
- service_search_uuid_cnt * 4);
+ db_hdl.max_rec_cnt =3D max_rec_cnt;
+ db_hdl.service_class_cnt =3D service_search_uuid_cnt;
+ memcpy(db_hdl.service_class_list, service_search_uuid,
+ service_search_uuid_cnt * sizeof *service_search_uuid);
=20=20=20=20=20
/* Here we ask the database for the requested attributes */
-=20=20=20=20
- handle_query((database_query_struct*) db_hdl);
-=20=20=20=20
- D_MEM("<--- free%d 0x%08x\n",malloc_dbg--, (int) db_hdl);
- free(db_hdl);
+ handle_query(&db_hdl.db);
}
-=20=20=20=20
#undef FNC
}
=20
@@ -508,9 +499,14 @@
}
else
{
- len =3D sizeof(service_attr_struct) + attr_list_pos * 4;
+ len =3D sizeof(service_attr_struct) + attr_list_pos * sizeof *attr_lis=
t;
=20=09
- db_hdl =3D (service_attr_struct*) malloc(len);
+ if (!(db_hdl =3D malloc(len))) {
+ D_ERR(__FUNCTION__ ": malloc failed to allocate %d bytes!\n", =
len);
+ send_error_rsp(sdp_con_id, trans_id, SDP_INSUFFICIENT_RESOURCE=
S);
+ return;
+ }
+
D_MEM("---> malloc%d %d bytes at 0x%08x\n",malloc_dbg++, len, (int)db_=
hdl);
=20=20=20=20=20
db_hdl->db.sdp_con_id =3D sdp_con_id;
@@ -520,11 +516,11 @@
db_hdl->max_attr_byte_cnt =3D max_attr_cnt;
db_hdl->rec_hdl =3D rec_hdl;
db_hdl->attr_cnt =3D attr_list_pos;
- memcpy(db_hdl->attr_list, attr_list, attr_list_pos * 4);
+ memcpy(db_hdl->attr_list, attr_list, attr_list_pos * sizeof *attr_list=
);
=20=20=20=20=20
/* Here we ask the database for the requested attributes */
=20=20=20=20=20
- handle_query((database_query_struct*) db_hdl);
+ handle_query(&db_hdl->db);
=20=20=20=20=20
D_MEM("<--- free%d 0x%08x\n",malloc_dbg--, (int) db_hdl);
free(db_hdl);
@@ -697,9 +693,14 @@
}
else
{
- tmp_len =3D sizeof(service_search_attr_struct) + attr_list_pos * 4;
+ tmp_len =3D sizeof(service_search_attr_struct) + attr_list_pos * sizeo=
f *attr_list;
=20=09
- db_hdl =3D (service_search_attr_struct*) malloc(tmp_len);
+ if (!(db_hdl =3D malloc(tmp_len))) {
+ D_ERR(__FUNCTION__ ": malloc failed to allocate %d bytes!\n", =
tmp_len);
+ send_error_rsp(sdp_con_id, trans_id, SDP_INSUFFICIENT_RESOURCE=
S);
+ return;
+ }
+
D_MEM("---> malloc%d %d bytes at 0x%08x\n",malloc_dbg++, tmp_len, (int=
) db_hdl);
=20=20=20=20=20
db_hdl->db.sdp_con_id =3D sdp_con_id;
@@ -708,12 +709,12 @@
=20=20=20=20=20
db_hdl->max_attr_byte_cnt =3D max_attr_cnt;
db_hdl->service_class_cnt =3D service_search_uuid_cnt;
- memcpy(db_hdl->service_class_list, service_search_uuid, service_search=
_uuid_cnt * 4);
+ memcpy(db_hdl->service_class_list, service_search_uuid, service_search=
_uuid_cnt * sizeof *service_search_uuid);
db_hdl->attr_cnt =3D attr_list_pos;
- memcpy(db_hdl->attr_list, attr_list, attr_list_pos * 4);
+ memcpy(db_hdl->attr_list, attr_list, attr_list_pos * sizeof *attr_list=
);
=20=20=20=20=20
/* Here we ask the database for the requested attributes */
- handle_query((database_query_struct*) db_hdl);
+ handle_query(&db_hdl->db);
=20
D_MEM("<--- free%d 0x%08x\n",malloc_dbg--, (int) db_hdl);
free(db_hdl);
@@ -753,8 +754,8 @@
unsigned char sdp_data[7];
unsigned short pdu_len;
=20
- /* Since we not send any error information the pdu length is just the si=
ze
- of the error code length, whoch is two bytes */
+ /* Since we do not send any error information, the pdu length is just the
+ size of the error code length, which is two bytes */
=20
pdu_len =3D 2;
=20=20=20
@@ -799,7 +800,7 @@
return 16;
case 5:
*new_pos =3D 2;
- return *(data + 1);
+ return data[1];
case 6:
*new_pos =3D 3;
return CHAR2INT16(data[1], data[2]);
@@ -813,22 +814,20 @@
void
write2stack(int sdp_con_id, char *data, int len)
{
- data_struct *db_hdl;
- int send_len;
+ data_struct db_hdl;
+ struct iovec vec[2];
=20
D_XMIT("write2stack: writing %d bytes to sdp_con_id %d\n", len,sdp_con_i=
d);
=20=20=20
- send_len =3D sizeof(data_struct) + len;
- db_hdl =3D (data_struct*) malloc(send_len);
- D_MEM("---> malloc%d %d bytes at 0x%08x\n",malloc_dbg++, send_len, (int)=
db_hdl);
+ db_hdl.sdp_con_id =3D sdp_con_id;
+ db_hdl.len =3D len;
=20=20=20
- db_hdl->sdp_con_id =3D sdp_con_id;
- db_hdl->len =3D len;
- memcpy(db_hdl->data, data, len);
+ vec[0].iov_base =3D &db_hdl;
+ vec[0].iov_len =3D sizeof db_hdl;
+ vec[1].iov_base =3D data;
+ vec[1].iov_len =3D len;
=20=20=20
- write(stack_if_fd, (char*) db_hdl, send_len);
- D_MEM("<--- free%d 0x%08x\n",malloc_dbg--, (int) db_hdl);
- free(db_hdl);
+ writev(stack_if_fd, vec, 2);
}
=20
#ifndef BTD_USERSTACK
--- sdp_parser.h 2001/03/23 16:06:18 1.4
+++ sdp_parser.h 2001/03/23 16:19:36 1.5
@@ -82,7 +82,6 @@
typedef struct data_struct {
unsigned int sdp_con_id;
int len;
- unsigned char data[0];
} data_struct;
=20
/****************** EXPORTED FUNCTION DECLARATION SECTION ****************=
***/
|