[PATCH] of: module: remove strlen() call in of_modalias()

Sergey Shtylyov
2024-10-15T20:15:14Z

In of_modalias(), there's no dire need to call strlen() (and then add 1
to its result to account for the 'C' char preceding the compat string).
Replace that strlen() with snprintf() (currently below it) -- this way,
we always try to print the compat string but then only advance the str
and len parameters iff the comapt string fit into the remaining buffer
space...

Signed-off-by: Sergey Shtylyov <s.shtylyov@omp.ru>

---
The patch is against the dt/next branch of Rob Herring's linux.git repo...

drivers/of/module.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

Index: linux/drivers/of/module.c
===================================================================
--- linux.orig/drivers/of/module.c
+++ linux/drivers/of/module.c
@@ -35,12 +35,10 @@ ssize_t of_modalias(const struct device_
str += csize;

of_property_for_each_string(np, "compatible", p, compat) {
- csize = strlen(compat) + 1;
+ csize = snprintf(str, len, "C%s", compat);
tsize += csize;
if (csize >= len)
continue;
-
- csize = snprintf(str, len, "C%s", compat);
for (c = str; c; ) {
c = strchr(c, ' ');
if (c)