diff mbox

drm/nouveau/hwmon: Uninitialized variables in sysfs

Message ID 20170717081711.4mj76r6qf2de4em6@mwanda (mailing list archive)
State New, archived
Headers show

Commit Message

Dan Carpenter July 17, 2017, 8:17 a.m. UTC
kstrtol() and friends can return -EINVAL or -ERANGE.  We have to test
for both, otherwise the value is possibly uninitialized.  Also in some
of these files we accidentally return "count" on error instead of a
negative error code.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Comments

Pierre Moreau July 22, 2017, 12:13 p.m. UTC | #1
Reviewed-by: Pierre Moreau <pierre.morrow@free.fr>

On 2017-07-17 — 11:17, Dan Carpenter wrote:
> kstrtol() and friends can return -EINVAL or -ERANGE.  We have to test
> for both, otherwise the value is possibly uninitialized.  Also in some
> of these files we accidentally return "count" on error instead of a
> negative error code.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> diff --git a/drivers/gpu/drm/nouveau/nouveau_hwmon.c b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> index 7c965648df80..5e75af91c446 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> @@ -68,9 +68,11 @@ nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
>  	struct nouveau_drm *drm = nouveau_drm(dev);
>  	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
>  	long value;
> +	int ret;
>  
> -	if (kstrtol(buf, 10, &value) == -EINVAL)
> -		return count;
> +	ret = kstrtol(buf, 10, &value);
> +	if (ret)
> +		return ret;
>  
>  	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
>  			value / 1000);
> @@ -101,9 +103,11 @@ nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
>  	struct nouveau_drm *drm = nouveau_drm(dev);
>  	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
>  	long value;
> +	int ret;
>  
> -	if (kstrtol(buf, 10, &value) == -EINVAL)
> -		return count;
> +	ret = kstrtol(buf, 10, &value);
> +	if (ret)
> +		return ret;
>  
>  	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
>  			value / 1000);
> @@ -156,8 +160,9 @@ nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
>  	long value;
>  	int ret;
>  
> -	if (kstrtol(buf, 10, &value) == -EINVAL)
> -		return -EINVAL;
> +	ret = kstrtol(buf, 10, &value);
> +	if (ret)
> +		return ret;
>  
>  	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
>  	if (ret < 0)
> @@ -179,8 +184,9 @@ nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
>  	long value;
>  	int ret;
>  
> -	if (kstrtol(buf, 10, &value) == -EINVAL)
> -		return -EINVAL;
> +	ret = kstrtol(buf, 10, &value);
> +	if (ret)
> +		return ret;
>  
>  	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
>  	if (ret < 0)
> _______________________________________________
> Nouveau mailing list
> Nouveau@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/nouveau
Pierre Moreau Dec. 29, 2017, 9:56 p.m. UTC | #2
Ping: this can still be an issue today as kstrtol() & co haven’t changed their
possible return value since the patch was written.

On 2017-07-22 — 14:13, Pierre Moreau wrote:
> Reviewed-by: Pierre Moreau <pierre.morrow@free.fr>
> 
> On 2017-07-17 — 11:17, Dan Carpenter wrote:
> > kstrtol() and friends can return -EINVAL or -ERANGE.  We have to test
> > for both, otherwise the value is possibly uninitialized.  Also in some
> > of these files we accidentally return "count" on error instead of a
> > negative error code.
> > 
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > 
> > diff --git a/drivers/gpu/drm/nouveau/nouveau_hwmon.c b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> > index 7c965648df80..5e75af91c446 100644
> > --- a/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> > +++ b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> > @@ -68,9 +68,11 @@ nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
> >  	struct nouveau_drm *drm = nouveau_drm(dev);
> >  	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
> >  	long value;
> > +	int ret;
> >  
> > -	if (kstrtol(buf, 10, &value) == -EINVAL)
> > -		return count;
> > +	ret = kstrtol(buf, 10, &value);
> > +	if (ret)
> > +		return ret;
> >  
> >  	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
> >  			value / 1000);
> > @@ -101,9 +103,11 @@ nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
> >  	struct nouveau_drm *drm = nouveau_drm(dev);
> >  	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
> >  	long value;
> > +	int ret;
> >  
> > -	if (kstrtol(buf, 10, &value) == -EINVAL)
> > -		return count;
> > +	ret = kstrtol(buf, 10, &value);
> > +	if (ret)
> > +		return ret;
> >  
> >  	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
> >  			value / 1000);
> > @@ -156,8 +160,9 @@ nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
> >  	long value;
> >  	int ret;
> >  
> > -	if (kstrtol(buf, 10, &value) == -EINVAL)
> > -		return -EINVAL;
> > +	ret = kstrtol(buf, 10, &value);
> > +	if (ret)
> > +		return ret;
> >  
> >  	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
> >  	if (ret < 0)
> > @@ -179,8 +184,9 @@ nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
> >  	long value;
> >  	int ret;
> >  
> > -	if (kstrtol(buf, 10, &value) == -EINVAL)
> > -		return -EINVAL;
> > +	ret = kstrtol(buf, 10, &value);
> > +	if (ret)
> > +		return ret;
> >  
> >  	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
> >  	if (ret < 0)
> > _______________________________________________
> > Nouveau mailing list
> > Nouveau@lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/nouveau



> _______________________________________________
> Nouveau mailing list
> Nouveau@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/nouveau
diff mbox

Patch

diff --git a/drivers/gpu/drm/nouveau/nouveau_hwmon.c b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
index 7c965648df80..5e75af91c446 100644
--- a/drivers/gpu/drm/nouveau/nouveau_hwmon.c
+++ b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
@@ -68,9 +68,11 @@  nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
 	struct nouveau_drm *drm = nouveau_drm(dev);
 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
 	long value;
+	int ret;
 
-	if (kstrtol(buf, 10, &value) == -EINVAL)
-		return count;
+	ret = kstrtol(buf, 10, &value);
+	if (ret)
+		return ret;
 
 	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
 			value / 1000);
@@ -101,9 +103,11 @@  nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
 	struct nouveau_drm *drm = nouveau_drm(dev);
 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
 	long value;
+	int ret;
 
-	if (kstrtol(buf, 10, &value) == -EINVAL)
-		return count;
+	ret = kstrtol(buf, 10, &value);
+	if (ret)
+		return ret;
 
 	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
 			value / 1000);
@@ -156,8 +160,9 @@  nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
 	long value;
 	int ret;
 
-	if (kstrtol(buf, 10, &value) == -EINVAL)
-		return -EINVAL;
+	ret = kstrtol(buf, 10, &value);
+	if (ret)
+		return ret;
 
 	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
 	if (ret < 0)
@@ -179,8 +184,9 @@  nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
 	long value;
 	int ret;
 
-	if (kstrtol(buf, 10, &value) == -EINVAL)
-		return -EINVAL;
+	ret = kstrtol(buf, 10, &value);
+	if (ret)
+		return ret;
 
 	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
 	if (ret < 0)