diff mbox series

[11/42] drivers/hid: Convert snprintf to sysfs_emit

Message ID 20240116045151.3940401-9-lizhijian@fujitsu.com (mailing list archive)
State New
Delegated to: Jiri Kosina
Headers show
Series Fix coccicheck warnings | expand

Commit Message

Zhijian Li (Fujitsu) Jan. 16, 2024, 4:51 a.m. UTC
Per Documentation/filesystems/sysfs.rst, show() should only use sysfs_emit()
or sysfs_emit_at() when formatting the value to be returned to user space.

coccinelle complains that there are still a couple of functions that use
snprintf(). Convert them to sysfs_emit().

> ./drivers/hid/hid-picolcd_core.c:259:9-17: WARNING: please use sysfs_emit
> ./drivers/hid/hid-picolcd_core.c:304:8-16: WARNING: please use sysfs_emit
> ./drivers/hid/hid-sensor-custom.c:375:10-18: WARNING: please use sysfs_emi

No functional change intended

CC: "Bruno Prémont" <bonbons@linux-vserver.org>
CC: Jiri Kosina <jikos@kernel.org>
CC: Benjamin Tissoires <benjamin.tissoires@redhat.com>
CC: Jonathan Cameron <jic23@kernel.org>
CC: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
CC: linux-input@vger.kernel.org
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
 drivers/hid/hid-picolcd_core.c  | 6 +++---
 drivers/hid/hid-sensor-custom.c | 3 +--
 2 files changed, 4 insertions(+), 5 deletions(-)

Comments

Bruno Prémont Feb. 24, 2024, 4:59 p.m. UTC | #1
Hi Li,

While only looking for the snprintf() in your coccinelle analysis you
probably also want to look for sprintf() (see e.g. else clause in
hid-sensor-custom.c and scnprintf() (see picolcd_fb_update_rate_show
in hid-picolcd_fb.c) to replace more s*printf() calls with sysfs_emit*
variants.



diff --git a/drivers/hid/hid-picolcd_fb.c b/drivers/hid/hid-picolcd_fb.c
index d726aaafb146..03074d25d662 100644
--- a/drivers/hid/hid-picolcd_fb.c
+++ b/drivers/hid/hid-picolcd_fb.c
@@ -459,9 +459,9 @@ static ssize_t picolcd_fb_update_rate_show(struct device *dev,
 		if (ret >= PAGE_SIZE)
 			break;
 		else if (i == fb_update_rate)
-			ret += scnprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
+			ret += sysfs_emit_at(buf, ret, "[%u] ", i);
 		else
-			ret += scnprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
+			ret += sysfs_emit_at(buf, ret, "%u ", i);
 	if (ret > 0)
 		buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
 	return ret;


For hid-picolcd-*.c,
  Acked-by: Bruno Prémont <bonbons@linux-vserver.org>
but preferably with the scnprintf() case is covered too.


Cheers,
Bruno

On Tue, 16 Jan 2024 12:51:20 +0800 Li Zhijian wrote:
> Per Documentation/filesystems/sysfs.rst, show() should only use sysfs_emit()
> or sysfs_emit_at() when formatting the value to be returned to user space.
> 
> coccinelle complains that there are still a couple of functions that use
> snprintf(). Convert them to sysfs_emit().
> 
> > ./drivers/hid/hid-picolcd_core.c:259:9-17: WARNING: please use sysfs_emit
> > ./drivers/hid/hid-picolcd_core.c:304:8-16: WARNING: please use sysfs_emit
> > ./drivers/hid/hid-sensor-custom.c:375:10-18: WARNING: please use sysfs_emi  
> 
> No functional change intended
> 
> CC: "Bruno Prémont" <bonbons@linux-vserver.org>
> CC: Jiri Kosina <jikos@kernel.org>
> CC: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> CC: Jonathan Cameron <jic23@kernel.org>
> CC: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
> CC: linux-input@vger.kernel.org
> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
> ---
>  drivers/hid/hid-picolcd_core.c  | 6 +++---
>  drivers/hid/hid-sensor-custom.c | 3 +--
>  2 files changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c
> index bbda231a7ce3..fa46fb6eab3f 100644
> --- a/drivers/hid/hid-picolcd_core.c
> +++ b/drivers/hid/hid-picolcd_core.c
> @@ -256,9 +256,9 @@ static ssize_t picolcd_operation_mode_show(struct device *dev,
>  	struct picolcd_data *data = dev_get_drvdata(dev);
>  
>  	if (data->status & PICOLCD_BOOTLOADER)
> -		return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
> +		return sysfs_emit(buf, "[bootloader] lcd\n");
>  	else
> -		return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
> +		return sysfs_emit(buf, "bootloader [lcd]\n");
>  }
>  
>  static ssize_t picolcd_operation_mode_store(struct device *dev,
> @@ -301,7 +301,7 @@ static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
>  {
>  	struct picolcd_data *data = dev_get_drvdata(dev);
>  
> -	return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
> +	return sysfs_emit(buf, "%hu\n", data->opmode_delay);
>  }
>  
>  static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
> diff --git a/drivers/hid/hid-sensor-custom.c b/drivers/hid/hid-sensor-custom.c
> index d85398721659..4fe8dccf671d 100644
> --- a/drivers/hid/hid-sensor-custom.c
> +++ b/drivers/hid/hid-sensor-custom.c
> @@ -372,8 +372,7 @@ static ssize_t show_value(struct device *dev, struct device_attribute *attr,
>  				     sizeof(struct hid_custom_usage_desc),
>  				     usage_id_cmp);
>  		if (usage_desc)
> -			return snprintf(buf, PAGE_SIZE, "%s\n",
> -					usage_desc->desc);
> +			return sysfs_emit(buf, "%s\n", usage_desc->desc);
>  		else
>  			return sprintf(buf, "not-specified\n");

Shouldn't the sprintf() in the else clause be replaced as well?

>  	 } else
Zhijian Li (Fujitsu) Feb. 28, 2024, 9:56 a.m. UTC | #2
on 2/25/2024 12:59 AM, Bruno Prémont wrote:
> Hi Li,
>
> While only looking for the snprintf() in your coccinelle analysis you
> probably also want to look for sprintf() (see e.g. else clause in
> hid-sensor-custom.c and scnprintf() (see picolcd_fb_update_rate_show
> in hid-picolcd_fb.c) to replace more s*printf() calls with sysfs_emit*
> variants.

Yeah,  previously, i intended to do the conversion for the files 
reported by coccinelle(snprintf() only),
Think more, I should consider the whole s*printf() family  for these 
files at the same time.

This will be done in V2.


Thanks
Li, Zhijian



>
>
>
> diff --git a/drivers/hid/hid-picolcd_fb.c b/drivers/hid/hid-picolcd_fb.c
> index d726aaafb146..03074d25d662 100644
> --- a/drivers/hid/hid-picolcd_fb.c
> +++ b/drivers/hid/hid-picolcd_fb.c
> @@ -459,9 +459,9 @@ static ssize_t picolcd_fb_update_rate_show(struct device *dev,
>   		if (ret >= PAGE_SIZE)
>   			break;
>   		else if (i == fb_update_rate)
> -			ret += scnprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
> +			ret += sysfs_emit_at(buf, ret, "[%u] ", i);
>   		else
> -			ret += scnprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
> +			ret += sysfs_emit_at(buf, ret, "%u ", i);
>   	if (ret > 0)
>   		buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
>   	return ret;
>
>
> For hid-picolcd-*.c,
>    Acked-by: Bruno Prémont <bonbons@linux-vserver.org>
> but preferably with the scnprintf() case is covered too.
>
>
> Cheers,
> Bruno
>
> On Tue, 16 Jan 2024 12:51:20 +0800 Li Zhijian wrote:
>> Per Documentation/filesystems/sysfs.rst, show() should only use sysfs_emit()
>> or sysfs_emit_at() when formatting the value to be returned to user space.
>>
>> coccinelle complains that there are still a couple of functions that use
>> snprintf(). Convert them to sysfs_emit().
>>
>>> ./drivers/hid/hid-picolcd_core.c:259:9-17: WARNING: please use sysfs_emit
>>> ./drivers/hid/hid-picolcd_core.c:304:8-16: WARNING: please use sysfs_emit
>>> ./drivers/hid/hid-sensor-custom.c:375:10-18: WARNING: please use sysfs_emi
>> No functional change intended
>>
>> CC: "Bruno Prémont" <bonbons@linux-vserver.org>
>> CC: Jiri Kosina <jikos@kernel.org>
>> CC: Benjamin Tissoires <benjamin.tissoires@redhat.com>
>> CC: Jonathan Cameron <jic23@kernel.org>
>> CC: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
>> CC: linux-input@vger.kernel.org
>> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
>> ---
>>   drivers/hid/hid-picolcd_core.c  | 6 +++---
>>   drivers/hid/hid-sensor-custom.c | 3 +--
>>   2 files changed, 4 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c
>> index bbda231a7ce3..fa46fb6eab3f 100644
>> --- a/drivers/hid/hid-picolcd_core.c
>> +++ b/drivers/hid/hid-picolcd_core.c
>> @@ -256,9 +256,9 @@ static ssize_t picolcd_operation_mode_show(struct device *dev,
>>   	struct picolcd_data *data = dev_get_drvdata(dev);
>>   
>>   	if (data->status & PICOLCD_BOOTLOADER)
>> -		return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
>> +		return sysfs_emit(buf, "[bootloader] lcd\n");
>>   	else
>> -		return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
>> +		return sysfs_emit(buf, "bootloader [lcd]\n");
>>   }
>>   
>>   static ssize_t picolcd_operation_mode_store(struct device *dev,
>> @@ -301,7 +301,7 @@ static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
>>   {
>>   	struct picolcd_data *data = dev_get_drvdata(dev);
>>   
>> -	return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
>> +	return sysfs_emit(buf, "%hu\n", data->opmode_delay);
>>   }
>>   
>>   static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
>> diff --git a/drivers/hid/hid-sensor-custom.c b/drivers/hid/hid-sensor-custom.c
>> index d85398721659..4fe8dccf671d 100644
>> --- a/drivers/hid/hid-sensor-custom.c
>> +++ b/drivers/hid/hid-sensor-custom.c
>> @@ -372,8 +372,7 @@ static ssize_t show_value(struct device *dev, struct device_attribute *attr,
>>   				     sizeof(struct hid_custom_usage_desc),
>>   				     usage_id_cmp);
>>   		if (usage_desc)
>> -			return snprintf(buf, PAGE_SIZE, "%s\n",
>> -					usage_desc->desc);
>> +			return sysfs_emit(buf, "%s\n", usage_desc->desc);
>>   		else
>>   			return sprintf(buf, "not-specified\n");
> Shouldn't the sprintf() in the else clause be replaced as well?
>
>>   	 } else
diff mbox series

Patch

diff --git a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c
index bbda231a7ce3..fa46fb6eab3f 100644
--- a/drivers/hid/hid-picolcd_core.c
+++ b/drivers/hid/hid-picolcd_core.c
@@ -256,9 +256,9 @@  static ssize_t picolcd_operation_mode_show(struct device *dev,
 	struct picolcd_data *data = dev_get_drvdata(dev);
 
 	if (data->status & PICOLCD_BOOTLOADER)
-		return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
+		return sysfs_emit(buf, "[bootloader] lcd\n");
 	else
-		return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
+		return sysfs_emit(buf, "bootloader [lcd]\n");
 }
 
 static ssize_t picolcd_operation_mode_store(struct device *dev,
@@ -301,7 +301,7 @@  static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
 {
 	struct picolcd_data *data = dev_get_drvdata(dev);
 
-	return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
+	return sysfs_emit(buf, "%hu\n", data->opmode_delay);
 }
 
 static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
diff --git a/drivers/hid/hid-sensor-custom.c b/drivers/hid/hid-sensor-custom.c
index d85398721659..4fe8dccf671d 100644
--- a/drivers/hid/hid-sensor-custom.c
+++ b/drivers/hid/hid-sensor-custom.c
@@ -372,8 +372,7 @@  static ssize_t show_value(struct device *dev, struct device_attribute *attr,
 				     sizeof(struct hid_custom_usage_desc),
 				     usage_id_cmp);
 		if (usage_desc)
-			return snprintf(buf, PAGE_SIZE, "%s\n",
-					usage_desc->desc);
+			return sysfs_emit(buf, "%s\n", usage_desc->desc);
 		else
 			return sprintf(buf, "not-specified\n");
 	 } else