From: Timo Sirainen Date: Mon, 8 Sep 2003 01:29:07 +0000 (+0300) Subject: Call printf_string_fix_format() only when printf_string_upper_bound() sees X-Git-Tag: 1.1.alpha1~4343 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc34e919907845ce01ad04aa8213033596a1ae70;p=thirdparty%2Fdovecot%2Fcore.git Call printf_string_fix_format() only when printf_string_upper_bound() sees %m. --HG-- branch : HEAD --- diff --git a/src/lib/failures.c b/src/lib/failures.c index 7f7882b1bb..64d81bb856 100644 --- a/src/lib/failures.c +++ b/src/lib/failures.c @@ -84,20 +84,17 @@ static int default_handler(const char *prefix, FILE *f, t_push(); if (recursed == 2) { - /* write without fixing format, that probably killed us - last time. */ + /* printf_string_upper_bound() probably killed us last time, + just write the format now. */ - /* make sure there's no %n in there */ - (void)printf_string_upper_bound(format, args); - vfprintf(f, format, args2); - fputs(" - recursed!", f); + fputs("recursed: ", f); + fputs(format, f); } else { write_prefix(f); fputs(prefix, f); - format = printf_string_fix_format(format); - /* make sure there's no %n in there */ - (void)printf_string_upper_bound(format, args); + /* make sure there's no %n in there and fix %m */ + (void)printf_string_upper_bound(&format, args); vfprintf(f, format, args2); } @@ -268,7 +265,7 @@ static int syslog_handler(int level, const char *format, va_list args) /* make sure there's no %n in there */ VA_COPY(args2, args); - (void)printf_string_upper_bound(format, args); + (void)printf_string_upper_bound(&format, args); vsyslog(level, format, args2); recursed--; diff --git a/src/lib/printf-upper-bound.c b/src/lib/printf-upper-bound.c index 33eb771fb3..bde012d21d 100644 --- a/src/lib/printf-upper-bound.c +++ b/src/lib/printf-upper-bound.c @@ -66,9 +66,11 @@ typedef struct # define HONOUR_LONGS 0 #endif -size_t printf_string_upper_bound(const char *format, va_list args) +size_t printf_string_upper_bound(const char **format_p, va_list args) { + const char *format = *format_p; size_t len = 1; + int fix_format = FALSE; if (!format) return len; @@ -296,10 +298,9 @@ size_t printf_string_upper_bound(const char *format, va_list args) (void) va_arg (args, void*); break; case 'm': - /* normally we shouldn't even get here, but we could be just - checking the format string is valid before giving the - format to vsyslog(). */ + /* %m, replace it with strerror() later */ conv_len += strlen(strerror(errno)) + 256; + fix_format = TRUE; break; /* handle invalid cases @@ -325,5 +326,7 @@ size_t printf_string_upper_bound(const char *format, va_list args) } /* else (c == '%') */ } /* while (*format) */ + if (fix_format) + *format_p = printf_string_fix_format(*format_p); return len; } diff --git a/src/lib/printf-upper-bound.h b/src/lib/printf-upper-bound.h index b6611de6c6..57d0235afa 100644 --- a/src/lib/printf-upper-bound.h +++ b/src/lib/printf-upper-bound.h @@ -2,7 +2,9 @@ #define __PRINTF_UPPER_BOUND_H /* Returns the maximum length of given format string when expanded. - If the format is invalid, i_fatal() is called. */ -size_t printf_string_upper_bound(const char *format, va_list args); + If the format is invalid, i_fatal() is called. + + If format contains %m, it's replaced with the real error message. */ +size_t printf_string_upper_bound(const char **format, va_list args); #endif diff --git a/src/lib/str.c b/src/lib/str.c index 83b68bb255..a5e16ce51f 100644 --- a/src/lib/str.c +++ b/src/lib/str.c @@ -119,9 +119,7 @@ void str_vprintfa(string_t *str, const char *fmt, va_list args) len = buffer_get_used_size(str); - fmt = printf_string_fix_format(fmt); - append_len = printf_string_upper_bound(fmt, args); - + append_len = printf_string_upper_bound(&fmt, args); buf = buffer_append_space_unsafe(str, append_len); #ifdef HAVE_VSNPRINTF diff --git a/src/lib/strfuncs.c b/src/lib/strfuncs.c index 02ce0657a9..357f93e01e 100644 --- a/src/lib/strfuncs.c +++ b/src/lib/strfuncs.c @@ -85,8 +85,7 @@ int i_snprintf(char *dest, size_t max_chars, const char *format, ...) va_start(args, format); VA_COPY(args2, args); - format = printf_string_fix_format(format); - len = printf_string_upper_bound(format, args); + len = printf_string_upper_bound(&format, args); i_assert(len >= 0); @@ -203,9 +202,7 @@ char *p_strdup_vprintf(pool_t pool, const char *format, va_list args) VA_COPY(args2, args); - format = printf_string_fix_format(format); - - len = printf_string_upper_bound(format, args); + len = printf_string_upper_bound(&format, args); ret = p_malloc(pool, len); #ifdef HAVE_VSNPRINTF