2022-02-17 12:01:35

by Tedd Ho-Jeong An

[permalink] [raw]
Subject: [BlueZ PATCH] shared: Fix the incorrect type with bit shift

From: Tedd Ho-Jeong An <[email protected]>

This patch fixes the following runtime error:

$ sudo ./monitor/btmon -w test.btsnoop
Bluetooth monitor ver 5.63
src/shared/btsnoop.c:339:18: runtime error: left shift of 65535 by 16 places cannot be represented in type 'int'
---
src/shared/btsnoop.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/shared/btsnoop.c b/src/shared/btsnoop.c
index a29bc928f..0a68282bc 100644
--- a/src/shared/btsnoop.c
+++ b/src/shared/btsnoop.c
@@ -336,7 +336,7 @@ bool btsnoop_write_hci(struct btsnoop *btsnoop, struct timeval *tv,
break;

case BTSNOOP_FORMAT_MONITOR:
- flags = (index << 16) | opcode;
+ flags = ((uint32_t)index << 16) | opcode;
break;

default:
--
2.25.1


2022-02-17 18:09:26

by Tedd Ho-Jeong An

[permalink] [raw]
Subject: Re: [BlueZ PATCH] shared: Fix the incorrect type with bit shift

Hi Marcel,

On Thu, 2022-02-17 at 10:55 +0100, Marcel Holtmann wrote:
> Hi Tedd,
>
> > This patch fixes the following runtime error:
> >
> > $ sudo ./monitor/btmon -w test.btsnoop
> >  Bluetooth monitor ver 5.63
> >  src/shared/btsnoop.c:339:18: runtime error: left shift of 65535 by 16 places cannot be represented in type 'int'
>
> what compiler version is this? Or what warnings did you enable? Since this is weird, so while yes 0xffff can not be shifted, but we are also not shifting into an uint16_t. The fix is fine, I am just
> curious. There will be other places with the same problem.

I am using gcc 9.3.0 came with Ubuntu 20.04.
This is enabled by the "-fsanitize=undefined" option.

Recent change enabled LSAN, ASAN, and UBSAN by default.


>
> Regards
>
> Marcel
>


2022-02-17 18:31:42

by Marcel Holtmann

[permalink] [raw]
Subject: Re: [BlueZ PATCH] shared: Fix the incorrect type with bit shift

Hi Tedd,

> This patch fixes the following runtime error:
>
> $ sudo ./monitor/btmon -w test.btsnoop
> Bluetooth monitor ver 5.63
> src/shared/btsnoop.c:339:18: runtime error: left shift of 65535 by 16 places cannot be represented in type 'int'

what compiler version is this? Or what warnings did you enable? Since this is weird, so while yes 0xffff can not be shifted, but we are also not shifting into an uint16_t. The fix is fine, I am just curious. There will be other places with the same problem.

Regards

Marcel