Skip to contentSkip to conversation

usb: replace snprintf in show functions with sysfs_emit

PATCH In review Qing Wang 2 participants 6 messages Oct 13, 2021 2 messages not in the archive USB

lore
Share & tools
lore

Qing Wang

author
#0 lore raw

PATCH 1/1 usb: replace snprintf in show functions with sysfs_emit

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the following coccicheck warning:
drivers/usb/core/sysfs.c:730:8-16: WARNING: use scnprintf or sprintf. drivers/usb/core/sysfs.c:921:8-16: WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
 drivers/usb/core/sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/core/sysfs.c +2 -2
index fa2e49d..6387c0d 100644
@@ -727,7 +727,7 @@ static ssize_t authorized_show(struct device *dev,
 			       struct device_attribute *attr, char *buf)
 {
 	struct usb_device *usb_dev = to_usb_device(dev);
-	return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
+	return sysfs_emit(buf, "%u\n", usb_dev->authorized);
 }
 
 /*
@@ -918,7 +918,7 @@ static ssize_t authorized_default_show(struct device *dev,
 	struct usb_hcd *hcd;
 
 	hcd = bus_to_hcd(usb_bus);
-	return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy);
+	return sysfs_emit(buf, "%u\n", hcd->dev_policy);
 }
 
 static ssize_t authorized_default_store(struct device *dev,
Signature · 1 line
2.7.4
#1 lore raw
Quoted 38 lines · Qing Wang

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the following coccicheck warning:
drivers/usb/core/sysfs.c:730:8-16: WARNING: use scnprintf or sprintf. drivers/usb/core/sysfs.c:921:8-16: WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
 drivers/usb/core/sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/core/sysfs.c +2 -2
index fa2e49d..6387c0d 100644
@@ -727,7 +727,7 @@ static ssize_t authorized_show(struct device *dev,
 			       struct device_attribute *attr, char *buf)
 {
 	struct usb_device *usb_dev = to_usb_device(dev);
-	return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
+	return sysfs_emit(buf, "%u\n", usb_dev->authorized);
 }
 
 /*
@@ -918,7 +918,7 @@ static ssize_t authorized_default_show(struct device *dev,
 	struct usb_hcd *hcd;
 
 	hcd = bus_to_hcd(usb_bus);
-	return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy);
+	return sysfs_emit(buf, "%u\n", hcd->dev_policy);
 }
 
 static ssize_t authorized_default_store(struct device *dev,
Signature · 1 line
2.7.4

If you are going to change this file, you should do this for all of the sysfs show functions in this file, not just 2 of them, right? Please change this patch to do that.

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): AA2A8gBxEnrQndzc*[email protected]

author
#3 lore raw

Quoted 44 lines
Quoted 29 lines · Qing Wang

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the following coccicheck warning:
drivers/usb/core/sysfs.c:730:8-16: WARNING: use scnprintf or sprintf. drivers/usb/core/sysfs.c:921:8-16: WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
  drivers/usb/core/sysfs.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/core/sysfs.c +1 -1
index fa2e49d..6387c0d 100644
@@ -727,7 +727,7 @@ static ssize_t authorized_show(struct device *dev,
                                struct device_attribute *attr, char *buf)
   {
         struct usb_device *usb_dev = to_usb_device(dev);
-     return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
+     return sysfs_emit(buf, "%u\n", usb_dev->authorized);
   }


  /*

drivers/usb/core/sysfs.c +0 -0
@@ -918,7 +918,7 @@ static ssize_t authorized_default_show(struct device *dev,
         struct usb_hcd *hcd;
Quoted 10 lines


        hcd = bus_to_hcd(usb_bus);
-     return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy); +     return sysfs_emit(buf, "%u\n", hcd->dev_policy); }

  static ssize_t authorized_default_store(struct device *dev,

Signature · 1 line
2.7.4

If you are going to change this file, you should do this for all of the sysfs show functions in this file, not just 2 of them, right?  Please change this patch to do that.

Quoted 3 lines

thanks,

greg k-h

Only these 2 snprintf need to be modified, other show functions used sprintf do not need to modify.

Thanks,

Qing

#4 lore raw
Quoted 45 lines · 王擎

Quoted 44 lines
Quoted 29 lines · Qing Wang

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the following coccicheck warning:
drivers/usb/core/sysfs.c:730:8-16: WARNING: use scnprintf or sprintf. drivers/usb/core/sysfs.c:921:8-16: WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
  drivers/usb/core/sysfs.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/core/sysfs.c +1 -1
index fa2e49d..6387c0d 100644
@@ -727,7 +727,7 @@ static ssize_t authorized_show(struct device *dev,
                                struct device_attribute *attr, char *buf)
   {
         struct usb_device *usb_dev = to_usb_device(dev);
-     return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
+     return sysfs_emit(buf, "%u\n", usb_dev->authorized);
   }


  /*

drivers/usb/core/sysfs.c +0 -0
@@ -918,7 +918,7 @@ static ssize_t authorized_default_show(struct device *dev,
         struct usb_hcd *hcd;
Quoted 10 lines


        hcd = bus_to_hcd(usb_bus);
-     return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy); +     return sysfs_emit(buf, "%u\n", hcd->dev_policy); }

  static ssize_t authorized_default_store(struct device *dev,

Signature · 1 line
2.7.4

If you are going to change this file, you should do this for all of the sysfs show functions in this file, not just 2 of them, right?  Please change this patch to do that.

Quoted 6 lines
Quoted 3 lines

thanks,

greg k-h

Only these 2 snprintf need to be modified, other show functions used sprintf do not need to modify.

I do not think you understand the change you are trying to make here.

Either the whole file should use the same api, or just leave it as-is as it obviously works properly today :)

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): AGUA7ADTEv9Q*[email protected]

author
#6 lore raw

Quoted 51 lines
Quoted 44 lines
Quoted 44 lines
Quoted 29 lines · Qing Wang

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the following coccicheck warning:
drivers/usb/core/sysfs.c:730:8-16: WARNING: use scnprintf or sprintf. drivers/usb/core/sysfs.c:921:8-16: WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
  drivers/usb/core/sysfs.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/core/sysfs.c +1 -1
index fa2e49d..6387c0d 100644
@@ -727,7 +727,7 @@ static ssize_t authorized_show(struct device *dev,
                                struct device_attribute *attr, char *buf)
   {
         struct usb_device *usb_dev = to_usb_device(dev);
-     return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
+     return sysfs_emit(buf, "%u\n", usb_dev->authorized);
   }


  /*

drivers/usb/core/sysfs.c +0 -0
@@ -918,7 +918,7 @@ static ssize_t authorized_default_show(struct device *dev,
         struct usb_hcd *hcd;
Quoted 10 lines


        hcd = bus_to_hcd(usb_bus);
-     return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy); +     return sysfs_emit(buf, "%u\n", hcd->dev_policy); }

  static ssize_t authorized_default_store(struct device *dev,

Signature · 1 line
2.7.4

If you are going to change this file, you should do this for all of the sysfs show functions in this file, not just 2 of them, right?  Please change this patch to do that.

Quoted 6 lines
Quoted 3 lines

thanks,

greg k-h

Only these 2 snprintf need to be modified, other show functions used sprintf do not need to modify.

Quoted 8 lines

I do not think you understand the change you are trying to make here.

Either the whole file should use the same api, or just leave it as-is as it obviously works properly today :)

thanks,

greg k-h

snprintf() returns the length of the string, not the length actually written. Here only correct this issue, as to whether it overflows should be guaranteed by the caller of sprintf().

Thanks,

Qing

#7 lore raw
Quoted 57 lines · 王擎

Quoted 56 lines
Quoted 44 lines
Quoted 44 lines
Quoted 29 lines · Qing Wang

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the following coccicheck warning:
drivers/usb/core/sysfs.c:730:8-16: WARNING: use scnprintf or sprintf. drivers/usb/core/sysfs.c:921:8-16: WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

---
  drivers/usb/core/sysfs.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/core/sysfs.c +1 -1
index fa2e49d..6387c0d 100644
@@ -727,7 +727,7 @@ static ssize_t authorized_show(struct device *dev,
                                struct device_attribute *attr, char *buf)
   {
         struct usb_device *usb_dev = to_usb_device(dev);
-     return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
+     return sysfs_emit(buf, "%u\n", usb_dev->authorized);
   }


  /*

drivers/usb/core/sysfs.c +0 -0
@@ -918,7 +918,7 @@ static ssize_t authorized_default_show(struct device *dev,
         struct usb_hcd *hcd;
Quoted 10 lines


        hcd = bus_to_hcd(usb_bus);
-     return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy); +     return sysfs_emit(buf, "%u\n", hcd->dev_policy); }

  static ssize_t authorized_default_store(struct device *dev,

Signature · 1 line
2.7.4

If you are going to change this file, you should do this for all of the sysfs show functions in this file, not just 2 of them, right?  Please change this patch to do that.

Quoted 6 lines
Quoted 3 lines

thanks,

greg k-h

Only these 2 snprintf need to be modified, other show functions used sprintf do not need to modify.

I do not think you understand the change you are trying to make here.

Either the whole file should use the same api, or just leave it as-is as it obviously works properly today :)

Quoted 7 lines
Quoted 3 lines

thanks,

greg k-h

snprintf() returns the length of the string, not the length actually written. Here only correct this issue, as to whether it overflows should be guaranteed by the caller of sprintf().

But for all of these, there is no overflow possible, that's not the issue here.

As-is, this code works just fine. If you really want to change it, as per the recommendation of some static checker, then do so for the whole file, to be unified and make sense overall. Do not just blindly follow a static tool for no good reason :)

thanks,

greg k-h