Skip to contentSkip to conversation

iio: maxim_thermocouple: detect invalid storage size in read()

PATCH In review Arnd Bergmann 2 participants 2 messages Oct 25, 2016 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: maxim_thermocouple: detect invalid storage size in read()

As found by gcc -Wmaybe-uninitialized, having a storage_bytes value other than 2 or 4 will result in undefined behavior:

drivers/iio/temperature/maxim_thermocouple.c: In function 'maxim_thermocouple_read':
drivers/iio/temperature/maxim_thermocouple.c:141:5: error: 'ret' may be used uninitialized in this function [-Werror=maybe-uninitialized]

This probably cannot happen, but returning -EINVAL here is appropriate and makes gcc happy and the code more robust.

Fixes 231147ee77f3 ("iio: maxim_thermocouple: Align 16 bit big endian value of raw reads")
---
 drivers/iio/temperature/maxim_thermocouple.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/temperature/maxim_thermocouple.c +2 -0
index 066161a4bccd..f962f31a5eb2 100644
@@ -136,6 +136,8 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 		ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
 		*val = be32_to_cpu(buf32);
 		break;
+	default:
+		ret = -EINVAL;
 	}
 
 	if (ret)
Signature · 1 line
2.9.0
maintainer
#1 lore raw
Quoted 8 lines · Arnd Bergmann

As found by gcc -Wmaybe-uninitialized, having a storage_bytes value other than 2 or 4 will result in undefined behavior:

drivers/iio/temperature/maxim_thermocouple.c: In function 'maxim_thermocouple_read':
drivers/iio/temperature/maxim_thermocouple.c:141:5: error: 'ret' may be used uninitialized in this function [-Werror=maybe-uninitialized]

This probably cannot happen, but returning -EINVAL here is appropriate and makes gcc happy and the code more robust.

Agreed on all counts.

Quoted 3 lines
Fixes 231147ee77f3 ("iio: maxim_thermocouple: Align 16 bit big endian value of raw reads")

Applied to the togreg branch of iio.git. Initially pushed out as testing for the autobuilders to play with it.

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/temperature/maxim_thermocouple.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/temperature/maxim_thermocouple.c +2 -0
index 066161a4bccd..f962f31a5eb2 100644
@@ -136,6 +136,8 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 		ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
 		*val = be32_to_cpu(buf32);
 		break;
+	default:
+		ret = -EINVAL;
 	}
 
 	if (ret)