compatible_show() incorrectly advances the buffer pointer by the
accumulated output length instead of the length of the last string.
If the property contains three or more strings, this leaves gaps in the
output and can truncate or omit later strings.

For example, a PowerMac11,2 OF dump [1] shows the compatible strings
"mac-io-i2c-control", "k2-i2c", and "keywest-i2c". The first two strings
take 19 and 7 bytes, but buf is advanced by 19 and then by 26, so the
third string is written at offset 45. However, only 38 bytes are
returned, so "keywest-i2c" is omitted from the sysfs output.

Use of_property_for_each_string() to iterate over the compatible strings
and sysfs_emit_at() to append each string at the correct offset.

Link: https://gist.github.com/rokuyama/079eebc318ad9417665f2c023527dac4 [1]
Fixes: b5bf5b6786cc ("[PATCH] openfirmware: add sysfs nodes for open firmware 
devices")
Cc: [email protected]
Signed-off-by: Thorsten Blum <[email protected]>
---
Compile-tested only.

 drivers/macintosh/macio_sysfs.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)

diff --git a/drivers/macintosh/macio_sysfs.c b/drivers/macintosh/macio_sysfs.c
index 2bbe359b26d9..59fb2a51de2d 100644
--- a/drivers/macintosh/macio_sysfs.c
+++ b/drivers/macintosh/macio_sysfs.c
@@ -3,30 +3,18 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/stat.h>
+#include <linux/sysfs.h>
 #include <asm/macio.h>
 
 static ssize_t
 compatible_show (struct device *dev, struct device_attribute *attr, char *buf)
 {
-       struct platform_device *of;
+       struct property *prop;
        const char *compat;
-       int cplen;
        int length = 0;
 
-       of = &to_macio_device (dev)->ofdev;
-       compat = of_get_property(of->dev.of_node, "compatible", &cplen);
-       if (!compat) {
-               *buf = '\0';
-               return 0;
-       }
-       while (cplen > 0) {
-               int l;
-               length += sprintf (buf, "%s\n", compat);
-               buf += length;
-               l = strlen (compat) + 1;
-               compat += l;
-               cplen -= l;
-       }
+       of_property_for_each_string(dev->of_node, "compatible", prop, compat)
+               length += sysfs_emit_at(buf, length, "%s\n", compat);
 
        return length;
 }

Reply via email to