2020-06-03 17:56:45

by Sonny Sasaka

[permalink] [raw]
Subject: [PATCH] shared/util: Fix undefined behavior of left shift

When left-shifting 1, we should be explicit that it is an unsigned 1.
---
src/shared/util.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/shared/util.c b/src/shared/util.c
index 330a0722a..3b976fa16 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -140,7 +140,7 @@ uint8_t util_get_uid(unsigned int *bitmap, uint8_t max)
if (!id || id > max)
return 0;

- *bitmap |= 1 << (id - 1);
+ *bitmap |= 1u << (id - 1);

return id;
}
@@ -151,7 +151,7 @@ void util_clear_uid(unsigned int *bitmap, uint8_t id)
if (!id)
return;

- *bitmap &= ~(1 << (id - 1));
+ *bitmap &= ~(1u << (id - 1));
}

static const struct {
--
2.26.2


2020-06-03 21:08:50

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH] shared/util: Fix undefined behavior of left shift

Hi Sonny,

On Wed, Jun 3, 2020 at 10:58 AM Sonny Sasaka <[email protected]> wrote:
>
> When left-shifting 1, we should be explicit that it is an unsigned 1.
> ---
> src/shared/util.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/src/shared/util.c b/src/shared/util.c
> index 330a0722a..3b976fa16 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -140,7 +140,7 @@ uint8_t util_get_uid(unsigned int *bitmap, uint8_t max)
> if (!id || id > max)
> return 0;
>
> - *bitmap |= 1 << (id - 1);
> + *bitmap |= 1u << (id - 1);
>
> return id;
> }
> @@ -151,7 +151,7 @@ void util_clear_uid(unsigned int *bitmap, uint8_t id)
> if (!id)
> return;
>
> - *bitmap &= ~(1 << (id - 1));
> + *bitmap &= ~(1u << (id - 1));
> }
>
> static const struct {
> --
> 2.26.2

Applied, thanks.

--
Luiz Augusto von Dentz