From 9a9a6365d5c5abb0fe3ec6cc09542e9c7e1d3bec Mon Sep 17 00:00:00 2001 From: Jones Desougi Date: Thu, 10 Jun 2010 15:04:51 +0200 Subject: *printf: Violation of precision with null string When a string format is processed and the argument is NULL, this yields "(null)" regardless of precision. This does not make sense, precision should not be exceeded. A simple test shows that glibc outputs nothing if precision is smaller than six and the attached patch implements this same behaviour. Consider the not uncommon case of strings implemented like this: struct string { int len; char *ptr; }; There is often no nultermination and they may be printed like this: printf("%.*s", string.len, string.ptr); If len is 0 then ptr may be anything, but NULL is a common value. Obviously the empty string would be expected, not "(null)". Signed-off-by: Jones Desougi Signed-off-by: Bernhard Reutner-Fischer --- libc/stdio/_vfprintf.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'libc/stdio') diff --git a/libc/stdio/_vfprintf.c b/libc/stdio/_vfprintf.c index 3b007084d..fa5dc44fc 100644 --- a/libc/stdio/_vfprintf.c +++ b/libc/stdio/_vfprintf.c @@ -1670,6 +1670,9 @@ static int _do_one_spec(FILE * __restrict stream, #endif s = "(null)"; slen = 6; + /* Use an empty string rather than truncation if precision is too small. */ + if (ppfs->info.prec >= 0 && ppfs->info.prec < slen) + slen = 0; } } else { /* char */ s = buf; @@ -1726,6 +1729,9 @@ static int _do_one_spec(FILE * __restrict stream, NULL_STRING: s = "(null)"; SLEN = slen = 6; + /* Use an empty string rather than truncation if precision is too small. */ + if (ppfs->info.prec >= 0 && ppfs->info.prec < slen) + SLEN = slen = 0; } } else { /* char */ *wbuf = btowc( (unsigned char)(*((const int *) *argptr)) ); -- cgit v1.2.3 From 8116ca7baec0ff8c9bc35a2920aa36549e6004a4 Mon Sep 17 00:00:00 2001 From: "Peter S. Mazinger" Date: Wed, 2 Mar 2011 15:31:00 +0100 Subject: make parse_printf_format() depend on UCLIBC_HAS_GLIBC_CUSTOM_PRINTF we already remove the printf.h header if this option is disabled Signed-off-by: Peter S. Mazinger --- libc/stdio/_vfprintf.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'libc/stdio') diff --git a/libc/stdio/_vfprintf.c b/libc/stdio/_vfprintf.c index fa5dc44fc..3db8cdf67 100644 --- a/libc/stdio/_vfprintf.c +++ b/libc/stdio/_vfprintf.c @@ -417,6 +417,8 @@ extern uintmax_t _load_inttype(int desttype, const void *src, int uflag) attribu /**********************************************************************/ #ifdef L_parse_printf_format +#ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ + /* NOTE: This function differs from the glibc version in that parsing stops * upon encountering an invalid conversion specifier. Since this is the way * my printf functions work, I think it makes sense to do it that way here. @@ -484,6 +486,8 @@ size_t parse_printf_format(register const char *template, return count; } +#endif + #endif /**********************************************************************/ #ifdef L__ppfs_init -- cgit v1.2.3 From abd6c6c29f82ddcc7c86c67519b79d2381622ed9 Mon Sep 17 00:00:00 2001 From: "Peter S. Mazinger" Date: Wed, 9 Mar 2011 23:20:16 +0100 Subject: remove unused hidden functions Signed-off-by: Peter S. Mazinger --- libc/stdio/_scanf.c | 1 - libc/stdio/fwprintf.c | 1 - 2 files changed, 2 deletions(-) (limited to 'libc/stdio') diff --git a/libc/stdio/_scanf.c b/libc/stdio/_scanf.c index 1ad81cbe3..f38e72b5c 100644 --- a/libc/stdio/_scanf.c +++ b/libc/stdio/_scanf.c @@ -198,7 +198,6 @@ int vscanf(const char * __restrict format, va_list arg) { return vfscanf(stdin, format, arg); } -libc_hidden_def(vscanf) #endif /**********************************************************************/ diff --git a/libc/stdio/fwprintf.c b/libc/stdio/fwprintf.c index 2f2dddc10..954970867 100644 --- a/libc/stdio/fwprintf.c +++ b/libc/stdio/fwprintf.c @@ -21,4 +21,3 @@ int fwprintf(FILE * __restrict stream, const wchar_t * __restrict format, ...) return rv; } -libc_hidden_def(fwprintf) -- cgit v1.2.3