]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
zlib-plugin: Check that UID is assigned
authorAki Tuomi <aki.tuomi@dovecot.fi>
Mon, 5 Sep 2016 12:56:05 +0000 (15:56 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 7 Sep 2016 19:04:17 +0000 (22:04 +0300)
When caching, code has to check whether UID is
assigned to avoid reusing cache for different
email since they all have UID = 0 before they
are actually committed.

src/plugins/zlib/zlib-plugin.c

index 7898b88e74581f8adbf000f1decd9e575159ef1f..334541a6a5ace1b99e82833ff1d608ed62283dd5 100644 (file)
@@ -75,13 +75,14 @@ static void zlib_mail_cache_close(struct zlib_user *zuser)
 
 static struct istream *
 zlib_mail_cache_open(struct zlib_user *zuser, struct mail *mail,
-                    struct istream *input)
+                    struct istream *input, bool do_cache)
 {
        struct zlib_mail_cache *cache = &zuser->cache;
        struct istream *inputs[2];
        string_t *temp_prefix = t_str_new(128);
 
-       zlib_mail_cache_close(zuser);
+       if (do_cache)
+               zlib_mail_cache_close(zuser);
 
        /* zlib istream is seekable, but very slow. create a seekable istream
           which we can use to quickly seek around in the stream that's been
@@ -101,15 +102,18 @@ zlib_mail_cache_open(struct zlib_user *zuser, struct mail *mail,
                                                 i_stream_get_name(inputs[0])));
        i_stream_unref(&inputs[0]);
 
-       cache->to = timeout_add(ZLIB_MAIL_CACHE_EXPIRE_MSECS,
-                               zlib_mail_cache_close, zuser);
-       cache->box = mail->box;
-       cache->uid = mail->uid;
-       cache->input = input;
-
-       /* index-mail wants the stream to be destroyed at close, so create
-          a new stream instead of just increasing reference. */
-       return i_stream_create_limit(cache->input, (uoff_t)-1);
+       if (do_cache) {
+               cache->to = timeout_add(ZLIB_MAIL_CACHE_EXPIRE_MSECS,
+                                       zlib_mail_cache_close, zuser);
+               cache->box = mail->box;
+               cache->uid = mail->uid;
+               cache->input = input;
+               /* index-mail wants the stream to be destroyed at close, so create
+                  a new stream instead of just increasing reference. */
+               return i_stream_create_limit(cache->input, (uoff_t)-1);
+       } else {
+               return input;
+       }
 }
 
 static int zlib_istream_opened(struct mail *_mail, struct istream **stream)
@@ -127,7 +131,7 @@ static int zlib_istream_opened(struct mail *_mail, struct istream **stream)
                return zmail->module_ctx.super.istream_opened(_mail, stream);
        }
 
-       if (cache->uid == _mail->uid && cache->box == _mail->box) {
+       if (_mail->uid > 0 && cache->uid == _mail->uid && cache->box == _mail->box) {
                /* use the cached stream. when doing partial reads it should
                   already be seeked into the wanted offset. */
                i_stream_unref(stream);
@@ -148,8 +152,8 @@ static int zlib_istream_opened(struct mail *_mail, struct istream **stream)
                input = *stream;
                *stream = handler->create_istream(input, TRUE);
                i_stream_unref(&input);
-
-               *stream = zlib_mail_cache_open(zuser, _mail, *stream);
+               /* dont cache the stream if _mail->uid is 0 */
+               *stream = zlib_mail_cache_open(zuser, _mail, *stream, (_mail->uid > 0));
        }
        return zmail->module_ctx.super.istream_opened(_mail, stream);
 }
@@ -162,7 +166,7 @@ static void zlib_mail_close(struct mail *_mail)
        struct zlib_mail_cache *cache = &zuser->cache;
        uoff_t size;
 
-       if (cache->uid == _mail->uid && cache->box == _mail->box) {
+       if (_mail->uid > 0 && cache->uid == _mail->uid && cache->box == _mail->box) {
                /* make sure we have read the entire email into the seekable
                   stream (which causes the original input stream to be
                   unrefed). we can't safely keep the original input stream