* [PATCH] common: console_common: Replace vsprintf with vsnprintf
@ 2021-04-17 21:11 Jules Maselbas
2021-05-12 5:35 ` Sascha Hauer
0 siblings, 1 reply; 3+ messages in thread
From: Jules Maselbas @ 2021-04-17 21:11 UTC (permalink / raw)
To: barebox; +Cc: Neeraj Pal, Jules Maselbas
Replace the *sprintf to their *snprintf version when printing
to the fixed size printbuffer.
Reported-by: Neeraj Pal <neerajpal09@gmail.com>
Signed-off-by: Jules Maselbas <jmaselbas@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
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] common: console_common: Replace vsprintf with vsnprintf
2021-04-17 21:11 [PATCH] common: console_common: Replace vsprintf with vsnprintf Jules Maselbas
@ 2021-05-12 5:35 ` Sascha Hauer
2021-05-12 7:17 ` Jules Maselbas
0 siblings, 1 reply; 3+ messages in thread
From: Sascha Hauer @ 2021-05-12 5:35 UTC (permalink / raw)
To: Jules Maselbas; +Cc: barebox, Neeraj Pal
Hi Jules,
On Sat, Apr 17, 2021 at 11:11:44PM +0200, Jules Maselbas wrote:
> Replace the *sprintf to their *snprintf version when printing
> to the fixed size printbuffer.
>
> Reported-by: Neeraj Pal <neerajpal09@gmail.com>
> Signed-off-by: Jules Maselbas <jmaselbas@kalray.eu>
> ---
I didn't realize you also created this patch. I like your version better
because of the sizeof() you used. Replaced my version with yours, but I
added the pbl/console.c changes from my patch to yours.
Sascha
> 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
>
>
>
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
>
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] common: console_common: Replace vsprintf with vsnprintf
2021-05-12 5:35 ` Sascha Hauer
@ 2021-05-12 7:17 ` Jules Maselbas
0 siblings, 0 replies; 3+ messages in thread
From: Jules Maselbas @ 2021-05-12 7:17 UTC (permalink / raw)
To: Sascha Hauer; +Cc: barebox, Neeraj Pal
Hi Sascha,
On Wed, May 12, 2021 at 07:35:15AM +0200, Sascha Hauer wrote:
> Hi Jules,
>
> I didn't realize you also created this patch. I like your version better
> because of the sizeof() you used. Replaced my version with yours, but I
> added the pbl/console.c changes from my patch to yours.
Ok cool
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-05-12 7:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-17 21:11 [PATCH] common: console_common: Replace vsprintf with vsnprintf Jules Maselbas
2021-05-12 5:35 ` Sascha Hauer
2021-05-12 7:17 ` Jules Maselbas
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox