dovecot-2.2: lib-test: Improved test-istream to catch more bugs

dovecot at dovecot.org dovecot at dovecot.org
Thu Jun 19 09:39:23 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/627f2a2ba362
changeset: 17507:627f2a2ba362
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Jun 19 12:38:11 2014 +0300
description:
lib-test: Improved test-istream to catch more bugs

diffstat:

 src/lib-test/test-common.c |  29 +++++++++++++++++++++++------
 1 files changed, 23 insertions(+), 6 deletions(-)

diffs (68 lines):

diff -r c65b4c41698c -r 627f2a2ba362 src/lib-test/test-common.c
--- a/src/lib-test/test-common.c	Thu Jun 19 12:32:45 2014 +0300
+++ b/src/lib-test/test-common.c	Thu Jun 19 12:38:11 2014 +0300
@@ -16,6 +16,7 @@
 
 struct test_istream {
 	struct istream_private istream;
+	const void *orig_buffer;
 	unsigned int skip_diff;
 	size_t max_pos;
 	bool allow_eof;
@@ -25,6 +26,7 @@
 {
 	struct test_istream *tstream = (struct test_istream *)stream;
 	unsigned int new_skip_diff;
+	size_t cur_max;
 	ssize_t ret;
 
 	i_assert(stream->skip <= stream->pos);
@@ -36,10 +38,9 @@
 		/* we seeked past the end of file. */
 		ret = 0;
 	} else {
-		/* move around the buffer */
+		/* copy data to a buffer in somewhat random place. this could
+		   help catch bugs. */
 		new_skip_diff = rand() % 128;
-		stream->buffer = (stream->buffer + tstream->skip_diff) -
-			new_skip_diff;
 		stream->skip = (stream->skip - tstream->skip_diff) +
 			new_skip_diff;
 		stream->pos = (stream->pos - tstream->skip_diff) +
@@ -48,8 +49,24 @@
 			new_skip_diff;
 		tstream->skip_diff = new_skip_diff;
 
-		ret = tstream->max_pos - stream->pos;
-		stream->pos = tstream->max_pos;
+		cur_max = tstream->max_pos;
+		if (stream->max_buffer_size < (size_t)-1 - stream->skip &&
+		    cur_max > stream->skip + stream->max_buffer_size)
+			cur_max = stream->skip + stream->max_buffer_size;
+
+		/* use exactly correct buffer size so valgrind can catch
+		   read overflows */
+		if (stream->buffer_size != cur_max) {
+			stream->w_buffer = i_realloc(stream->w_buffer, 0,
+						     cur_max);
+			stream->buffer = stream->w_buffer;
+			stream->buffer_size = cur_max;
+		}
+		memcpy(stream->w_buffer + new_skip_diff, tstream->orig_buffer,
+		       cur_max - new_skip_diff);
+
+		ret = cur_max - stream->pos;
+		stream->pos = cur_max;
 	}
 
 	if (ret > 0)
@@ -78,7 +95,7 @@
 	struct test_istream *tstream;
 
 	tstream = i_new(struct test_istream, 1);
-	tstream->istream.buffer = data;
+	tstream->orig_buffer = data;
 
 	tstream->istream.read = test_read;
 	tstream->istream.seek = test_seek;


More information about the dovecot-cvs mailing list