iio: ssp_sensors: avoid uninitialized variable usage
PATCH 1/1 iio: ssp_sensors: avoid uninitialized variable usage
clang points out that 'calculated_time' is only sometimes initialized here, which leads to incorrect data being passed into another function:
drivers/iio/common/ssp_sensors/ssp_iio.c:95:6: error: variable 'calculated_time' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
if (indio_dev->scan_timestamp) {
^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iio/common/ssp_sensors/ssp_iio.c:102:9: note: uninitialized use occurs here
calculated_time);
^~~~~~~~~~~~~~~
drivers/iio/common/ssp_sensors/ssp_iio.c:95:2: note: remove the 'if' if its condition is always true
if (indio_dev->scan_timestamp) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iio/common/ssp_sensors/ssp_iio.c:84:25: note: initialize the variable 'calculated_time' to silence this warning
int64_t calculated_time;
^The data is subsequently ignored by iio_push_to_buffers_with_timestamp(), but the warning still feels legitimate and to work around it, we can initialize the time in the other case.
--- drivers/iio/common/ssp_sensors/ssp_iio.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/iio/common/ssp_sensors/ssp_iio.c +2 -0
@@ -96,6 +96,8 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,memcpy (&time, &((char *)buf)[len],SSP_TIME_SIZE ); calculated_time = timestamp + (int64_t )le32_to_cpu (time) *1000000 ; + }else { + calculated_time =0 ; } return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
Signature · 1 line
2.20.0
Quoted 42 lines · Arnd Bergmann
clang points out that '
calculated_time' is only sometimes initialized here, which leads to incorrect data being passed into another function:
drivers/iio/common/ssp_sensors/ssp_iio.c:95:6: error: variable 'calculated_time' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]if (indio_dev->scan_timestamp) { ^~~~~~~~~~~~~~~~~~~~~~~~~ drivers/iio/common/ssp_sensors/ssp_iio.c:102:9: note: uninitialized use occurs here calculated_time); ^~~~~~~~~~~~~~~ drivers/iio/common/ssp_sensors/ssp_iio.c:95:2: note: remove the 'if' if its condition is always true if (indio_dev->scan_timestamp) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/iio/common/ssp_sensors/ssp_iio.c:84:25: note: initialize the variable 'calculated_time' to silence this warning int64_t calculated_time; ^The data is subsequently ignored by
iio_push_to_buffers_with_timestamp(), but the warning still feels legitimate and to work around it, we can initialize the time in the other case.Signed-off-by Arnd Bergmann <[email protected]>--- drivers/iio/common/ssp_sensors/ssp_iio.c | 2 ++ 1 file changed, 2 insertions(+)drivers/iio/common/ssp_sensors/ssp_iio.c +2
-0@@ -96,6 +96,8 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,memcpy (&time, &((char *)buf)[len],SSP_TIME_SIZE ); calculated_time = timestamp + (int64_t )le32_to_cpu (time) *1000000 ; + }else { + calculated_time =0 ; } return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,Signature · 1 line
2.20.0
I sent a similar change, which is sitting in Jonathan's testing branch:
You made a good point previously that initializing the variable at the beginning of a function may not always be the best choice. I don't have a personal preference for which patch stays around so:
Just in case.
Nathan
Jonathan Cameron
maintainerQuoted 52 lines · On Fri, 22 Mar 2019 08:01:19 -0700 Nathan Chancellor…
Quoted 42 lines · Arnd Bergmann
clang points out that '
calculated_time' is only sometimes initialized here, which leads to incorrect data being passed into another function:
drivers/iio/common/ssp_sensors/ssp_iio.c:95:6: error: variable 'calculated_time' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]if (indio_dev->scan_timestamp) { ^~~~~~~~~~~~~~~~~~~~~~~~~ drivers/iio/common/ssp_sensors/ssp_iio.c:102:9: note: uninitialized use occurs here calculated_time); ^~~~~~~~~~~~~~~ drivers/iio/common/ssp_sensors/ssp_iio.c:95:2: note: remove the 'if' if its condition is always true if (indio_dev->scan_timestamp) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/iio/common/ssp_sensors/ssp_iio.c:84:25: note: initialize the variable 'calculated_time' to silence this warning int64_t calculated_time; ^The data is subsequently ignored by
iio_push_to_buffers_with_timestamp(), but the warning still feels legitimate and to work around it, we can initialize the time in the other case.Signed-off-by Arnd Bergmann <[email protected]>--- drivers/iio/common/ssp_sensors/ssp_iio.c | 2 ++ 1 file changed, 2 insertions(+)drivers/iio/common/ssp_sensors/ssp_iio.c +2
-0@@ -96,6 +96,8 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,memcpy (&time, &((char *)buf)[len],SSP_TIME_SIZE ); calculated_time = timestamp + (int64_t )le32_to_cpu (time) *1000000 ; + }else { + calculated_time =0 ; } return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,Signature · 1 line
2.20.0I sent a similar change, which is sitting in Jonathan's testing branch:
You made a good point previously that initializing the variable at the beginning of a function may not always be the best choice. I don't have a personal preference for which patch stays around so:
Quoted 5 lines
Reviewed-by Nathan Chancellor <[email protected]>Just in case.
Nathan
I'll stick to the original, mostly to avoid unnecessary churn. Apologies for the delay in pull requests making these more generally visible. Always takes me a while to get one out after RC1. Would imagine these will go to Greg and hence linux-next sometime in the next week.
Jonathan