[PATCH] usbip: handle length at sysfs show() functions

From: NÃmeth MÃrton
Date: Wed Jun 01 2011 - 01:18:11 EST


From: MÃrton NÃmeth <nm127@xxxxxxxxxxx>

The sysfs show() functions shall return the actual content length of
the result buffer. According to Documentation/filesystems/sysfs.txt:215
the scnprintf() function is preferred.

See also the article titled "snprintf() confusion" at
http://lwn.net/Articles/69419/ .

Signed-off-by: MÃrton NÃmeth <nm127@xxxxxxxxxxx>
---
diff --git a/drivers/staging/usbip/stub_dev.c b/drivers/staging/usbip/stub_dev.c
index 6e99ec8..af5f107 100644
--- a/drivers/staging/usbip/stub_dev.c
+++ b/drivers/staging/usbip/stub_dev.c
@@ -80,7 +80,7 @@ static ssize_t show_status(struct device *dev, struct device_attribute *attr,
status = sdev->ud.status;
spin_unlock(&sdev->ud.lock);

- return snprintf(buf, PAGE_SIZE, "%d\n", status);
+ return scnprintf(buf, PAGE_SIZE, "%d\n", status);
}
static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);

diff --git a/drivers/staging/usbip/stub_main.c b/drivers/staging/usbip/stub_main.c
index e9085d6..604e6bb 100644
--- a/drivers/staging/usbip/stub_main.c
+++ b/drivers/staging/usbip/stub_main.c
@@ -79,18 +79,22 @@ static ssize_t show_match_busid(struct device_driver *drv, char *buf)
{
int i;
char *out = buf;
+ int count = 0;

spin_lock(&busid_table_lock);

for (i = 0; i < MAX_BUSID; i++)
- if (busid_table[i].name[0])
- out += sprintf(out, "%s ", busid_table[i].name);
+ if (busid_table[i].name[0]) {
+ count += scnprintf(out, PAGE_SIZE - count,
+ "%s ", busid_table[i].name);
+ out = buf + count;
+ }

spin_unlock(&busid_table_lock);

- out += sprintf(out, "\n");
+ count += scnprintf(out, PAGE_SIZE - count, "\n");

- return out - buf;
+ return count;
}

static int add_match_busid(char *busid)
diff --git a/drivers/staging/usbip/usbip_common.c b/drivers/staging/usbip/usbip_common.c
index 433a3b6..127fdbb 100644
--- a/drivers/staging/usbip/usbip_common.c
+++ b/drivers/staging/usbip/usbip_common.c
@@ -43,7 +43,7 @@ EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
char *buf)
{
- return sprintf(buf, "%lx\n", usbip_debug_flag);
+ return scnprintf(buf, PAGE_SIZE, "%lx\n", usbip_debug_flag);
}

static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
diff --git a/drivers/staging/usbip/vhci_sysfs.c b/drivers/staging/usbip/vhci_sysfs.c
index d9736f9..1571882 100644
--- a/drivers/staging/usbip/vhci_sysfs.c
+++ b/drivers/staging/usbip/vhci_sysfs.c
@@ -27,10 +27,11 @@

/* Sysfs entry to show port status */
static ssize_t show_status(struct device *dev, struct device_attribute *attr,
- char *out)
+ char *buf)
{
- char *s = out;
+ char *out = buf;
int i = 0;
+ int count;

BUG_ON(!the_controller || !out);

@@ -46,32 +47,43 @@ static ssize_t show_status(struct device *dev, struct device_attribute *attr,
* up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
* port number and its peer IP address.
*/
- out += sprintf(out, "prt sta spd bus dev socket "
- "local_busid\n");
+ count = scnprintf(out, PAGE_SIZE,
+ "prt sta spd bus dev socket local_busid\n");
+ out = buf + count;

for (i = 0; i < VHCI_NPORTS; i++) {
struct vhci_device *vdev = port_to_vdev(i);

spin_lock(&vdev->ud.lock);
- out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
+ count += scnprintf(out, PAGE_SIZE - count,
+ "%03u %03u ", i, vdev->ud.status);
+ out = buf + count;

if (vdev->ud.status == VDEV_ST_USED) {
- out += sprintf(out, "%03u %08x ",
- vdev->speed, vdev->devid);
- out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
- out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
+ count += scnprintf(out, PAGE_SIZE - count,
+ "%03u %08x ", vdev->speed, vdev->devid);
+ out = buf + count;
+ count += scnprintf(out, PAGE_SIZE - count,
+ "%16p ", vdev->ud.tcp_socket);
+ out = buf + count;
+ count += scnprintf(out, PAGE_SIZE - count,
+ "%s", dev_name(&vdev->udev->dev));
+ out = buf + count;

} else {
- out += sprintf(out, "000 000 000 0000000000000000 0-0");
+ count += scnprintf(out, PAGE_SIZE - count,
+ "000 000 000 0000000000000000 0-0");
+ out = buf + count;
}

- out += sprintf(out, "\n");
+ count += scnprintf(out, PAGE_SIZE - count, "\n");
+ out = buf + count;
spin_unlock(&vdev->ud.lock);
}

spin_unlock(&the_controller->lock);

- return out - s;
+ return count;
}
static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/