]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Call printf_string_fix_format() only when printf_string_upper_bound() sees
authorTimo Sirainen <tss@iki.fi>
Mon, 8 Sep 2003 01:29:07 +0000 (04:29 +0300)
committerTimo Sirainen <tss@iki.fi>
Mon, 8 Sep 2003 01:29:07 +0000 (04:29 +0300)
%m.

--HG--
branch : HEAD

src/lib/failures.c
src/lib/printf-upper-bound.c
src/lib/printf-upper-bound.h
src/lib/str.c
src/lib/strfuncs.c

index 7f7882b1bbba60b8ebcfd957a983d6485455df07..64d81bb856e3319451eebdeabb7a42f2d230bd2f 100644 (file)
@@ -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--;
index 33eb771fb35803c954aa6ad303179c449cd45db8..bde012d21d266fe87ff4185ef5b39a83c3ed1165 100644 (file)
@@ -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;
 }
index b6611de6c6225bb5c8f9d0e75f86cc96f6621cc1..57d0235afa84d6c6c5548111a7cb1c410b28d067 100644 (file)
@@ -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
index 83b68bb255904146796d6f8ebf4f19465d810267..a5e16ce51fb461d15d4500f70579c1d6c0eefc82 100644 (file)
@@ -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
index 02ce0657a9d278279dfaee62541a27965cd9811a..357f93e01e1afb8bf09cd0c2f1931ce2805982fc 100644 (file)
@@ -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