2012-02-10 21:39:45

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 00/22] Using clang for compiling BlueZ

Hi,

After hearing so much about LLVM/Clang[1][2], I decided to give it a
try on a project that I care about and see how would it go. The first
step was cleaning all the warnings that it could find. This is the
result.

One thing that I really liked was that the warnings are useful, making
it very easy to make these changes. In my opinion, Clang is worth
trying for this feature alone.

As a bonus point, we can now use Clang static analyzer on our code
base, if someone wants to take a look at what it looks like, here's an
example[3]. The analyzer still doesn't catch everything that paid
alternatives do but it is improving.

--
[1] http://llvm.org/

[2] http://clang.llvm.org/

[3] http://littlechina.org/~vcgomes/bluez-static-analysis/2012-02-10-1/


Vinicius Costa Gomes (22):
lib: Add functions to deal with unaligned access without conversion
lib: Add functions to storing values in unaligned places
lib: Fix the unaligned memory "getters" not receiving a const pointer
tools: Fix using old-style initializers
sap: Fix clang compiler warnings related to unaligned memory access
lib: Fix using old-style initializers
sdp: Fix clang compiler warnings related to unaligned memory access
l2test: Fix clang compiler warning related to unaligned memory access
rctest: Fix clang compiler warnings related to unaligned memory
access
dun: Fix using old-style initializers
scotest: Fix clang compiler warnings related to unaligned memory
access
ipctest: Fix using the format specifier "as"
audio: Fix clang compiler warnings related to unaligned memory access
audio: Fix clang compiler warnings regarding implicit enum conversion
sap: Fix compiler warnings related to unaligned memory access
network: Fix clang compiler warnings related to unaligned memory
access
mgmtops: Fix doing a useless memset()
adaptername: Fix clang warning related to unaligned memory access
sdp: Fix clang compiler warnings related to unaligned memory access
hciconfig: Fix using old-style initializers
hciconfig: Fix compiler warnings related to unaligned memory access
hciemu: Fix clang compiler warnings related to unaligned memory
access

audio/avrcp.c | 8 ++--
audio/sink.c | 4 +-
audio/sink.h | 2 +-
audio/source.c | 4 +-
audio/source.h | 2 +-
compat/dun.c | 4 +-
lib/bluetooth.h | 124 ++++++++++++++++++++++++++++++++++++++++++++-----
lib/hci.c | 4 +-
lib/sdp.c | 66 +++++++++++++-------------
network/server.c | 8 ++--
plugins/adaptername.c | 2 +-
plugins/mgmtops.c | 4 --
sap/sap-u8500.c | 10 ++--
sap/server.c | 10 +---
src/sdpd-request.c | 42 ++++++++--------
src/sdpd-service.c | 21 ++++----
test/hciemu.c | 9 ++-
test/ipctest.c | 12 ++--
test/l2test.c | 8 ++--
test/rctest.c | 4 +-
test/scotest.c | 4 +-
tools/hciconfig.c | 6 +-
tools/hcitool.c | 2 +-
tools/rfcomm.c | 2 +-
24 files changed, 229 insertions(+), 133 deletions(-)

--
1.7.8.1



2012-02-14 12:43:58

by Johan Hedberg

[permalink] [raw]
Subject: Re: [RFC BlueZ 00/22] Using clang for compiling BlueZ

Hi Vinicius,

On Fri, Feb 10, 2012, Vinicius Costa Gomes wrote:
> After hearing so much about LLVM/Clang[1][2], I decided to give it a
> try on a project that I care about and see how would it go. The first
> step was cleaning all the warnings that it could find. This is the
> result.
>
> One thing that I really liked was that the warnings are useful, making
> it very easy to make these changes. In my opinion, Clang is worth
> trying for this feature alone.
>
> As a bonus point, we can now use Clang static analyzer on our code
> base, if someone wants to take a look at what it looks like, here's an
> example[3]. The analyzer still doesn't catch everything that paid
> alternatives do but it is improving.
>
> --
> [1] http://llvm.org/
>
> [2] http://clang.llvm.org/
>
> [3] http://littlechina.org/~vcgomes/bluez-static-analysis/2012-02-10-1/
>
>
> Vinicius Costa Gomes (22):
> lib: Add functions to deal with unaligned access without conversion
> lib: Add functions to storing values in unaligned places
> lib: Fix the unaligned memory "getters" not receiving a const pointer
> tools: Fix using old-style initializers
> sap: Fix clang compiler warnings related to unaligned memory access
> lib: Fix using old-style initializers
> sdp: Fix clang compiler warnings related to unaligned memory access
> l2test: Fix clang compiler warning related to unaligned memory access
> rctest: Fix clang compiler warnings related to unaligned memory
> access
> dun: Fix using old-style initializers
> scotest: Fix clang compiler warnings related to unaligned memory
> access
> ipctest: Fix using the format specifier "as"
> audio: Fix clang compiler warnings related to unaligned memory access
> audio: Fix clang compiler warnings regarding implicit enum conversion
> sap: Fix compiler warnings related to unaligned memory access
> network: Fix clang compiler warnings related to unaligned memory
> access
> mgmtops: Fix doing a useless memset()
> adaptername: Fix clang warning related to unaligned memory access
> sdp: Fix clang compiler warnings related to unaligned memory access
> hciconfig: Fix using old-style initializers
> hciconfig: Fix compiler warnings related to unaligned memory access
> hciemu: Fix clang compiler warnings related to unaligned memory
> access
>
> audio/avrcp.c | 8 ++--
> audio/sink.c | 4 +-
> audio/sink.h | 2 +-
> audio/source.c | 4 +-
> audio/source.h | 2 +-
> compat/dun.c | 4 +-
> lib/bluetooth.h | 124 ++++++++++++++++++++++++++++++++++++++++++++-----
> lib/hci.c | 4 +-
> lib/sdp.c | 66 +++++++++++++-------------
> network/server.c | 8 ++--
> plugins/adaptername.c | 2 +-
> plugins/mgmtops.c | 4 --
> sap/sap-u8500.c | 10 ++--
> sap/server.c | 10 +---
> src/sdpd-request.c | 42 ++++++++--------
> src/sdpd-service.c | 21 ++++----
> test/hciemu.c | 9 ++-
> test/ipctest.c | 12 ++--
> test/l2test.c | 8 ++--
> test/rctest.c | 4 +-
> test/scotest.c | 4 +-
> tools/hciconfig.c | 6 +-
> tools/hcitool.c | 2 +-
> tools/rfcomm.c | 2 +-
> 24 files changed, 229 insertions(+), 133 deletions(-)

I've applied the patches which fulfilled the following criteria:

- Made sense
- No objections from me or Marcel
- Applied cleanly
- No compiler warnings or errors introduced

This amounted to the following patches: 4, 6, 10, 14, 16, 17, 20 and 21.

Johan

2012-02-14 12:39:00

by Johan Hedberg

[permalink] [raw]
Subject: Re: [RFC BlueZ 18/22] adaptername: Fix clang warning related to unaligned memory access

Hi Vinicius,

On Fri, Feb 10, 2012, Vinicius Costa Gomes wrote:
> ---
> plugins/adaptername.c | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/plugins/adaptername.c b/plugins/adaptername.c
> index e154e92..a2f9c8f 100644
> --- a/plugins/adaptername.c
> +++ b/plugins/adaptername.c
> @@ -241,7 +241,7 @@ static gboolean handle_inotify_cb(GIOChannel *channel, GIOCondition cond,
>
> i = 0;
> while (i < len) {
> - struct inotify_event *pevent = (struct inotify_event *) &buf[i];
> + struct inotify_event *pevent = (void *) &buf[i];
>
> /* check that it's ours */
> if (pevent->len && pevent->name != NULL &&

I see how this might remove the warning but not how it removes potential
unaligned access of pevent->len and pevent->name.

Johan

2012-02-11 11:31:58

by Marcel Holtmann

[permalink] [raw]
Subject: Re: [RFC BlueZ 12/22] ipctest: Fix using the format specifier "as"

Hi Vinicius,

> This format specifier is GNU specific.
> ---
> test/ipctest.c | 12 ++++++------
> 1 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/test/ipctest.c b/test/ipctest.c
> index cbfd78d..012de0a 100644
> --- a/test/ipctest.c
> +++ b/test/ipctest.c
> @@ -987,10 +987,10 @@ static gboolean input_cb(GIOChannel *gin, GIOCondition condition, gpointer data)
> }
>
> IF_CMD(debug) {
> - char *what = NULL;
> + char what[15];
> int enable;
>
> - if (sscanf(line, "%*s %as %d", &what, &enable) != 1)
> + if (sscanf(line, "%*s %s %d", what, &enable) != 1)
> DBG("debug [stream_read|stream_write] [0|1]");

"Matches a sequence of non-white-space characters; the next
pointer must be a pointer to character array that is long enough
to hold the input sequence and the terminating null character
('\0'), which is added automatically."

At least use alloca. And it is still pointer of pointer.

Regards

Marcel



2012-02-11 11:27:57

by Marcel Holtmann

[permalink] [raw]
Subject: Re: [RFC BlueZ 02/22] lib: Add functions to storing values in unaligned places

Hi Vinicius,

> lib/bluetooth.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 60 insertions(+), 0 deletions(-)
>
> diff --git a/lib/bluetooth.h b/lib/bluetooth.h
> index e9ced19..ba241a1 100644
> --- a/lib/bluetooth.h
> +++ b/lib/bluetooth.h
> @@ -172,60 +172,120 @@ static inline uint64_t bt_get_le64(void *ptr)
> return bt_get_unaligned((uint64_t *) ptr);
> }
>
> +static inline void bt_put_le64(uint64_t val, void *ptr)
> +{
> + bt_put_unaligned(val, (uint64_t *) ptr);
> +}
> +

and how does this work on a big endian machine?

Regards

Marcel



2012-02-11 11:25:09

by Marcel Holtmann

[permalink] [raw]
Subject: Re: [RFC BlueZ 01/22] lib: Add functions to deal with unaligned access without conversion

Hi Vinicius,

> In various places we need to do access unaligned memory, but we don't
> want to do any type of conversions, i.e. the values are already in the
> host order.
> ---
> lib/bluetooth.h | 40 ++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 40 insertions(+), 0 deletions(-)
>
> diff --git a/lib/bluetooth.h b/lib/bluetooth.h
> index 1dee6df..e9ced19 100644
> --- a/lib/bluetooth.h
> +++ b/lib/bluetooth.h
> @@ -126,6 +126,46 @@ do { \
> __p->__v = (val); \
> } while(0)
>
> +static inline uint64_t bt_get_h64(void *ptr)
> +{
> + return bt_get_unaligned((uint64_t *) ptr);
> +}

this is a bad idea. Rather change the code to always clearly use the le
vs be versions. There is no need for a host copy. You only do this on
wire protocols and you know the endian.

Regards

Marcel



2012-02-10 21:40:07

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 22/22] hciemu: Fix clang compiler warnings related to unaligned memory access

---
test/hciemu.c | 9 ++++++---
1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/test/hciemu.c b/test/hciemu.c
index 7eb8814..ac44aa8 100644
--- a/test/hciemu.c
+++ b/test/hciemu.c
@@ -388,8 +388,11 @@ static void num_completed_pkts(struct vhci_conn *conn)
np = (void *) ptr; ptr += EVT_NUM_COMP_PKTS_SIZE;
np->num_hndl = 1;

- *((uint16_t *) ptr) = htobs(conn->handle); ptr += 2;
- *((uint16_t *) ptr) = htobs(vdev.acl_cnt); ptr += 2;
+ bt_put_le16(conn->handle, ptr);
+ ptr += 2;
+
+ bt_put_le16(vdev.acl_cnt, ptr);
+ ptr += 2;

write_snoop(vdev.dd, HCI_EVENT_PKT, 1, buf, ptr - buf);

@@ -1009,7 +1012,7 @@ static int getbdaddrbyname(char *str, bdaddr_t *ba)
bdaddr_t b;

memcpy(&b, &addr, 4);
- *(uint16_t *) (&b.b[4]) = htons(atoi(str));
+ bt_put_be16(atoi(str), &b.b[4]);
baswap(ba, &b);

return 0;
--
1.7.8.1


2012-02-10 21:40:06

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 21/22] hciconfig: Fix compiler warnings related to unaligned memory access

---
tools/hciconfig.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/tools/hciconfig.c b/tools/hciconfig.c
index 5d01bd9..f1458b9 100644
--- a/tools/hciconfig.c
+++ b/tools/hciconfig.c
@@ -1305,7 +1305,7 @@ static void cmd_inq_data(int ctl, int hdev, char *opt)
printf("\t%s service classes:",
type == 0x02 ? "Shortened" : "Complete");
for (i = 0; i < (len - 1) / 2; i++) {
- uint16_t val = btohs(bt_get_unaligned((uint16_t *) (ptr + (i * 2))));
+ uint16_t val = bt_get_le16((ptr + (i * 2)));
printf(" 0x%4.4x", val);
}
printf("\n");
--
1.7.8.1


2012-02-10 21:40:05

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 20/22] hciconfig: Fix using old-style initializers

---
tools/hciconfig.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/hciconfig.c b/tools/hciconfig.c
index a7249db..5d01bd9 100644
--- a/tools/hciconfig.c
+++ b/tools/hciconfig.c
@@ -503,7 +503,7 @@ static void cmd_lm(int ctl, int hdev, char *opt)

static void cmd_aclmtu(int ctl, int hdev, char *opt)
{
- struct hci_dev_req dr = { dev_id: hdev };
+ struct hci_dev_req dr = { .dev_id = hdev };
uint16_t mtu, mpkt;

if (!opt)
@@ -523,7 +523,7 @@ static void cmd_aclmtu(int ctl, int hdev, char *opt)

static void cmd_scomtu(int ctl, int hdev, char *opt)
{
- struct hci_dev_req dr = { dev_id: hdev };
+ struct hci_dev_req dr = { .dev_id = hdev };
uint16_t mtu, mpkt;

if (!opt)
--
1.7.8.1


2012-02-10 21:40:04

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 19/22] sdp: Fix clang compiler warnings related to unaligned memory access

---
src/sdpd-request.c | 42 +++++++++++++++++++++---------------------
src/sdpd-service.c | 21 ++++++++++-----------
2 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/src/sdpd-request.c b/src/sdpd-request.c
index ff00df7..dae6ec6 100644
--- a/src/sdpd-request.c
+++ b/src/sdpd-request.c
@@ -178,11 +178,11 @@ static int extract_des(uint8_t *buf, int len, sdp_list_t **svcReqSeq, uint8_t *p
struct attrid *aid;
aid = malloc(sizeof(struct attrid));
aid->dtd = dataType;
- bt_put_unaligned(ntohs(bt_get_unaligned((uint16_t *)p)), (uint16_t *)&aid->uint16);
+ bt_put_be16(bt_get_h16(p), &aid->uint16);
pElem = (char *) aid;
} else {
pElem = malloc(sizeof(uint16_t));
- bt_put_unaligned(ntohs(bt_get_unaligned((uint16_t *)p)), (uint16_t *)pElem);
+ bt_put_be16(bt_get_h16(p), pElem);
}
p += sizeof(uint16_t);
seqlen += sizeof(uint16_t);
@@ -201,11 +201,11 @@ static int extract_des(uint8_t *buf, int len, sdp_list_t **svcReqSeq, uint8_t *p
struct attrid *aid;
aid = malloc(sizeof(struct attrid));
aid->dtd = dataType;
- bt_put_unaligned(ntohl(bt_get_unaligned((uint32_t *)p)), (uint32_t *)&aid->uint32);
+ bt_put_be32(bt_get_h32(p), &aid->uint32);
pElem = (char *) aid;
} else {
pElem = malloc(sizeof(uint32_t));
- bt_put_unaligned(ntohl(bt_get_unaligned((uint32_t *)p)), (uint32_t *)pElem);
+ bt_put_be32(bt_get_h32(p), pElem);
}
p += sizeof(uint32_t);
seqlen += sizeof(uint32_t);
@@ -215,7 +215,7 @@ static int extract_des(uint8_t *buf, int len, sdp_list_t **svcReqSeq, uint8_t *p
case SDP_UUID32:
case SDP_UUID128:
pElem = malloc(sizeof(uuid_t));
- status = sdp_uuid_extract(p, bufsize, (uuid_t *) pElem, &localSeqLength);
+ status = sdp_uuid_extract(p, bufsize, (void *) pElem, &localSeqLength);
if (status < 0) {
free(pElem);
goto failed;
@@ -385,7 +385,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
goto done;
}

- expected = ntohs(bt_get_unaligned((uint16_t *)pdata));
+ expected = bt_get_be16(pdata);

SDPDBG("Expected count: %d", expected);
SDPDBG("Bytes scanned : %d", scanned);
@@ -409,14 +409,14 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
pdata = buf->data;

/* total service record count = 0 */
- pTotalRecordCount = (short *)pdata;
- bt_put_unaligned(0, (uint16_t *)pdata);
+ pTotalRecordCount = (void *) pdata;
+ bt_put_h16(0, pdata);
pdata += sizeof(uint16_t);
buf->data_size += sizeof(uint16_t);

/* current service record count = 0 */
- pCurrentRecordCount = (short *)pdata;
- bt_put_unaligned(0, (uint16_t *)pdata);
+ pCurrentRecordCount = (void *) pdata;
+ bt_put_h16(0, pdata);
pdata += sizeof(uint16_t);
buf->data_size += sizeof(uint16_t);

@@ -433,7 +433,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
if (sdp_match_uuid(pattern, rec->pattern) > 0 &&
sdp_check_access(rec->handle, &req->device)) {
rsp_count++;
- bt_put_unaligned(htonl(rec->handle), (uint32_t *)pdata);
+ bt_put_be32(rec->handle, pdata);
pdata += sizeof(uint32_t);
handleSize += sizeof(uint32_t);
}
@@ -442,8 +442,8 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
SDPDBG("Match count: %d", rsp_count);

buf->data_size += handleSize;
- bt_put_unaligned(htons(rsp_count), (uint16_t *)pTotalRecordCount);
- bt_put_unaligned(htons(rsp_count), (uint16_t *)pCurrentRecordCount);
+ bt_put_be16(rsp_count, pTotalRecordCount);
+ bt_put_be16(rsp_count, pCurrentRecordCount);

if (rsp_count > actual) {
/* cache the rsp and generate a continuation state */
@@ -472,7 +472,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
if (pCache) {
pCacheBuffer = pCache->data;
/* get the rsp_count from the cached buffer */
- rsp_count = ntohs(bt_get_unaligned((uint16_t *)pCacheBuffer));
+ rsp_count = bt_get_be16(pCacheBuffer);

/* get index of the last sdp_record_t sent */
lastIndex = cstate->cStateValue.lastIndexSent;
@@ -498,7 +498,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
if (cstate) {
handleSize = 0;
for (i = lastIndex; (i - lastIndex) < actual && i < rsp_count; i++) {
- bt_put_unaligned(bt_get_unaligned((uint32_t *)(pCacheBuffer + i * sizeof(uint32_t))), (uint32_t *)pdata);
+ bt_put_h32(bt_get_h32((pCacheBuffer + i * sizeof(uint32_t))), pdata);
pdata += sizeof(uint32_t);
handleSize += sizeof(uint32_t);
}
@@ -639,7 +639,7 @@ static int service_attr_req(sdp_req_t *req, sdp_buf_t *buf)
goto done;
}

- handle = ntohl(bt_get_unaligned((uint32_t *)pdata));
+ handle = bt_get_be32(pdata);

pdata += sizeof(uint32_t);
data_left -= sizeof(uint32_t);
@@ -649,7 +649,7 @@ static int service_attr_req(sdp_req_t *req, sdp_buf_t *buf)
goto done;
}

- max_rsp_size = ntohs(bt_get_unaligned((uint16_t *)pdata));
+ max_rsp_size = bt_get_be16(pdata);

pdata += sizeof(uint16_t);
data_left -= sizeof(uint16_t);
@@ -765,7 +765,7 @@ done:
return status;

/* set attribute list byte count */
- bt_put_unaligned(htons(buf->data_size - cstate_size), (uint16_t *)buf->data);
+ bt_put_be16(buf->data_size - cstate_size, buf->data);
buf->data_size += sizeof(uint16_t);
return 0;
}
@@ -806,7 +806,7 @@ static int service_search_attr_req(sdp_req_t *req, sdp_buf_t *buf)
goto done;
}

- max = ntohs(bt_get_unaligned((uint16_t *)pdata));
+ max = bt_get_be16(pdata);

pdata += sizeof(uint16_t);
data_left -= sizeof(uint16_t);
@@ -936,7 +936,7 @@ static int service_search_attr_req(sdp_req_t *req, sdp_buf_t *buf)

if (!status) {
/* set attribute list byte count */
- bt_put_unaligned(htons(buf->data_size - cstate_size), (uint16_t *)buf->data);
+ bt_put_be16(buf->data_size - cstate_size, buf->data);
buf->data_size += sizeof(uint16_t);
}

@@ -1020,7 +1020,7 @@ static void process_request(sdp_req_t *req)
send_rsp:
if (status) {
rsphdr->pdu_id = SDP_ERROR_RSP;
- bt_put_unaligned(htons(status), (uint16_t *)rsp.data);
+ bt_put_be16(status, rsp.data);
rsp.data_size = sizeof(uint16_t);
}

diff --git a/src/sdpd-service.c b/src/sdpd-service.c
index de11562..7977043 100644
--- a/src/sdpd-service.c
+++ b/src/sdpd-service.c
@@ -313,7 +313,7 @@ static sdp_record_t *extract_pdu_server(bdaddr_t *device, uint8_t *p,
return NULL;
}

- lookAheadAttrId = ntohs(bt_get_unaligned((uint16_t *) (p + sizeof(uint8_t))));
+ lookAheadAttrId = bt_get_be16(p + sizeof(uint8_t));

SDPDBG("Look ahead attr id : %d", lookAheadAttrId);

@@ -323,9 +323,8 @@ static sdp_record_t *extract_pdu_server(bdaddr_t *device, uint8_t *p,
SDPDBG("Unexpected end of packet");
return NULL;
}
- handle = ntohl(bt_get_unaligned((uint32_t *) (p +
- sizeof(uint8_t) + sizeof(uint16_t) +
- sizeof(uint8_t))));
+ handle = bt_get_be32(p + sizeof(uint8_t) + sizeof(uint16_t) +
+ sizeof(uint8_t));
SDPDBG("SvcRecHandle : 0x%x", handle);
rec = sdp_record_find(handle);
} else if (handleExpected != 0xffffffff)
@@ -359,7 +358,7 @@ static sdp_record_t *extract_pdu_server(bdaddr_t *device, uint8_t *p,
seqlen, localExtractedLength);
dtd = *(uint8_t *) p;

- attrId = ntohs(bt_get_unaligned((uint16_t *) (p + attrSize)));
+ attrId = bt_get_be16(p + attrSize);
attrSize += sizeof(uint16_t);

SDPDBG("DTD of attrId : %d Attr id : 0x%x", dtd, attrId);
@@ -450,13 +449,13 @@ success:
update_db_timestamp();

/* Build a rsp buffer */
- bt_put_unaligned(htonl(rec->handle), (uint32_t *) rsp->data);
+ bt_put_be32(rec->handle, rsp->data);
rsp->data_size = sizeof(uint32_t);

return 0;

invalid:
- bt_put_unaligned(htons(SDP_INVALID_SYNTAX), (uint16_t *) rsp->data);
+ bt_put_be16(SDP_INVALID_SYNTAX, rsp->data);
rsp->data_size = sizeof(uint16_t);

return -1;
@@ -471,7 +470,7 @@ int service_update_req(sdp_req_t *req, sdp_buf_t *rsp)
int status = 0, scanned = 0;
uint8_t *p = req->buf + sizeof(sdp_pdu_hdr_t);
int bufsize = req->len - sizeof(sdp_pdu_hdr_t);
- uint32_t handle = ntohl(bt_get_unaligned((uint32_t *) p));
+ uint32_t handle = bt_get_be32(p);

SDPDBG("Svc Rec Handle: 0x%x", handle);

@@ -499,7 +498,7 @@ int service_update_req(sdp_req_t *req, sdp_buf_t *rsp)

done:
p = rsp->data;
- bt_put_unaligned(htons(status), (uint16_t *) p);
+ bt_put_be16(status, p);
rsp->data_size = sizeof(uint16_t);
return status;
}
@@ -510,7 +509,7 @@ done:
int service_remove_req(sdp_req_t *req, sdp_buf_t *rsp)
{
uint8_t *p = req->buf + sizeof(sdp_pdu_hdr_t);
- uint32_t handle = ntohl(bt_get_unaligned((uint32_t *) p));
+ uint32_t handle = bt_get_be32(p);
sdp_record_t *rec;
int status = 0;

@@ -529,7 +528,7 @@ int service_remove_req(sdp_req_t *req, sdp_buf_t *rsp)
}

p = rsp->data;
- bt_put_unaligned(htons(status), (uint16_t *) p);
+ bt_put_be16(status, p);
rsp->data_size = sizeof(uint16_t);

return status;
--
1.7.8.1


2012-02-10 21:40:03

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 18/22] adaptername: Fix clang warning related to unaligned memory access

---
plugins/adaptername.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/plugins/adaptername.c b/plugins/adaptername.c
index e154e92..a2f9c8f 100644
--- a/plugins/adaptername.c
+++ b/plugins/adaptername.c
@@ -241,7 +241,7 @@ static gboolean handle_inotify_cb(GIOChannel *channel, GIOCondition cond,

i = 0;
while (i < len) {
- struct inotify_event *pevent = (struct inotify_event *) &buf[i];
+ struct inotify_event *pevent = (void *) &buf[i];

/* check that it's ours */
if (pevent->len && pevent->name != NULL &&
--
1.7.8.1


2012-02-10 21:40:02

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 17/22] mgmtops: Fix doing a useless memset()

The buffers were already allocated using g_try_malloc0().
---
plugins/mgmtops.c | 4 ----
1 files changed, 0 insertions(+), 4 deletions(-)

diff --git a/plugins/mgmtops.c b/plugins/mgmtops.c
index 7e47386..d354bc6 100644
--- a/plugins/mgmtops.c
+++ b/plugins/mgmtops.c
@@ -1958,8 +1958,6 @@ static int mgmt_load_link_keys(int index, GSList *keys, gboolean debug_keys)
if (buf == NULL)
return -ENOMEM;

- memset(buf, 0, sizeof(buf));
-
hdr = (void *) buf;
hdr->opcode = htobs(MGMT_OP_LOAD_LINK_KEYS);
hdr->len = htobs(cp_size);
@@ -2169,8 +2167,6 @@ static int mgmtops_load_ltks(int index, GSList *keys)
if (buf == NULL)
return -ENOMEM;

- memset(buf, 0, sizeof(buf));
-
hdr = (void *) buf;
hdr->opcode = htobs(MGMT_OP_LOAD_LONG_TERM_KEYS);
hdr->len = htobs(cp_size);
--
1.7.8.1


2012-02-10 21:40:01

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 16/22] network: Fix clang compiler warnings related to unaligned memory access

---
network/server.c | 8 ++++----
1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/network/server.c b/network/server.c
index 6e0ce09..67d81e3 100644
--- a/network/server.c
+++ b/network/server.c
@@ -322,13 +322,13 @@ static uint16_t bnep_setup_decode(struct bnep_setup_conn_req *req,

switch (req->uuid_size) {
case 2: /* UUID16 */
- *dst_role = ntohs(bt_get_unaligned((uint16_t *) dest));
- *src_role = ntohs(bt_get_unaligned((uint16_t *) source));
+ *dst_role = bt_get_be16(dest);
+ *src_role = bt_get_be16(source);
break;
case 4: /* UUID32 */
case 16: /* UUID128 */
- *dst_role = ntohl(bt_get_unaligned((uint32_t *) dest));
- *src_role = ntohl(bt_get_unaligned((uint32_t *) source));
+ *dst_role = bt_get_be32(dest);
+ *src_role = bt_get_be32(source);
break;
default:
return BNEP_CONN_INVALID_SVC;
--
1.7.8.1


2012-02-10 21:40:00

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 15/22] sap: Fix compiler warnings related to unaligned memory access

---
sap/server.c | 10 +++-------
1 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/sap/server.c b/sap/server.c
index b8aa8a5..5497045 100644
--- a/sap/server.c
+++ b/sap/server.c
@@ -310,7 +310,7 @@ static int disconnect_ind(void *sap_device, uint8_t disc_type)
static void connect_req(struct sap_connection *conn,
struct sap_parameter *param)
{
- uint16_t maxmsgsize, *val;
+ uint16_t maxmsgsize;

DBG("conn %p state %d", conn, conn->state);

@@ -322,8 +322,7 @@ static void connect_req(struct sap_connection *conn,

stop_guard_timer(conn);

- val = (uint16_t *) &param->val;
- maxmsgsize = ntohs(*val);
+ maxmsgsize = bt_get_be16(&param->val);

DBG("Connect MaxMsgSize: 0x%04x", maxmsgsize);

@@ -645,14 +644,11 @@ int sap_connect_rsp(void *sap_device, uint8_t status, uint16_t maxmsgsize)

/* Add MaxMsgSize */
if (maxmsgsize) {
- uint16_t *len;
-
msg->nparam++;
param = (struct sap_parameter *) &buf[size];
param->id = SAP_PARAM_ID_MAX_MSG_SIZE;
param->len = htons(SAP_PARAM_ID_MAX_MSG_SIZE_LEN);
- len = (uint16_t *) &param->val;
- *len = htons(maxmsgsize);
+ bt_put_be16(maxmsgsize, &param->val);
size += PARAMETER_SIZE(SAP_PARAM_ID_MAX_MSG_SIZE_LEN);
}

--
1.7.8.1


2012-02-10 21:39:59

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 14/22] audio: Fix clang compiler warnings regarding implicit enum conversion

It was expected that source_get_state() returned a source_state_t, and
that sink_get_state() returned a sink_state_t.
---
audio/sink.c | 4 ++--
audio/sink.h | 2 +-
audio/source.c | 4 ++--
audio/source.h | 2 +-
4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/audio/sink.c b/audio/sink.c
index 2d5db18..52f70a9 100644
--- a/audio/sink.c
+++ b/audio/sink.c
@@ -649,11 +649,11 @@ gboolean sink_is_active(struct audio_device *dev)
return FALSE;
}

-avdtp_state_t sink_get_state(struct audio_device *dev)
+sink_state_t sink_get_state(struct audio_device *dev)
{
struct sink *sink = dev->sink;

- return sink->stream_state;
+ return sink->state;
}

gboolean sink_new_stream(struct audio_device *dev, struct avdtp *session,
diff --git a/audio/sink.h b/audio/sink.h
index 7b1902b..4fea5ba 100644
--- a/audio/sink.h
+++ b/audio/sink.h
@@ -42,7 +42,7 @@ gboolean sink_remove_state_cb(unsigned int id);
struct sink *sink_init(struct audio_device *dev);
void sink_unregister(struct audio_device *dev);
gboolean sink_is_active(struct audio_device *dev);
-avdtp_state_t sink_get_state(struct audio_device *dev);
+sink_state_t sink_get_state(struct audio_device *dev);
gboolean sink_new_stream(struct audio_device *dev, struct avdtp *session,
struct avdtp_stream *stream);
gboolean sink_setup_stream(struct sink *sink, struct avdtp *session);
diff --git a/audio/source.c b/audio/source.c
index 6d266f2..4c6e2d0 100644
--- a/audio/source.c
+++ b/audio/source.c
@@ -564,11 +564,11 @@ gboolean source_is_active(struct audio_device *dev)
return FALSE;
}

-avdtp_state_t source_get_state(struct audio_device *dev)
+source_state_t source_get_state(struct audio_device *dev)
{
struct source *source = dev->source;

- return source->stream_state;
+ return source->state;
}

gboolean source_new_stream(struct audio_device *dev, struct avdtp *session,
diff --git a/audio/source.h b/audio/source.h
index 7837284..695bded 100644
--- a/audio/source.h
+++ b/audio/source.h
@@ -43,7 +43,7 @@ gboolean source_remove_state_cb(unsigned int id);
struct source *source_init(struct audio_device *dev);
void source_unregister(struct audio_device *dev);
gboolean source_is_active(struct audio_device *dev);
-avdtp_state_t source_get_state(struct audio_device *dev);
+source_state_t source_get_state(struct audio_device *dev);
gboolean source_new_stream(struct audio_device *dev, struct avdtp *session,
struct avdtp_stream *stream);
gboolean source_setup_stream(struct source *source, struct avdtp *session);
--
1.7.8.1


2012-02-10 21:39:58

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 13/22] audio: Fix clang compiler warnings related to unaligned memory access

---
audio/avrcp.c | 8 ++++----
1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/audio/avrcp.c b/audio/avrcp.c
index c9ec314..a07ffeb 100644
--- a/audio/avrcp.c
+++ b/audio/avrcp.c
@@ -638,13 +638,13 @@ static uint8_t avrcp_handle_get_element_attributes(struct avrcp_player *player,
uint8_t transaction)
{
uint16_t len = ntohs(pdu->params_len);
- uint64_t *identifier = (uint64_t *) &pdu->params[0];
+ uint64_t identifier = bt_get_h64(pdu->params);
uint16_t pos;
uint8_t nattr;
GList *attr_ids;
uint16_t offset;

- if (len < 9 || *identifier != 0)
+ if (len < 9 || identifier != 0)
goto err;

nattr = pdu->params[8];
@@ -661,10 +661,10 @@ static uint8_t avrcp_handle_get_element_attributes(struct avrcp_player *player,
len = g_list_length(attr_ids);
} else {
unsigned int i;
- uint32_t *attr = (uint32_t *) &pdu->params[9];
+ uint32_t *attr = (void *) &pdu->params[9];

for (i = 0, len = 0, attr_ids = NULL; i < nattr; i++, attr++) {
- uint32_t id = ntohl(bt_get_unaligned(attr));
+ uint32_t id = bt_get_be32(attr);

/* Don't add invalid attributes */
if (id == AVRCP_MEDIA_ATTRIBUTE_ILLEGAL ||
--
1.7.8.1


2012-02-10 21:39:57

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 12/22] ipctest: Fix using the format specifier "as"

This format specifier is GNU specific.
---
test/ipctest.c | 12 ++++++------
1 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/test/ipctest.c b/test/ipctest.c
index cbfd78d..012de0a 100644
--- a/test/ipctest.c
+++ b/test/ipctest.c
@@ -987,10 +987,10 @@ static gboolean input_cb(GIOChannel *gin, GIOCondition condition, gpointer data)
}

IF_CMD(debug) {
- char *what = NULL;
+ char what[15];
int enable;

- if (sscanf(line, "%*s %as %d", &what, &enable) != 1)
+ if (sscanf(line, "%*s %s %d", what, &enable) != 1)
DBG("debug [stream_read|stream_write] [0|1]");
if (strncmp(what, "stream_read", 12) == 0) {
u->debug_stream_read = enable;
@@ -1030,7 +1030,8 @@ static gboolean input_cb(GIOChannel *gin, GIOCondition condition, gpointer data)
IF_CMD(bdaddr) {
char *address;

- if (sscanf(line, "%*s %as", &address) != 1)
+ address = calloc(18, sizeof(char));
+ if (sscanf(line, "%*s %s", address) != 1)
DBG("set with bdaddr BDADDR");

free(u->address);
@@ -1040,9 +1041,9 @@ static gboolean input_cb(GIOChannel *gin, GIOCondition condition, gpointer data)
}

IF_CMD(profile) {
- char *profile = NULL;
+ char profile[6];

- if (sscanf(line, "%*s %as", &profile) != 1)
+ if (sscanf(line, "%*s %s", profile) != 1)
DBG("set with profile [hsp|a2dp]");
if (strncmp(profile, "hsp", 4) == 0) {
u->transport = BT_CAPABILITIES_TRANSPORT_SCO;
@@ -1052,7 +1053,6 @@ static gboolean input_cb(GIOChannel *gin, GIOCondition condition, gpointer data)
DBG("set with profile [hsp|a2dp]");
}

- free(profile);
DBG("profile %s", u->transport == BT_CAPABILITIES_TRANSPORT_SCO ?
"hsp" : "a2dp");
}
--
1.7.8.1


2012-02-10 21:39:56

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 11/22] scotest: Fix clang compiler warnings related to unaligned memory access

---
test/scotest.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/test/scotest.c b/test/scotest.c
index 17bd8a6..fc87f5e 100644
--- a/test/scotest.c
+++ b/test/scotest.c
@@ -269,8 +269,8 @@ static void send_mode(char *svr)

seq = 0;
while (1) {
- *(uint32_t *) buf = htobl(seq);
- *(uint16_t *) (buf + 4) = htobs(data_size);
+ bt_put_le32(seq, buf);
+ bt_put_le16(data_size, buf + 4);
seq++;

if (send(sk, buf, so.mtu, 0) <= 0) {
--
1.7.8.1


2012-02-10 21:39:55

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 10/22] dun: Fix using old-style initializers

---
compat/dun.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/compat/dun.c b/compat/dun.c
index de98830..3f3a0d4 100644
--- a/compat/dun.c
+++ b/compat/dun.c
@@ -191,8 +191,8 @@ static int dun_create_tty(int sk, char *tty, int size)
int id, try = 30;

struct rfcomm_dev_req req = {
- flags: (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP),
- dev_id: -1
+ .flags = (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP),
+ .dev_id = -1
};

alen = sizeof(sa);
--
1.7.8.1


2012-02-10 21:39:54

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 09/22] rctest: Fix clang compiler warnings related to unaligned memory access

---
test/rctest.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/test/rctest.c b/test/rctest.c
index 4d7c90a..ef86291 100644
--- a/test/rctest.c
+++ b/test/rctest.c
@@ -536,8 +536,8 @@ static void do_send(int sk)

seq = 0;
while ((num_frames == -1) || (num_frames-- > 0)) {
- *(uint32_t *) buf = htobl(seq);
- *(uint16_t *) (buf + 4) = htobs(data_size);
+ bt_put_le32(seq, buf);
+ bt_put_le16(data_size, buf + 4);
seq++;

if (send(sk, buf, data_size, 0) <= 0) {
--
1.7.8.1


2012-02-10 21:39:53

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 08/22] l2test: Fix clang compiler warning related to unaligned memory access

---
test/l2test.c | 8 ++++----
1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/test/l2test.c b/test/l2test.c
index c5bc3d3..9792a40 100644
--- a/test/l2test.c
+++ b/test/l2test.c
@@ -784,7 +784,7 @@ static void recv_mode(int sk)
}

/* Check sequence */
- sq = btohl(*(uint32_t *) buf);
+ sq = bt_get_le32(buf);
if (seq != sq) {
syslog(LOG_INFO, "seq missmatch: %d -> %d", seq, sq);
seq = sq;
@@ -792,7 +792,7 @@ static void recv_mode(int sk)
seq++;

/* Check length */
- l = btohs(*(uint16_t *) (buf + 4));
+ l = bt_get_le16(buf + 4);
if (len != l) {
syslog(LOG_INFO, "size missmatch: %d -> %d", len, l);
continue;
@@ -851,8 +851,8 @@ static void do_send(int sk)

seq = 0;
while ((num_frames == -1) || (num_frames-- > 0)) {
- *(uint32_t *) buf = htobl(seq);
- *(uint16_t *) (buf + 4) = htobs(data_size);
+ bt_put_le32(seq, buf);
+ bt_put_le16(data_size, buf + 4);
seq++;

sent = 0;
--
1.7.8.1


2012-02-10 21:39:52

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 07/22] sdp: Fix clang compiler warnings related to unaligned memory access

In these places, the memory accesses were breaking strict-aliasing,
and clang is more sensitive for these.
---
lib/sdp.c | 66 ++++++++++++++++++++++++++++++------------------------------
1 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/lib/sdp.c b/lib/sdp.c
index a48ee14..11668bf 100644
--- a/lib/sdp.c
+++ b/lib/sdp.c
@@ -624,13 +624,13 @@ void sdp_set_seq_len(uint8_t *ptr, uint32_t length)
case SDP_ALT16:
case SDP_TEXT_STR16:
case SDP_URL_STR16:
- bt_put_unaligned(htons(length), (uint16_t *) ptr);
+ bt_put_be16(length, ptr);
break;
case SDP_SEQ32:
case SDP_ALT32:
case SDP_TEXT_STR32:
case SDP_URL_STR32:
- bt_put_unaligned(htonl(length), (uint32_t *) ptr);
+ bt_put_be32(length, ptr);
break;
}
}
@@ -687,7 +687,7 @@ void sdp_set_attrid(sdp_buf_t *buf, uint16_t attr)
/* data type for attr */
*p++ = SDP_UINT16;
buf->data_size = sizeof(uint8_t);
- bt_put_unaligned(htons(attr), (uint16_t *) p);
+ bt_put_be16(attr, p);
buf->data_size += sizeof(uint16_t);
}

@@ -1018,14 +1018,14 @@ int sdp_uuid_extract(const uint8_t *p, int bufsize, uuid_t *uuid, int *scanned)
SDPERR("Not enough room for 16-bit UUID");
return -1;
}
- sdp_uuid16_create(uuid, ntohs(bt_get_unaligned((uint16_t *) p)));
+ sdp_uuid16_create(uuid, bt_get_be16(p));
*scanned += sizeof(uint16_t);
} else if (type == SDP_UUID32) {
if (bufsize < (int) sizeof(uint32_t)) {
SDPERR("Not enough room for 32-bit UUID");
return -1;
}
- sdp_uuid32_create(uuid, ntohl(bt_get_unaligned((uint32_t *) p)));
+ sdp_uuid32_create(uuid, bt_get_be32(p));
*scanned += sizeof(uint32_t);
} else {
if (bufsize < (int) sizeof(uint128_t)) {
@@ -1253,7 +1253,7 @@ int sdp_extract_seqtype(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *siz
SDPERR("Unexpected end of packet");
return 0;
}
- *size = ntohs(bt_get_unaligned((uint16_t *) buf));
+ *size = bt_get_be16(buf);
scanned += sizeof(uint16_t);
break;
case SDP_SEQ32:
@@ -1262,7 +1262,7 @@ int sdp_extract_seqtype(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *siz
SDPERR("Unexpected end of packet");
return 0;
}
- *size = ntohl(bt_get_unaligned((uint32_t *) buf));
+ *size = bt_get_be32(buf);
scanned += sizeof(uint32_t);
break;
default:
@@ -1429,7 +1429,7 @@ sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int bufsize, int *scanned)
}

dtd = *(uint8_t *) p;
- attr = ntohs(bt_get_unaligned((uint16_t *) (p + n)));
+ attr = bt_get_be16(p + n);
n += sizeof(uint16_t);

SDPDBG("DTD of attrId : %d Attr id : 0x%x \n", dtd, attr);
@@ -2785,10 +2785,10 @@ void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len)
*(uint8_t *) p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t);
break;
case SDP_SEQ16:
- bt_put_unaligned(htons(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t)), (uint16_t *) p);
+ bt_put_be16(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t), p);
break;
case SDP_SEQ32:
- bt_put_unaligned(htonl(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t)), (uint32_t *) p);
+ bt_put_be32(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t), p);
break;
}
}
@@ -2885,7 +2885,7 @@ int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device,
goto end;
}
if (handle)
- *handle = ntohl(bt_get_unaligned((uint32_t *) p));
+ *handle = bt_get_be32(p);
}

end:
@@ -2968,7 +2968,7 @@ int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device

p = reqbuf + sizeof(sdp_pdu_hdr_t);
reqsize = sizeof(sdp_pdu_hdr_t);
- bt_put_unaligned(htonl(handle), (uint32_t *) p);
+ bt_put_be32(handle, p);
reqsize += sizeof(uint32_t);

reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
@@ -2985,7 +2985,7 @@ int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device

rsphdr = (sdp_pdu_hdr_t *) rspbuf;
p = rspbuf + sizeof(sdp_pdu_hdr_t);
- status = bt_get_unaligned((uint16_t *) p);
+ status = bt_get_h16(p);

if (rsphdr->pdu_id == SDP_ERROR_RSP) {
/* For this case the status always is invalid record handle */
@@ -3061,7 +3061,7 @@ int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp
p = reqbuf + sizeof(sdp_pdu_hdr_t);
reqsize = sizeof(sdp_pdu_hdr_t);

- bt_put_unaligned(htonl(handle), (uint32_t *) p);
+ bt_put_be32(handle, p);
reqsize += sizeof(uint32_t);
p += sizeof(uint32_t);

@@ -3090,7 +3090,7 @@ int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp

rsphdr = (sdp_pdu_hdr_t *) rspbuf;
p = rspbuf + sizeof(sdp_pdu_hdr_t);
- status = bt_get_unaligned((uint16_t *) p);
+ status = bt_get_h16(p);

if (rsphdr->pdu_id == SDP_ERROR_RSP) {
/* The status can be invalid sintax or invalid record handle */
@@ -3177,7 +3177,7 @@ static void extract_record_handle_seq(uint8_t *pdu, int bufsize, sdp_list_t **se
pSvcRec = malloc(sizeof(uint32_t));
if (!pSvcRec)
break;
- *pSvcRec = ntohl(bt_get_unaligned((uint32_t *) pdata));
+ *pSvcRec = bt_get_be32(pdata);
pSeq = sdp_list_append(pSeq, pSvcRec);
pdata += sizeof(uint32_t);
*scanned += sizeof(uint32_t);
@@ -3348,7 +3348,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search,
pdata += seqlen;

/* specify the maximum svc rec count that client expects */
- bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata);
+ bt_put_be16(max_rec_num, pdata);
reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);

@@ -3401,7 +3401,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search,
pdata += sizeof(uint16_t);
scanned += sizeof(uint16_t);
pdata_len -= sizeof(uint16_t);
- rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ rec_count = bt_get_be16(pdata);
pdata += sizeof(uint16_t);
scanned += sizeof(uint16_t);
pdata_len -= sizeof(uint16_t);
@@ -3511,12 +3511,12 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle,
reqsize = sizeof(sdp_pdu_hdr_t);

/* add the service record handle */
- bt_put_unaligned(htonl(handle), (uint32_t *) pdata);
+ bt_put_be32(handle, pdata);
reqsize += sizeof(uint32_t);
pdata += sizeof(uint32_t);

/* specify the response limit */
- bt_put_unaligned(htons(65535), (uint16_t *) pdata);
+ bt_put_be16(65535, pdata);
reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);

@@ -3568,7 +3568,7 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle,
goto end;
}

- rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ rsp_count = bt_get_be16(pdata);
attr_list_len += rsp_count;
pdata += sizeof(uint16_t);
pdata_len -= sizeof(uint16_t);
@@ -3770,7 +3770,7 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u
t->reqsize += seqlen;
pdata += seqlen;

- bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata);
+ bt_put_be16(max_rec_num, pdata);
t->reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);

@@ -3863,12 +3863,12 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_
t->reqsize = sizeof(sdp_pdu_hdr_t);

/* add the service record handle */
- bt_put_unaligned(htonl(handle), (uint32_t *) pdata);
+ bt_put_be32(handle, pdata);
t->reqsize += sizeof(uint32_t);
pdata += sizeof(uint32_t);

/* specify the response limit */
- bt_put_unaligned(htons(65535), (uint16_t *) pdata);
+ bt_put_be16(65535, pdata);
t->reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);

@@ -3983,7 +3983,7 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear
t->reqsize += seqlen;
pdata += seqlen;

- bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata);
+ bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
t->reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);

@@ -4119,9 +4119,9 @@ int sdp_process(sdp_session_t *session)
* CSRC: Current Service Record Count (2 bytes)
*/
ssr_pdata = pdata;
- tsrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata));
+ tsrc = bt_get_be16(ssr_pdata);
ssr_pdata += sizeof(uint16_t);
- csrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata));
+ csrc = bt_get_be16(ssr_pdata);

/* csrc should never be larger than tsrc */
if (csrc > tsrc) {
@@ -4141,14 +4141,14 @@ int sdp_process(sdp_session_t *session)
rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
} else {
/* point to the first csrc */
- uint16_t *pcsrc = (uint16_t *) (t->rsp_concat_buf.data + 2);
+ uint16_t pcsrc = bt_get_h16(t->rsp_concat_buf.data + 2);

/* FIXME: update the interface later. csrc doesn't need be passed to clients */

pdata += sizeof(uint16_t); /* point to csrc */

/* the first csrc contains the sum of partial csrc responses */
- *pcsrc += bt_get_unaligned((uint16_t *) pdata);
+ pcsrc += bt_get_h16(pdata);

pdata += sizeof(uint16_t); /* point to the first handle */
rsp_count = csrc * 4;
@@ -4157,7 +4157,7 @@ int sdp_process(sdp_session_t *session)
break;
case SDP_SVC_ATTR_RSP:
case SDP_SVC_SEARCH_ATTR_RSP:
- rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ rsp_count = bt_get_be16(pdata);
SDPDBG("Attrlist byte count : %d\n", rsp_count);

/*
@@ -4170,7 +4170,7 @@ int sdp_process(sdp_session_t *session)
status = 0x0000;
break;
case SDP_ERROR_RSP:
- status = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ status = bt_get_be16(pdata);
size = ntohs(rsphdr->plen);

goto end;
@@ -4331,7 +4331,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search
reqsize += seqlen;
pdata += seqlen;

- bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata);
+ bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
reqsize += sizeof(uint16_t);
pdata += sizeof(uint16_t);

@@ -4389,7 +4389,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search
goto end;
}

- rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ rsp_count = bt_get_be16(pdata);
attr_list_len += rsp_count;
pdata += sizeof(uint16_t); /* pdata points to attribute list */
pdata_len -= sizeof(uint16_t);
--
1.7.8.1


2012-02-10 21:39:51

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 06/22] lib: Fix using old-style initializers

---
lib/hci.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/hci.c b/lib/hci.c
index b122313..269c021 100644
--- a/lib/hci.c
+++ b/lib/hci.c
@@ -860,7 +860,7 @@ done:

static int __other_bdaddr(int dd, int dev_id, long arg)
{
- struct hci_dev_info di = { dev_id: dev_id };
+ struct hci_dev_info di = { .dev_id = dev_id };

if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
return 0;
@@ -873,7 +873,7 @@ static int __other_bdaddr(int dd, int dev_id, long arg)

static int __same_bdaddr(int dd, int dev_id, long arg)
{
- struct hci_dev_info di = { dev_id: dev_id };
+ struct hci_dev_info di = { .dev_id = dev_id };

if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
return 0;
--
1.7.8.1


2012-02-10 21:39:50

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 05/22] sap: Fix clang compiler warnings related to unaligned memory access

---
sap/sap-u8500.c | 10 ++++++----
1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/sap/sap-u8500.c b/sap/sap-u8500.c
index ef7d95c..9949504 100644
--- a/sap/sap-u8500.c
+++ b/sap/sap-u8500.c
@@ -32,6 +32,8 @@
#include <sys/socket.h>
#include <sys/un.h>

+#include <bluetooth/bluetooth.h>
+
#include "log.h"
#include "sap.h"

@@ -319,16 +321,16 @@ static void recv_status(uint32_t status)

static void recv_card_status(uint32_t status, uint8_t *param)
{
- uint32_t *card_status;
+ uint32_t card_status;
uint8_t result;
uint8_t iccrs;

if (status != STE_STATUS_OK)
return;

- card_status = (uint32_t *)param;
+ card_status = bt_get_h32(param);

- if (get_sap_reader_status(*card_status, &iccrs) < 0)
+ if (get_sap_reader_status(card_status, &iccrs) < 0)
result = SAP_RESULT_ERROR_NO_REASON;
else
result = get_sap_result(STE_GET_STATUS_MSG, status);
@@ -430,7 +432,7 @@ static void recv_response(struct ste_message *msg)
}

param = msg->payload;
- status = *(uint32_t *)param;
+ status = bt_get_h32(param);
param += sizeof(status);

DBG_VERBOSE("status 0x%x", status);
--
1.7.8.1


2012-02-10 21:39:49

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 04/22] tools: Fix using old-style initializers

---
tools/hcitool.c | 2 +-
tools/rfcomm.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/hcitool.c b/tools/hcitool.c
index 5189d8d..66e5c20 100644
--- a/tools/hcitool.c
+++ b/tools/hcitool.c
@@ -74,7 +74,7 @@ static void usage(void);

static int dev_info(int s, int dev_id, long arg)
{
- struct hci_dev_info di = { dev_id: dev_id };
+ struct hci_dev_info di = { .dev_id = dev_id };
char addr[18];

if (ioctl(s, HCIGETDEVINFO, (void *) &di))
diff --git a/tools/rfcomm.c b/tools/rfcomm.c
index 6800445..e73b0ba 100644
--- a/tools/rfcomm.c
+++ b/tools/rfcomm.c
@@ -673,7 +673,7 @@ static void cmd_show(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
if (strcmp(argv[0], "all") == 0)
print_dev_list(ctl, 0);
else {
- struct rfcomm_dev_info di = { id: atoi(argv[0]) };
+ struct rfcomm_dev_info di = { .id = atoi(argv[0]) };
if (ioctl(ctl, RFCOMMGETDEVINFO, &di) < 0) {
perror("Get info failed");
exit(1);
--
1.7.8.1


2012-02-10 21:39:48

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 03/22] lib: Fix the unaligned memory "getters" not receiving a const pointer

It should be clear that those functions doesn't change the value of the
pointer passed to them.
---
lib/bluetooth.h | 32 ++++++++++++++++----------------
1 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/lib/bluetooth.h b/lib/bluetooth.h
index ba241a1..c70bcf9 100644
--- a/lib/bluetooth.h
+++ b/lib/bluetooth.h
@@ -126,22 +126,22 @@ do { \
__p->__v = (val); \
} while(0)

-static inline uint64_t bt_get_h64(void *ptr)
+static inline uint64_t bt_get_h64(const void *ptr)
{
return bt_get_unaligned((uint64_t *) ptr);
}

-static inline uint32_t bt_get_h32(void *ptr)
+static inline uint32_t bt_get_h32(const void *ptr)
{
return bt_get_unaligned((uint32_t *) ptr);
}

-static inline uint8_t bt_get_h16(void *ptr)
+static inline uint16_t bt_get_h16(const void *ptr)
{
return bt_get_unaligned((uint16_t *) ptr);
}

-static inline uint8_t bt_get_h8(void *ptr)
+static inline uint8_t bt_get_h8(const void *ptr)
{
return bt_get_unaligned((uint8_t *) ptr);
}
@@ -167,7 +167,7 @@ static inline void bt_put_h8(uint8_t val, void *ptr)
}

#if __BYTE_ORDER == __LITTLE_ENDIAN
-static inline uint64_t bt_get_le64(void *ptr)
+static inline uint64_t bt_get_le64(const void *ptr)
{
return bt_get_unaligned((uint64_t *) ptr);
}
@@ -177,7 +177,7 @@ static inline void bt_put_le64(uint64_t val, void *ptr)
bt_put_unaligned(val, (uint64_t *) ptr);
}

-static inline uint64_t bt_get_be64(void *ptr)
+static inline uint64_t bt_get_be64(const void *ptr)
{
return bswap_64(bt_get_unaligned((uint64_t *) ptr));
}
@@ -187,7 +187,7 @@ static inline void bt_put_be64(uint64_t val, void *ptr)
bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
}

-static inline uint32_t bt_get_le32(void *ptr)
+static inline uint32_t bt_get_le32(const void *ptr)
{
return bt_get_unaligned((uint32_t *) ptr);
}
@@ -197,7 +197,7 @@ static inline void bt_put_le32(uint32_t val, void *ptr)
bt_put_unaligned(val, (uint32_t *) ptr);
}

-static inline uint32_t bt_get_be32(void *ptr)
+static inline uint32_t bt_get_be32(const void *ptr)
{
return bswap_32(bt_get_unaligned((uint32_t *) ptr));
}
@@ -207,7 +207,7 @@ static inline void bt_put_be32(uint32_t val, void *ptr)
bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
}

-static inline uint16_t bt_get_le16(void *ptr)
+static inline uint16_t bt_get_le16(const void *ptr)
{
return bt_get_unaligned((uint16_t *) ptr);
}
@@ -217,7 +217,7 @@ static inline void bt_put_le16(uint16_t val, void *ptr)
bt_put_unaligned(val, (uint16_t *) ptr);
}

-static inline uint16_t bt_get_be16(void *ptr)
+static inline uint16_t bt_get_be16(const void *ptr)
{
return bswap_16(bt_get_unaligned((uint16_t *) ptr));
}
@@ -227,7 +227,7 @@ static inline void bt_put_be16(uint16_t val, void *ptr)
bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
}
#elif __BYTE_ORDER == __BIG_ENDIAN
-static inline uint64_t bt_get_le64(void *ptr)
+static inline uint64_t bt_get_le64(const void *ptr)
{
return bswap_64(bt_get_unaligned((uint64_t *) ptr));
}
@@ -237,7 +237,7 @@ static inline void bt_put_le64(uint64_t val, void *ptr)
bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
}

-static inline uint64_t bt_get_be64(void *ptr)
+static inline uint64_t bt_get_be64(const void *ptr)
{
return bt_get_unaligned((uint64_t *) ptr);
}
@@ -247,7 +247,7 @@ static inline void bt_put_be64(uint64_t val, void *ptr)
bt_put_unaligned(val, (uint64_t *) ptr);
}

-static inline uint32_t bt_get_le32(void *ptr)
+static inline uint32_t bt_get_le32(const void *ptr)
{
return bswap_32(bt_get_unaligned((uint32_t *) ptr));
}
@@ -257,7 +257,7 @@ static inline void bt_put_le32(uint32_t val, void *ptr)
bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
}

-static inline uint32_t bt_get_be32(void *ptr)
+static inline uint32_t bt_get_be32(const void *ptr)
{
return bt_get_unaligned((uint32_t *) ptr);
}
@@ -267,7 +267,7 @@ static inline void bt_put_be32(uint32_t val, void *ptr)
bt_put_unaligned(val, (uint32_t *) ptr);
}

-static inline uint16_t bt_get_le16(void *ptr)
+static inline uint16_t bt_get_le16(const void *ptr)
{
return bswap_16(bt_get_unaligned((uint16_t *) ptr));
}
@@ -277,7 +277,7 @@ static inline void bt_put_le16(uint16_t val, void *ptr)
bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
}

-static inline uint16_t bt_get_be16(void *ptr)
+static inline uint16_t bt_get_be16(const void *ptr)
{
return bt_get_unaligned((uint16_t *) ptr);
}
--
1.7.8.1


2012-02-10 21:39:47

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 02/22] lib: Add functions to storing values in unaligned places

---
lib/bluetooth.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/lib/bluetooth.h b/lib/bluetooth.h
index e9ced19..ba241a1 100644
--- a/lib/bluetooth.h
+++ b/lib/bluetooth.h
@@ -172,60 +172,120 @@ static inline uint64_t bt_get_le64(void *ptr)
return bt_get_unaligned((uint64_t *) ptr);
}

+static inline void bt_put_le64(uint64_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint64_t *) ptr);
+}
+
static inline uint64_t bt_get_be64(void *ptr)
{
return bswap_64(bt_get_unaligned((uint64_t *) ptr));
}

+static inline void bt_put_be64(uint64_t val, void *ptr)
+{
+ bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
+}
+
static inline uint32_t bt_get_le32(void *ptr)
{
return bt_get_unaligned((uint32_t *) ptr);
}

+static inline void bt_put_le32(uint32_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint32_t *) ptr);
+}
+
static inline uint32_t bt_get_be32(void *ptr)
{
return bswap_32(bt_get_unaligned((uint32_t *) ptr));
}

+static inline void bt_put_be32(uint32_t val, void *ptr)
+{
+ bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
+}
+
static inline uint16_t bt_get_le16(void *ptr)
{
return bt_get_unaligned((uint16_t *) ptr);
}

+static inline void bt_put_le16(uint16_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint16_t *) ptr);
+}
+
static inline uint16_t bt_get_be16(void *ptr)
{
return bswap_16(bt_get_unaligned((uint16_t *) ptr));
}
+
+static inline void bt_put_be16(uint16_t val, void *ptr)
+{
+ bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
+}
#elif __BYTE_ORDER == __BIG_ENDIAN
static inline uint64_t bt_get_le64(void *ptr)
{
return bswap_64(bt_get_unaligned((uint64_t *) ptr));
}

+static inline void bt_put_le64(uint64_t val, void *ptr)
+{
+ bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);
+}
+
static inline uint64_t bt_get_be64(void *ptr)
{
return bt_get_unaligned((uint64_t *) ptr);
}

+static inline void bt_put_be64(uint64_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint64_t *) ptr);
+}
+
static inline uint32_t bt_get_le32(void *ptr)
{
return bswap_32(bt_get_unaligned((uint32_t *) ptr));
}

+static inline void bt_put_le32(uint32_t val, void *ptr)
+{
+ bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);
+}
+
static inline uint32_t bt_get_be32(void *ptr)
{
return bt_get_unaligned((uint32_t *) ptr);
}

+static inline void bt_put_be32(uint32_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint32_t *) ptr);
+}
+
static inline uint16_t bt_get_le16(void *ptr)
{
return bswap_16(bt_get_unaligned((uint16_t *) ptr));
}

+static inline void bt_put_le16(uint16_t val, void *ptr)
+{
+ bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);
+}
+
static inline uint16_t bt_get_be16(void *ptr)
{
return bt_get_unaligned((uint16_t *) ptr);
}
+
+static inline void bt_put_be16(uint16_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint16_t *) ptr);
+}
#else
#error "Unknown byte order"
#endif
--
1.7.8.1


2012-02-10 21:39:46

by Vinicius Costa Gomes

[permalink] [raw]
Subject: [RFC BlueZ 01/22] lib: Add functions to deal with unaligned access without conversion

In various places we need to do access unaligned memory, but we don't
want to do any type of conversions, i.e. the values are already in the
host order.
---
lib/bluetooth.h | 40 ++++++++++++++++++++++++++++++++++++++++
1 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/lib/bluetooth.h b/lib/bluetooth.h
index 1dee6df..e9ced19 100644
--- a/lib/bluetooth.h
+++ b/lib/bluetooth.h
@@ -126,6 +126,46 @@ do { \
__p->__v = (val); \
} while(0)

+static inline uint64_t bt_get_h64(void *ptr)
+{
+ return bt_get_unaligned((uint64_t *) ptr);
+}
+
+static inline uint32_t bt_get_h32(void *ptr)
+{
+ return bt_get_unaligned((uint32_t *) ptr);
+}
+
+static inline uint8_t bt_get_h16(void *ptr)
+{
+ return bt_get_unaligned((uint16_t *) ptr);
+}
+
+static inline uint8_t bt_get_h8(void *ptr)
+{
+ return bt_get_unaligned((uint8_t *) ptr);
+}
+
+static inline void bt_put_h64(uint64_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint64_t *) ptr);
+}
+
+static inline void bt_put_h32(uint32_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint32_t *) ptr);
+}
+
+static inline void bt_put_h16(uint16_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint16_t *) ptr);
+}
+
+static inline void bt_put_h8(uint8_t val, void *ptr)
+{
+ bt_put_unaligned(val, (uint8_t *) ptr);
+}
+
#if __BYTE_ORDER == __LITTLE_ENDIAN
static inline uint64_t bt_get_le64(void *ptr)
{
--
1.7.8.1