Re: [PATCH v2 1/1] Bluetooth: make hci_test_bit's addr const

From: Marcel Holtmann
Date: Fri Feb 27 2015 - 12:32:38 EST


Hi Jiri,

>>> gcc5 warns about passing a const array to hci_test_bit which takes a
>>> non-const pointer:
>>> net/bluetooth/hci_sock.c: In function âhci_sock_sendmsgâ:
>>> net/bluetooth/hci_sock.c:955:8: warning: passing argument 2 of âhci_test_bitâ discards âconstâ qualifier from pointer target type [-Wdiscarded-array-qualifiers]
>>> &hci_sec_filter.ocf_mask[ogf])) &&
>>> ^
>>> net/bluetooth/hci_sock.c:49:19: note: expected âvoid *â but argument is of type âconst __u32 (*)[4] {aka const unsigned int (*)[4]}â
>>> static inline int hci_test_bit(int nr, void *addr)
>>> ^
>>>
>>> So make 'addr' 'const void *'.
>>>
>>> [v2] make the pointer in the cast const too.
>>>
>>> Signed-off-by: Jiri Slaby <jslaby@xxxxxxx>
>>> Cc: Marcel Holtmann <marcel@xxxxxxxxxxxx>
>>> Cc: Gustavo Padovan <gustavo@xxxxxxxxxxx>
>>> Cc: Johan Hedberg <johan.hedberg@xxxxxxxxx>
>>> ---
>>> net/bluetooth/hci_sock.c | 4 ++--
>>> 1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
>>> index 1d65c5be7c82..d1a7d67619d8 100644
>>> --- a/net/bluetooth/hci_sock.c
>>> +++ b/net/bluetooth/hci_sock.c
>>> @@ -46,9 +46,9 @@ struct hci_pinfo {
>>> unsigned short channel;
>>> };
>>>
>>> -static inline int hci_test_bit(int nr, void *addr)
>>> +static inline int hci_test_bit(int nr, const void *addr)
>>> {
>>> - return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
>>> + return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
>>> }
>>
>> we had a lengthy discussion about this before. I am reluctant to change any of this until we have a test tool that proofs this does not break userspace API compatibility.
>>
>> I don't see how just adding const would break anything, but I cautious since this code is pretty much a big mistake that we are carrying around for almost 14 years now.
>
> Yeah, I understand, but relying on the compiler is worse in this case, I
> think. Here, the function casts away const from a const array, which is
> undefined behaviour (6.7.3 of the standard).
>
> So all in all I believe applying this fix cannot make the code worse.

lets give this a try then. Seems nobody is willing to write a test tool anyway.

Regards

Marcel

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/