[dovecot-cvs] dovecot/src/lib compat.c,1.7,1.8 data-stack.c,1.11,1.12 failures.c,1.10,1.11 str.c,1.2,1.3 strfuncs.c,1.20,1.21

cras at procontrol.fi cras at procontrol.fi
Sun Dec 22 08:25:52 EET 2002


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv26917/lib

Modified Files:
	compat.c data-stack.c failures.c str.c strfuncs.c 
Log Message:
Use vsnprintf() always when possible, even if we went through the
upper_bound function. DEBUG message in data stack might have sometimes
caused an infinite loop. Made sure infinite loops don't happen anymore with
failure handlers. str_printfa() didn't handle %m. Made my_vsyslog() a lot
simpler, since t_strdup_printf() is now safe enough to call in failure
handler.



Index: compat.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/compat.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- compat.c	18 Dec 2002 15:15:41 -0000	1.7
+++ compat.c	22 Dec 2002 06:25:50 -0000	1.8
@@ -72,30 +72,7 @@
 #ifndef HAVE_VSYSLOG
 void my_vsyslog(int priority, const char *format, va_list args)
 {
-	const char *str;
-	char buf[1024];
-
-#ifdef HAVE_VSNPRINTF
-	int ret;
-
-	ret = vsnprintf(buf, sizeof(buf), format, args);
-	if (ret < 0 || (size_t)ret >= sizeof(buf))
-		buf[sizeof(buf)-1] = '\0';
-	str = buf;
-#else
-        va_list args2;
-
-	VA_COPY(args2, args);
-
-	if (printf_string_upper_bound(format, args) < sizeof(buf)) {
-		vsprintf(buf, format, args);
-		str = buf;
-	} else {
-		/* this may not be safe but not choice really.. */
-		str = t_strdup_vprintf(format, args2);
-	}
-#endif
-	syslog(priority, "%s", str);
+	syslog(priority, "%s", t_strdup_vprintf(format, args));
 }
 #endif
 

Index: data-stack.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/data-stack.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- data-stack.c	18 Dec 2002 15:15:41 -0000	1.11
+++ data-stack.c	22 Dec 2002 06:25:50 -0000	1.12
@@ -198,6 +198,9 @@
 {
 	StackBlock *block;
         void *ret;
+#ifdef DEBUG
+	int warn = FALSE;
+#endif
 
 	if (size == 0)
 		return NULL;
@@ -233,7 +236,7 @@
 	} else {
 		block = mem_block_alloc(size);
 #ifdef DEBUG
-		i_warning("Growing data stack with: %"PRIuSIZE_T, block->size);
+		warn = TRUE;
 #endif
 	}
 
@@ -245,7 +248,16 @@
 	current_block->next = block;
 	current_block = block;
 
-        return STACK_BLOCK_DATA(current_block);
+	ret = STACK_BLOCK_DATA(current_block);
+#ifdef DEBUG
+	if (warn) {
+		/* warn later, so that if i_warning() wants to allocate more
+		   memory we don't go to infinite loop */
+		i_warning("Growing data stack with: %"PRIuSIZE_T, block->size);
+	}
+#endif
+
+        return ret;
 }
 
 void *t_malloc(size_t size)

Index: failures.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/failures.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- failures.c	21 Dec 2002 22:28:07 -0000	1.10
+++ failures.c	22 Dec 2002 06:25:50 -0000	1.11
@@ -78,26 +78,56 @@
 	}
 }
 
-static void default_panic_handler(const char *format, va_list args)
+static void default_handler(const char *prefix, const char *format,
+			    va_list args)
 {
-	if (log_fd == NULL) log_fd = stderr;
-	write_prefix(log_fd);
+	static int recursed = 0;
+	int old_errno = errno;
+
+	if (recursed == 2) {
+		/* we're being called from some signal handler, or
+		   printf_string_upper_bound() killed us again */
+		return;
+	}
+
+	recursed++;
+
+	if (log_fd == NULL)
+		log_fd = stderr;
+
+	if (recursed == 2) {
+		/* write without fixing format, that probably killed us
+		   last time. */
+
+		/* make sure there's no %n in there */
+                (void)printf_string_upper_bound(format, args);
+		vfprintf(log_fd, format, args);
+		fputs(" - recursed!", log_fd);
+	} else {
+		write_prefix(log_fd);
+
+		fputs(prefix, log_fd);
+		format = printf_string_fix_format(format);
+		/* make sure there's no %n in there */
+                (void)printf_string_upper_bound(format, args);
+		vfprintf(log_fd, format, args);
+	}
 
-	fputs("Panic: ", log_fd);
-	vfprintf(log_fd, printf_string_fix_format(format), args);
 	fputc('\n', log_fd);
 
+	errno = old_errno;
+	recursed--;
+}
+
+static void default_panic_handler(const char *format, va_list args)
+{
+	default_handler("Panic: ", format, args);
 	abort();
 }
 
 static void default_fatal_handler(int status, const char *format, va_list args)
 {
-	if (log_fd == NULL) log_fd = stderr;
-	write_prefix(log_fd);
-
-	fputs("Fatal: ", log_fd);
-	vfprintf(log_fd, printf_string_fix_format(format), args);
-	fputc('\n', log_fd);
+	default_handler("Fatal: ", format, args);
 
 	if (fflush(log_fd) < 0 && status == FATAL_DEFAULT)
 		status = FATAL_LOGWRITE;
@@ -109,14 +139,7 @@
 {
 	int old_errno = errno;
 
-	if (log_fd == NULL) log_fd = stderr;
-	write_prefix(log_fd);
-
-	t_push();
-	fputs("Error: ", log_fd);
-	vfprintf(log_fd, printf_string_fix_format(format), args);
-        fputc('\n', log_fd);
-	t_pop();
+	default_handler("Error: ", format, args);
 
 	if (fflush(log_fd) < 0)
 		exit(FATAL_LOGWRITE);
@@ -128,14 +151,7 @@
 {
 	int old_errno = errno;
 
-	if (log_fd == NULL) log_fd = stderr;
-	write_prefix(log_fd);
-
-	t_push();
-	fputs("Warning: ", log_fd);
-	vfprintf(log_fd, printf_string_fix_format(format), args);
-	fputc('\n', log_fd);
-	t_pop();
+	default_handler("Warning: ", format, args);
 
 	if (fflush(log_fd) < 0)
 		exit(FATAL_LOGWRITE);
@@ -147,15 +163,9 @@
 {
 	int old_errno = errno;
 
-	if (log_info_fd == NULL) log_info_fd = stderr;
-	write_prefix(log_info_fd);
-
-	t_push();
-	vfprintf(log_info_fd, printf_string_fix_format(format), args);
-	fputc('\n', log_info_fd);
-	t_pop();
+	default_handler("Info: ", format, args);
 
-	if (fflush(log_info_fd) < 0)
+	if (fflush(log_fd) < 0)
 		exit(FATAL_LOGWRITE);
 
 	errno = old_errno;
@@ -250,31 +260,47 @@
         info_handler = func;
 }
 
+static void syslog_handler(int level, const char *format, va_list args)
+{
+	static int recursed = 0;
+
+	if (recursed != 0)
+		return;
+
+	recursed++;
+
+	/* make sure there's no %n in there */
+	(void)printf_string_upper_bound(format, args);
+
+	vsyslog(level, format, args);
+	recursed--;
+}
+
 void i_syslog_panic_handler(const char *fmt, va_list args)
 {
-	vsyslog(LOG_CRIT, fmt, args);
+	syslog_handler(LOG_CRIT, fmt, args);
         abort();
 }
 
 void i_syslog_fatal_handler(int status, const char *fmt, va_list args)
 {
-	vsyslog(LOG_CRIT, fmt, args);
+	syslog_handler(LOG_CRIT, fmt, args);
 	exit(status);
 }
 
 void i_syslog_error_handler(const char *fmt, va_list args)
 {
-	vsyslog(LOG_ERR, fmt, args);
+	syslog_handler(LOG_ERR, fmt, args);
 }
 
 void i_syslog_warning_handler(const char *fmt, va_list args)
 {
-	vsyslog(LOG_WARNING, fmt, args);
+	syslog_handler(LOG_WARNING, fmt, args);
 }
 
 void i_syslog_info_handler(const char *fmt, va_list args)
 {
-	vsyslog(LOG_INFO, fmt, args);
+	syslog_handler(LOG_INFO, fmt, args);
 }
 
 void i_set_failure_syslog(const char *ident, int options, int facility)

Index: str.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/str.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- str.c	21 Dec 2002 22:04:12 -0000	1.2
+++ str.c	22 Dec 2002 06:25:50 -0000	1.3
@@ -121,12 +121,25 @@
 void str_vprintfa(String *str, const char *fmt, va_list args)
 {
 	char *buf;
-	size_t len;
+	int ret;
+	size_t len, append_len;
 
 	len = buffer_get_used_size(str);
 
-	buf = buffer_append_space(str, printf_string_upper_bound(fmt, args));
-	len += vsprintf(buf, fmt, args);
+	fmt = printf_string_fix_format(fmt);
+	append_len = printf_string_upper_bound(fmt, args);
+
+	buf = buffer_append_space(str, append_len);
+
+#ifdef HAVE_VSNPRINTF
+	ret = vsnprintf(buf, append_len, fmt, args);
+	i_assert(ret >= 0 && (size_t)ret <= append_len);
+#else
+	ret = vsprintf(buf, fmt, args);
+	i_assert(ret >= 0);
+#endif
+
+	len += ret;
 
 	buffer_set_used_size(str, len);
 }

Index: strfuncs.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/strfuncs.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- strfuncs.c	19 Dec 2002 01:02:35 -0000	1.20
+++ strfuncs.c	22 Dec 2002 06:25:50 -0000	1.21
@@ -389,44 +389,30 @@
 
 int i_snprintf(char *dest, size_t max_chars, const char *format, ...)
 {
-#ifdef HAVE_VSNPRINTF
-	va_list args;
-	int ret;
-
-	i_assert(dest != NULL);
-	i_assert(max_chars < INT_MAX);
-	i_assert(format != NULL);
-
-	t_push();
-	va_start(args, format);
-	ret = vsnprintf(dest, max_chars,
-			printf_string_fix_format(format), args);
-	va_end(args);
-	t_pop();
-
-	if (ret < 0 || (size_t)ret >= max_chars) {
-		dest[max_chars-1] = '\0';
-		return -1;
-	}
-
-	return 0;
-#else
+#ifndef HAVE_VSNPRINTF
 	char *buf;
+#endif
 	va_list args;
-        int len, ret;
+	ssize_t len;
+	int ret;
 
-	i_assert(dest != NULL);
 	i_assert(max_chars < INT_MAX);
-	i_assert(format != NULL);
 
 	t_push();
 
 	va_start(args, format);
 	format = printf_string_fix_format(format);
-	buf = t_buffer_get(printf_string_upper_bound(format, args));
+	len = printf_string_upper_bound(format, args);
 	va_end(args);
 
+	i_assert(len >= 0);
+
+#ifdef HAVE_VSNPRINTF
+	len = vsnprintf(dest, max_chars, format, args);
+#else
+	buf = t_buffer_get(len);
 	len = vsprintf(buf, format, args);
+#endif
 	if (len < 0) {
 		/* some error occured */
 		len = 0;
@@ -439,12 +425,13 @@
 		ret = 0;
 	}
 
-        memcpy(dest, buf, len);
+#ifndef HAVE_VSNPRINTF
+	memcpy(dest, buf, len);
+#endif
 	dest[len] = '\0';
 
 	t_pop();
 	return ret;
-#endif
 }
 
 #define STRDUP_CORE(alloc_func, str) STMT_START { \
@@ -608,7 +595,8 @@
 		    ALLOC_FUNC alloc_func, Pool pool)
 {
         va_list temp_args;
-        char *ret;
+	char *ret;
+	size_t len;
 
 	if (format == NULL)
 		return NULL;
@@ -617,8 +605,14 @@
 
 	VA_COPY(temp_args, args);
 
-        ret = alloc_func(pool, printf_string_upper_bound(format, args));
+	len = printf_string_upper_bound(format, args);
+        ret = alloc_func(pool, len);
+
+#ifdef HAVE_VSNPRINTF
+	vsnprintf(ret, len, format, args);
+#else
 	vsprintf(ret, format, args);
+#endif
 
 	va_end(temp_args);
 




More information about the dovecot-cvs mailing list