[PATCH] common: console_common: Replace vsprintf with vsnprintf

Jules Maselbas jmaselbas at kalray.eu
Sat Apr 17 22:11:44 BST 2021


Replace the *sprintf to their *snprintf version when printing
to the fixed size printbuffer.

Reported-by: Neeraj Pal <neerajpal09 at gmail.com>
Signed-off-by: Jules Maselbas <jmaselbas at kalray.eu>
---
 common/console_common.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/common/console_common.c b/common/console_common.c
index 3e0741572..dc3a611e1 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -126,7 +126,7 @@ int pr_print(int level, const char *fmt, ...)
 		return 0;
 
 	va_start(args, fmt);
-	i = vsprintf(printbuffer, fmt, args);
+	i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
 	va_end(args);
 
 	pr_puts(level, printbuffer);
@@ -139,18 +139,19 @@ int dev_printf(int level, const struct device_d *dev, const char *format, ...)
 	va_list args;
 	int ret = 0;
 	char printbuffer[CFG_PBSIZE];
+	size_t size = sizeof(printbuffer);
 
 	if (!IS_ENABLED(CONFIG_LOGBUF) && level > barebox_loglevel)
 		return 0;
 
 	if (dev->driver && dev->driver->name)
-		ret += sprintf(printbuffer, "%s ", dev->driver->name);
+		ret += snprintf(printbuffer, size, "%s ", dev->driver->name);
 
-	ret += sprintf(printbuffer + ret, "%s: ", dev_name(dev));
+	ret += snprintf(printbuffer + ret, size - ret, "%s: ", dev_name(dev));
 
 	va_start(args, format);
 
-	ret += vsprintf(printbuffer + ret, format, args);
+	ret += vsnprintf(printbuffer + ret, size - ret, format, args);
 
 	va_end(args);
 
@@ -237,7 +238,7 @@ int printf(const char *fmt, ...)
 	 * For this to work, printbuffer must be larger than
 	 * anything we ever want to print.
 	 */
-	i = vsprintf (printbuffer, fmt, args);
+	i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
 	va_end(args);
 
 	/* Print the string */
@@ -256,7 +257,7 @@ int vprintf(const char *fmt, va_list args)
 	 * For this to work, printbuffer must be larger than
 	 * anything we ever want to print.
 	 */
-	i = vsprintf(printbuffer, fmt, args);
+	i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
 
 	/* Print the string */
 	puts(printbuffer);
@@ -344,7 +345,7 @@ int dprintf(int file, const char *fmt, ...)
 	 * For this to work, printbuffer must be larger than
 	 * anything we ever want to print.
 	 */
-	vsprintf(printbuffer, fmt, args);
+	vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
 	va_end(args);
 
 	/* Print the string */
-- 
2.30.2





More information about the barebox mailing list