diff mbox

[v1] HID: core: don't use negative operands when shift

Message ID 20170613092222.4073-1-andriy.shevchenko@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Andy Shevchenko June 13, 2017, 9:22 a.m. UTC
From: Andy Shevchenko <andy.shevchenko@gmail.com>

The recent C standard in 6.5.7 paragraph 4 defines that operands for
bitwise shift operators should be non-negative, otherwise it's an
undefined behaviour.

Signed-off-by: Andy Shevchenko <andy.shevchenko@gmail.com>
---
 drivers/hid/hid-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Benjamin Tissoires June 13, 2017, 9:59 a.m. UTC | #1
On Jun 13 2017 or thereabouts, Andy Shevchenko wrote:
> From: Andy Shevchenko <andy.shevchenko@gmail.com>
> 
> The recent C standard in 6.5.7 paragraph 4 defines that operands for
> bitwise shift operators should be non-negative, otherwise it's an
> undefined behaviour.
> 
> Signed-off-by: Andy Shevchenko <andy.shevchenko@gmail.com>

Acked-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

> ---
>  drivers/hid/hid-core.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
> index 5f87dbe28336..73416fef983e 100644
> --- a/drivers/hid/hid-core.c
> +++ b/drivers/hid/hid-core.c
> @@ -1046,7 +1046,7 @@ static s32 snto32(__u32 value, unsigned n)
>  	case 16: return ((__s16)value);
>  	case 32: return ((__s32)value);
>  	}
> -	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
> +	return value & (1 << (n - 1)) ? value | (~0U << n) : value;
>  }
>  
>  s32 hid_snto32(__u32 value, unsigned n)
> -- 
> 2.11.0
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jiri Kosina June 13, 2017, 12:29 p.m. UTC | #2
On Tue, 13 Jun 2017, Andy Shevchenko wrote:

> From: Andy Shevchenko <andy.shevchenko@gmail.com>
> 
> The recent C standard in 6.5.7 paragraph 4 defines that operands for
> bitwise shift operators should be non-negative, otherwise it's an
> undefined behaviour.
> 
> Signed-off-by: Andy Shevchenko <andy.shevchenko@gmail.com>

Applied, thanks.
diff mbox

Patch

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 5f87dbe28336..73416fef983e 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1046,7 +1046,7 @@  static s32 snto32(__u32 value, unsigned n)
 	case 16: return ((__s16)value);
 	case 32: return ((__s32)value);
 	}
-	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
+	return value & (1 << (n - 1)) ? value | (~0U << n) : value;
 }
 
 s32 hid_snto32(__u32 value, unsigned n)