Skip to contentSkip to conversation

HID: core: don't use negative operands when shift

PATCH Applied Andy Shevchenko 3 participants 3 messages Jun 13, 2017 HID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: core: don't use negative operands when shift

From: Andy Shevchenko <[email protected]>

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.

---
 drivers/hid/hid-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/hid-core.c +1 -1
index 5f87dbe28336..73416fef983e 100644
@@ -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)
Signature · 1 line
2.11.0
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Andy Shevchenko

From: Andy Shevchenko <[email protected]>

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.

Quoted 20 lines
---
 drivers/hid/hid-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/hid-core.c +1 -1
index 5f87dbe28336..73416fef983e 100644
@@ -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)
Signature · 1 line
2.11.0