[U-Boot] [PATCH 3/4] Make printf and vprintf safe from buffer overruns

Simon Glass
2011-09-23T17:38:51Z

From: Sonny Rao <sonnyrao@chromium.org>

From: Sonny Rao <sonnyrao@chromium.org>

utilize the added vscnprintf functions to avoid buffer overruns
The implementation is fairly dumb in that it doesn't detect
that the buffer is too small, but at least will not cause crashes.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
common/console.c | 10 +++++-----
1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/common/console.c b/common/console.c
index 8c650e0..6057e9a 100644
--- a/common/console.c
+++ b/common/console.c
@@ -212,7 +212,7 @@ int serial_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 = vscnprintf(printbuffer, CONFIG_SYS_PBSIZE, fmt, args);
va_end(args);

serial_puts(printbuffer);
@@ -281,7 +281,7 @@ int fprintf(int file, const char *fmt, ...)
/* For this to work, printbuffer must be larger than
* anything we ever want to print.
*/
- i = vsprintf(printbuffer, fmt, args);
+ i = vscnprintf(printbuffer, CONFIG_SYS_PBSIZE, fmt, args);
va_end(args);

/* Send to desired file */
@@ -376,7 +376,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 = vscnprintf(printbuffer, CONFIG_SYS_PBSIZE, fmt, args);
va_end(args);

/* Print the string */
@@ -392,7 +392,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 = vscnprintf(printbuffer, CONFIG_SYS_PBSIZE, fmt, args);

/* Print the string */
puts(printbuffer);
@@ -459,7 +459,7 @@ inline void dbg(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, CONFIG_SYS_PBSIZE, fmt, args);
va_end(args);

if ((screen + sizeof(screen) - 1 - cursor)
--
1.7.3.1