[dovecot-cvs] dovecot/src/lib-index/mbox mbox-index.c,1.53,1.54 mbox-rewrite.c,1.42,1.43

cras at procontrol.fi cras at procontrol.fi
Sun Dec 8 07:23:10 EET 2002


Update of /home/cvs/dovecot/src/lib-index/mbox
In directory danu:/tmp/cvs-serv19285/lib-index/mbox

Modified Files:
	mbox-index.c mbox-rewrite.c 
Log Message:
Added buffer API. Point is to hide all buffer writing behind this API which
verifies that nothing overflows. Much better than doing the same checks all
around the code, even if it is slightly slower.

Buffer reading is still mostly done directly, that isn't such a big security
risk and I can't think of a reasonable API for it anyway.



Index: mbox-index.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-index.c,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -d -r1.53 -r1.54
--- mbox-index.c	6 Dec 2002 01:09:23 -0000	1.53
+++ mbox-index.c	8 Dec 2002 05:23:08 -0000	1.54
@@ -1,6 +1,7 @@
 /* Copyright (C) 2002 Timo Sirainen */
 
 #include "lib.h"
+#include "buffer.h"
 #include "istream.h"
 #include "rfc822-tokenize.h"
 #include "mbox-index.h"
@@ -13,6 +14,9 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 
+/* Don't try reading more custom flags than this. */
+#define MAX_CUSTOM_FLAGS 1024
+
 extern MailIndex mbox_index;
 
 int mbox_set_syscall_error(MailIndex *index, const char *function)
@@ -189,10 +193,12 @@
 static int mbox_parse_imapbase(const char *value, size_t len,
 			       MboxHeaderContext *ctx)
 {
-	const char **custom_flags, **old_flags;
+	const char **flag;
+	Buffer *buf;
 	size_t pos, start;
 	MailFlags flags;
-	int idx, ret, spaces, max;
+	unsigned int count;
+	int ret, spaces;
 
 	/* skip <uid validity> and <last uid> fields */
 	spaces = 0;
@@ -211,22 +217,16 @@
 	t_push();
 
 	/* we're at the 3rd field now, which begins the list of custom flags */
-	max = MAIL_CUSTOM_FLAGS_COUNT;
-	custom_flags = t_new(const char *, max);
-	for (idx = 0, start = pos; ; pos++) {
+	buf = buffer_create_dynamic(data_stack_pool,
+				    MAIL_CUSTOM_FLAGS_COUNT, MAX_CUSTOM_FLAGS);
+	for (start = pos; ; pos++) {
 		if (pos == len || value[pos] == ' ' || value[pos] == '\t') {
 			if (start != pos) {
-				if (idx == max) {
-					/* need more memory */
-					old_flags = custom_flags;
-					max *= 2;
-					custom_flags = t_new(const char *, max);
-					memcpy(custom_flags, old_flags,
-					       sizeof(const char *) * idx);
-				}
+				flag = buffer_append_space(buf, sizeof(*flag));
+				if (flag == NULL)
+					break;
 
-				custom_flags[idx++] =
-					t_strdup_until(value+start, value+pos);
+				*flag = t_strdup_until(value+start, value+pos);
 			}
 			start = pos+1;
 
@@ -236,8 +236,10 @@
 	}
 
 	flags = MAIL_CUSTOM_FLAGS_MASK;
+	count = buffer_get_used_size(buf) / sizeof(const char *);
+	flag = buffer_free_without_data(buf);
 	ret = mail_custom_flags_fix_list(ctx->index->custom_flags, &flags,
-					 custom_flags, idx);
+					 flag, count);
 
 	t_pop();
 

Index: mbox-rewrite.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mbox/mbox-rewrite.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -d -r1.42 -r1.43
--- mbox-rewrite.c	6 Dec 2002 01:09:23 -0000	1.42
+++ mbox-rewrite.c	8 Dec 2002 05:23:08 -0000	1.43
@@ -201,7 +201,7 @@
 {
 	/* leave only unknown flags, very likely none */
 	char *ret, *p;
-	unsigned int i;
+	size_t i;
 
 	ret = p = t_buffer_get(value_len+1);
 	for (i = 0; i < value_len; i++) {




More information about the dovecot-cvs mailing list