From 8d620314633583879ad42e588ec2c062257f98eb Mon Sep 17 00:00:00 2001
From: smlng <s@mlng.net>
Date: Wed, 30 Aug 2017 14:32:57 +0200
Subject: [PATCH] drivers: fix overshift and overflow errors on 8Bit MCUs

    - bmp180: fix overshift error
    - hih6130: fix overflow in sleep interval
    - pn532: fix overshift error
    - bh1750fvi: fix overshift error
    - mpu9050: fix overflow error
---
 drivers/bh1750fvi/bh1750fvi.c | 2 +-
 drivers/bmp180/bmp180.c       | 5 +++--
 drivers/hih6130/hih6130.c     | 2 +-
 drivers/mpu9150/mpu9150.c     | 4 ++--
 drivers/pn532/pn532.c         | 8 ++++----
 5 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/drivers/bh1750fvi/bh1750fvi.c b/drivers/bh1750fvi/bh1750fvi.c
index 226f9cfa04..cba0f70c49 100644
--- a/drivers/bh1750fvi/bh1750fvi.c
+++ b/drivers/bh1750fvi/bh1750fvi.c
@@ -71,7 +71,7 @@ uint16_t bh1750fvi_sample(const bh1750fvi_t *dev)
     i2c_release(dev->i2c);
 
     /* and finally we calculate the actual LUX value */
-    tmp = (raw[0] << 24) | (raw[1] << 16);
+    tmp = ((uint32_t)raw[0] << 24) | ((uint32_t)raw[1] << 16);
     tmp /= RES_DIV;
     return (uint16_t)(tmp);
 }
diff --git a/drivers/bmp180/bmp180.c b/drivers/bmp180/bmp180.c
index ff4c7e0485..1927c6e797 100644
--- a/drivers/bmp180/bmp180.c
+++ b/drivers/bmp180/bmp180.c
@@ -198,7 +198,7 @@ static int _read_ut(const bmp180_t *dev, int32_t *output)
         i2c_release(DEV_I2C);
         return -1;
     }
-    *output = ( ut[0] << 8 ) | ut[1];
+    *output = ((uint16_t)ut[0] << 8) | ut[1];
 
     DEBUG("UT: %i\n", (int)*output);
 
@@ -234,7 +234,8 @@ static int _read_up(const bmp180_t *dev, int32_t *output)
         return -1;
     }
 
-    *output = ((up[0] << 16) | (up[1] << 8) | up[2]) >> (8 - OVERSAMPLING);
+    *output = (((uint32_t)up[0] << 16) |
+               ((uint32_t)up[1] <<  8) | up[2]) >> (8 - OVERSAMPLING);
 
     DEBUG("UP: %i\n", (int)*output);
 
diff --git a/drivers/hih6130/hih6130.c b/drivers/hih6130/hih6130.c
index 1f476a0d97..0428240875 100644
--- a/drivers/hih6130/hih6130.c
+++ b/drivers/hih6130/hih6130.c
@@ -52,7 +52,7 @@ enum {
 };
 
 /** @brief Delay between requesting a measurement and data becoming ready */
-#define MEASUREMENT_DELAY   (50*1000)
+#define MEASUREMENT_DELAY   (50LU * US_PER_MS)
 
 /** @brief Trigger a new measurement on the sensor */
 static inline int hih6130_measurement_request(const hih6130_t *dev)
diff --git a/drivers/mpu9150/mpu9150.c b/drivers/mpu9150/mpu9150.c
index d30d9bcf2c..bbc74c630c 100644
--- a/drivers/mpu9150/mpu9150.c
+++ b/drivers/mpu9150/mpu9150.c
@@ -375,8 +375,8 @@ int mpu9150_read_temperature(const mpu9150_t *dev, int32_t *output)
     /* Release the bus */
     i2c_release(dev->i2c_dev);
 
-    temp = (data[0] << 8) | data[1];
-    *output = ((((int32_t)temp) * 1000) / 340) + (35*1000);
+    temp = ((uint16_t)data[0] << 8) | data[1];
+    *output = (((int32_t)temp * 1000LU) / 340) + (35 * 1000LU);
 
     return 0;
 }
diff --git a/drivers/pn532/pn532.c b/drivers/pn532/pn532.c
index d5d2476a96..7039691ba5 100644
--- a/drivers/pn532/pn532.c
+++ b/drivers/pn532/pn532.c
@@ -379,10 +379,10 @@ int pn532_fw_version(pn532_t *dev, uint32_t *fw_ver)
     buff[BUFF_CMD_START] = CMD_FIRMWARE_VERSION;
 
     if (send_rcv(dev, buff, 0, 4) == 4) {
-        *fw_ver = (buff[0] << 24);  /* ic version */
-        *fw_ver += (buff[1] << 16); /* fw ver */
-        *fw_ver += (buff[2] << 8);  /* fw rev */
-        *fw_ver += (buff[3]);       /* feature support */
+        *fw_ver =  ((uint32_t)buff[0] << 24);   /* ic version */
+        *fw_ver += ((uint32_t)buff[1] << 16);   /* fw ver */
+        *fw_ver += ((uint32_t)buff[2] << 8);    /* fw rev */
+        *fw_ver += (buff[3]);                   /* feature support */
         ret = 0;
     }
 
-- 
GitLab