Skip to contentSkip to conversation

ARM: OMAP2: use snprintf() in multiple check revision functions

PATCH New Thorsten Blum 1 participant 4 messages Apr 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 ARM: OMAP2: use snprintf() in multiple check revision functions

Replace unbounded sprintf() calls with the safer snprintf() in multiple *_check_revision() functions.

---
 arch/arm/mach-omap2/id.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
arch/arm/mach-omap2/id.c +11 -11
index cf2bfb447ee2..44d7471666a8 100644
@@ -192,8 +192,8 @@ void __init omap2xxx_check_revision(void)
 		j = i;
 	}
 
-	sprintf(soc_name, "OMAP%04x", omap_rev() >> 16);
-	sprintf(soc_rev, "ES%x", (omap_rev() >> 12) & 0xf);
+	snprintf(soc_name, sizeof(soc_name), "OMAP%04x", omap_rev() >> 16);
+	snprintf(soc_rev, sizeof(soc_rev), "ES%x", (omap_rev() >> 12) & 0xf);
 
 	pr_info("%s", soc_name);
 	if ((omap_rev() >> 8) & 0x0f)
@@ -519,7 +519,7 @@ void __init omap3xxx_check_revision(void)
 		pr_warn("Warning: unknown chip type: hawkeye %04x, assuming OMAP3630ES1.2\n",
 			hawkeye);
 	}
-	sprintf(soc_rev, "ES%s", cpu_rev);
+	snprintf(soc_rev, sizeof(soc_rev), "ES%s", cpu_rev);
 }
 
 void __init omap4xxx_check_revision(void)
@@ -594,9 +594,9 @@ void __init omap4xxx_check_revision(void)
 		omap_revision = OMAP4430_REV_ES2_3;
 	}
 
-	sprintf(soc_name, "OMAP%04x", omap_rev() >> 16);
-	sprintf(soc_rev, "ES%d.%d", (omap_rev() >> 12) & 0xf,
-						(omap_rev() >> 8) & 0xf);
+	snprintf(soc_name, sizeof(soc_name), "OMAP%04x", omap_rev() >> 16);
+	snprintf(soc_rev, sizeof(soc_rev), "ES%d.%d", (omap_rev() >> 12) & 0xf,
+		 (omap_rev() >> 8) & 0xf);
 	pr_info("%s %s\n", soc_name, soc_rev);
 }
 
@@ -637,8 +637,8 @@ void __init omap5xxx_check_revision(void)
 		omap_revision = OMAP5430_REV_ES2_0;
 	}
 
-	sprintf(soc_name, "OMAP%04x", omap_rev() >> 16);
-	sprintf(soc_rev, "ES%d.0", (omap_rev() >> 12) & 0xf);
+	snprintf(soc_name, sizeof(soc_name), "OMAP%04x", omap_rev() >> 16);
+	snprintf(soc_rev, sizeof(soc_rev), "ES%d.0", (omap_rev() >> 12) & 0xf);
 
 	pr_info("%s %s\n", soc_name, soc_rev);
 }
@@ -712,9 +712,9 @@ void __init dra7xxx_check_revision(void)
 		omap_revision = DRA752_REV_ES2_0;
 	}
 
-	sprintf(soc_name, "DRA%03x", omap_rev() >> 16);
-	sprintf(soc_rev, "ES%d.%d", (omap_rev() >> 12) & 0xf,
-		(omap_rev() >> 8) & 0xf);
+	snprintf(soc_name, sizeof(soc_name), "DRA%03x", omap_rev() >> 16);
+	snprintf(soc_rev, sizeof(soc_rev), "ES%d.%d", (omap_rev() >> 12) & 0xf,
+		 (omap_rev() >> 8) & 0xf);
 
 	pr_info("%s %s\n", soc_name, soc_rev);
 }
#1 lore raw

PATCH 2/4 ARM: OMAP2: use sysfs_emit() in type_show

Replace unbounded sprintf() with sysfs_emit() in type_show().

---
 arch/arm/mach-omap2/id.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm/mach-omap2/id.c +2 -1
index 44d7471666a8..25ded74e4b01 100644
@@ -18,6 +18,7 @@
 #include <linux/random.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/sysfs.h>
 
 #ifdef CONFIG_SOC_BUS
 #include <linux/sys_soc.h>
@@ -771,7 +772,7 @@ static const char * __init omap_get_family(void)
 static ssize_t
 type_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%s\n", omap_types[omap_type()]);
+	return sysfs_emit(buf, "%s\n", omap_types[omap_type()]);
 }
 
 static DEVICE_ATTR_RO(type);
#2 lore raw

PATCH 3/4 ARM: OMAP2: clean up string copying and formatting in omap3_cpuinfo

In reply to Thorsten Blum

Replace scnprintf("%s") with the faster and more direct strscpy().

Remove unnecessary and inconsistent 'n' arithmetic when 'n = 0', and when 'n' is no longer used at the end of the function.

---
 arch/arm/mach-omap2/id.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/mach-omap2/id.c +2 -2
index 25ded74e4b01..126ffd87f572 100644
@@ -255,7 +255,7 @@ static void __init omap3_cpuinfo(void)
 	strscpy(soc_name, cpu_name);
 
 	/* Print verbose information */
-	n += scnprintf(buf, sizeof(buf) - n, "%s %s (", soc_name, soc_rev);
+	n += scnprintf(buf, sizeof(buf), "%s %s (", soc_name, soc_rev);
 
 	OMAP3_SHOW_FEATURE(l2cache);
 	OMAP3_SHOW_FEATURE(iva);
@@ -265,7 +265,7 @@ static void __init omap3_cpuinfo(void)
 	OMAP3_SHOW_FEATURE(192mhz_clk);
 	if (*(buf + n - 1) == ' ')
 		n--;
-	n += scnprintf(buf + n, sizeof(buf) - n, ")\n");
+	scnprintf(buf + n, sizeof(buf) - n, ")\n");
 	pr_info("%s", buf);
 }
 
#3 lore raw

PATCH 4/4 ARM: OMAP2: zero-initialize local buffer in omap3_cpuinfo

In reply to Thorsten Blum

Remove memset(0) and zero-initialize the local buffer instead.

---
 arch/arm/mach-omap2/id.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/arm/mach-omap2/id.c +1 -3
index 126ffd87f572..9a10e96e65e3 100644
@@ -209,11 +209,9 @@ void __init omap2xxx_check_revision(void)
 static void __init omap3_cpuinfo(void)
 {
 	const char *cpu_name;
-	char buf[64];
+	char buf[64] = { 0 };
 	int n = 0;
 
-	memset(buf, 0, sizeof(buf));
-
 	/*
 	 * OMAP3430 and OMAP3530 are assumed to be same.
 	 *