]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add i_stream_memarea_detach()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 4 Oct 2021 22:00:38 +0000 (01:00 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Fri, 8 Oct 2021 13:14:15 +0000 (13:14 +0000)
src/lib/istream-private.h
src/lib/istream.c

index 7666bee5a5025c737bb61594cfa278c392fb6727..afabe2359943e14e91aa06b56844235fa2fdc566 100644 (file)
@@ -101,6 +101,10 @@ bool ATTR_NOWARN_UNUSED_RESULT
 i_stream_try_alloc_avoid_compress(struct istream_private *stream,
                                  size_t wanted_size, size_t *size_r);
 void *i_stream_alloc(struct istream_private *stream, size_t size);
+/* Detach istream from its current memarea. This unreferences the memarea and
+   resets the w_buffer to empty. This can be used to make sure i_stream_*alloc()
+   won't return a pointer to memory referenced to in a snapshot. */
+void i_stream_memarea_detach(struct istream_private *stream);
 /* Free memory allocated by i_stream_*alloc() */
 void i_stream_free_buffer(struct istream_private *stream);
 ssize_t i_stream_read_copy_from_parent(struct istream *istream);
index eef54b94886518eb63dd93752017270912eae35c..c63fa2b5e13c6ce27651fbaf945f2f2fda4d35ae 100644 (file)
@@ -956,6 +956,18 @@ void *i_stream_alloc(struct istream_private *stream, size_t size)
        return stream->w_buffer + stream->pos;
 }
 
+void i_stream_memarea_detach(struct istream_private *stream)
+{
+       if (stream->memarea != NULL) {
+               /* Don't overwrite data in a snapshot. Allocate a new
+                  buffer instead. */
+               memarea_unref(&stream->memarea);
+               stream->buffer_size = 0;
+               stream->buffer = NULL;
+               stream->w_buffer = NULL;
+       }
+}
+
 bool i_stream_add_data(struct istream *_stream, const unsigned char *data,
                       size_t size)
 {