Commit 046a37bd authored by Sonny Rao's avatar Sonny Rao Committed by Wolfgang Denk

Add safe vsnprintf and snprintf library functions

From: Sonny Rao <sonnyrao@chromium.org>

These functions are useful in U-Boot because they allow a graceful failure
rather than an unpredictable stack overflow when printf() buffers are
exceeded.

Mostly copied from the Linux kernel. I copied vscnprintf and
scnprintf so we can change printf and vprintf to use the safe
implementation but still return the correct values.

(Simon Glass <sjg@chromium.org> modified this commit a little)
Signed-off-by: default avatarSonny Rao <sonnyrao@chromium.org>
parent 9785c905
......@@ -655,6 +655,15 @@ The following options need to be configured:
to get the character out. Baud rates will need to default
to something sensible.
- Safe printf() functions
Define CONFIG_SYS_VSNPRINTF to compile in safe versions of
the printf() functions. These are defined in
include/vsprintf.h and include snprintf(), vsnprintf() and
so on. Code size increase is approximately 300-500 bytes.
If this option is not given then these functions will
silently discard their buffer size argument - this means
you are not getting any overflow checking in this case.
- Boot Delay: CONFIG_BOOTDELAY - in seconds
Delay before automatically booting the default image;
set to -1 to disable autoboot.
......
......@@ -36,4 +36,23 @@ int sprintf(char *buf, const char *fmt, ...)
int vsprintf(char *buf, const char *fmt, va_list args);
char *simple_itoa(ulong i);
#ifdef CONFIG_SYS_VSNPRINTF
int snprintf(char *buf, size_t size, const char *fmt, ...)
__attribute__ ((format (__printf__, 3, 4)));
int scnprintf(char *buf, size_t size, const char *fmt, ...)
__attribute__ ((format (__printf__, 3, 4)));
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
#else
/*
* Use macros to silently drop the size parameter. Note that the 'cn'
* versions are the same as the 'n' versions since the functions assume
* there is always enough buffer space when !CONFIG_SYS_VSNPRINTF
*/
#define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
#define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
#define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
#define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
#endif /* CONFIG_SYS_VSNPRINTF */
#endif
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment