Skip to contentSkip to conversation

hwmon: (emc2103) Fix use of an uninitilized variable in error case

PATCH In review Guenter Roeck 2 participants 3 messages Jun 4, 2012 – Jun 13, 2012

Subsystems (2)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: (emc2103) Fix use of an uninitilized variable in error case

Fix:

emc2103.c: In function set_pwm_enable:
emc2103.c:463:12: warning: conf_reg may be used uninitialized in this function

by checking the return value from read_u8_from_i2c(). This fixes a real problem, as conf_reg is really uninitialized if read_u8_from_i2c returns an error.

Cc Steve Glendinning <[email protected]>
---
 drivers/hwmon/emc2103.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
drivers/hwmon/emc2103.c +8 -4
index 9691f66..ff68563 100644
@@ -451,11 +451,13 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
 		data->fan_rpm_control = true;
 		break;
 	default:
-		mutex_unlock(&data->update_lock);
-		return -EINVAL;
+		result = -EINVAL;
+		goto err;
 	}
 
-	read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
+	result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
+	if (result)
+		goto err;
 
 	if (data->fan_rpm_control)
 		conf_reg |= 0x80;
@@ -464,8 +466,10 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
 
 	i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
 
+	result = count;
+err:
 	mutex_unlock(&data->update_lock);
-	return count;
+	return result;
 }
 
 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
Signature · 7 lines
1.7.5.4


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw

Hi Guenter,

Quoted 5 lines · Guenter Roeck

Fix:

emc2103.c: In function set_pwm_enable:
emc2103.c:463:12: warning: conf_reg may be used uninitialized in this function

Quoted 8 lines

by checking the return value from read_u8_from_i2c(). This fixes a real problem, as conf_reg is really uninitialized if read_u8_from_i2c returns an error.

Cc Steve Glendinning <[email protected]>
---
 drivers/hwmon/emc2103.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

Good catch. Still I'm a little worried about...

Quoted 23 lines
drivers/hwmon/emc2103.c +5 -3
index 9691f66..ff68563 100644
@@ -451,11 +451,13 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
 		data->fan_rpm_control = true;
 		break;
 	default:
-		mutex_unlock(&data->update_lock);
-		return -EINVAL;
+		result = -EINVAL;
+		goto err;
 	}
 
-	read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
+	result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
+	if (result)
+		goto err;
 
 	if (data->fan_rpm_control)
 		conf_reg |= 0x80;

drivers/hwmon/emc2103.c static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da, · line 469

Quoted 4 lines
drivers/hwmon/emc2103.c +1 -0
@@ -464,8 +466,10 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
 
 	i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
 
+	result = count;

... this. Is it guaranteed that sizeof(int) >= sizeof(size_t)? Probably it doesn't matter much in practice, but I'm still wondering. I think I would prefer if you'd return "count" and stuff error codes in it when needed, rather than returning "result".

But you can also just ignore me if you think it's irrelevant. Both cases, you have my ack.

Quoted 7 lines
drivers/hwmon/emc2103.c +2 -1
+err:
 	mutex_unlock(&data->update_lock);
-	return count;
+	return result;
 }
 
 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#2 lore raw
In reply to Guenter Roeck
Quoted 11 lines · Jean Delvare

Hi Guenter,

Quoted 8 lines · Guenter Roeck

Fix:

emc2103.c: In function set_pwm_enable:
emc2103.c:463:12: warning: conf_reg may be used uninitialized in this function

by checking the return value from read_u8_from_i2c(). This fixes a real problem, as conf_reg is really uninitialized if read_u8_from_i2c returns an error.

Quoted 8 lines
Quoted 5 lines
Cc Steve Glendinning <[email protected]>
---
 drivers/hwmon/emc2103.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

Good catch. Still I'm a little worried about...

thanks to those modern compilers ...

Quoted 33 lines
Quoted 23 lines
drivers/hwmon/emc2103.c +5 -3
index 9691f66..ff68563 100644
@@ -451,11 +451,13 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
 		data->fan_rpm_control = true;
 		break;
 	default:
-		mutex_unlock(&data->update_lock);
-		return -EINVAL;
+		result = -EINVAL;
+		goto err;
 	}
 
-	read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
+	result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
+	if (result)
+		goto err;
 
 	if (data->fan_rpm_control)
 		conf_reg |= 0x80;

drivers/hwmon/emc2103.c static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da, · line 469

Quoted 4 lines
drivers/hwmon/emc2103.c +1 -0
@@ -464,8 +466,10 @@ static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
 
 	i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
 
+	result = count;

... this. Is it guaranteed that sizeof(int) >= sizeof(size_t)? Probably it doesn't matter much in practice, but I'm still wondering. I think I would prefer if you'd return "count" and stuff error codes in it when needed, rather than returning "result".

Quoted 3 lines

But you can also just ignore me if you think it's irrelevant. Both cases, you have my ack.

Hi Jean,

good point. sizeof(size_t) _can_ be larger than sizeof(int), and often is. Sure, it doesn't matter much in practice, as count is hopefully never that large, but it is better to be careful. I changed the code as you suggested, ie return count and assign the error code to it if needed.

Thanks a lot for the review.

Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors