From 046a37bd53f479915bcd5041e0834dad576371a2 Mon Sep 17 00:00:00 2001 From: Sonny Rao Date: Wed, 2 Nov 2011 09:52:08 +0000 Subject: Add safe vsnprintf and snprintf library functions From: Sonny Rao 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 modified this commit a little) Signed-off-by: Sonny Rao --- include/vsprintf.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'include/vsprintf.h') diff --git a/include/vsprintf.h b/include/vsprintf.h index 065144669f..5195598f97 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -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 -- cgit v1.2.1