OSDN Git Service

net: dsa: fix unintended sign extension on a u16 left shift
authorColin Ian King <colin.king@canonical.com>
Mon, 9 Nov 2020 12:40:08 +0000 (12:40 +0000)
committerJakub Kicinski <kuba@kernel.org>
Wed, 11 Nov 2020 01:46:20 +0000 (17:46 -0800)
The left shift of u16 variable high is promoted to the type int and
then sign extended to a 64 bit u64 value.  If the top bit of high is
set then the upper 32 bits of the result end up being set by the
sign extension. Fix this by explicitly casting the value in high to
a u64 before left shifting by 16 places.

Also, remove the initialisation of variable value to 0 at the start
of each loop iteration as the value is never read and hence the
assignment it is redundant.

Addresses-Coverity: ("Unintended sign extension")
Fixes: e4b27ebc780f ("net: dsa: Add DSA driver for Hirschmann Hellcreek switches")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Reviewed-by: Kurt Kanzenbach <kurt@linutronix.de>
Link: https://lore.kernel.org/r/20201109124008.2079873-1-colin.king@canonical.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/dsa/hirschmann/hellcreek.c

index dfa66f7..d42f40c 100644 (file)
@@ -308,7 +308,7 @@ static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
                const struct hellcreek_counter *counter = &hellcreek_counter[i];
                u8 offset = counter->offset + port * 64;
                u16 high, low;
-               u64 value = 0;
+               u64 value;
 
                mutex_lock(&hellcreek->reg_lock);
 
@@ -320,7 +320,7 @@ static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
                 */
                high  = hellcreek_read(hellcreek, HR_CRDH);
                low   = hellcreek_read(hellcreek, HR_CRDL);
-               value = (high << 16) | low;
+               value = ((u64)high << 16) | low;
 
                hellcreek_port->counter_values[i] += value;
                data[i] = hellcreek_port->counter_values[i];