]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-test: istream-test - Don't modify existing snapshots' memareas
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Sun, 10 Dec 2017 18:00:21 +0000 (20:00 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 11 Dec 2017 13:31:01 +0000 (15:31 +0200)
At least with --enable-devel-checks this caused istream-sized unit test to
randomly panic:

Panic: file istream.c: line 279 (i_stream_read): assertion failed: (memcmp(prev_buf, prev_data + prev_skip, prev_pos - prev_skip) == 0)

src/lib-test/test-istream.c

index 84f06b1b652ccc9af747ab258ba6f6c88c9203b8..1c3efcf58a69077a1773cc108c10340c92d32600 100644 (file)
@@ -52,9 +52,15 @@ static ssize_t test_read(struct istream_private *stream)
                    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 && cur_max > 0) {
+               /* Reallocate the memory area if needed. Use exactly correct
+                  buffer size so valgrind can catch read overflows. If a
+                  correctly sized memarea already exists, use it only if
+                  its refcount is 1. Otherwise with refcount>1 we could be
+                  moving data within an existing memarea, which breaks
+                  snapshots. */
+               if (cur_max > 0 && (stream->buffer_size != cur_max ||
+                                   stream->memarea == NULL ||
+                                   memarea_get_refcount(stream->memarea) > 1)) {
                        void *old_w_buffer = stream->w_buffer;
                        stream->w_buffer = i_malloc(cur_max);
                        memcpy(stream->w_buffer, old_w_buffer,