Commit dbbf8dae dbbf8dae0bdd56d108f8bfda7614b4a3b895eae2 by Sergey Poznyakoff

Fix memory leaks in filter and filter_iconv code.

* mailbox/filter.c (filter_destroy): Destroy the underlying
stream, unless flag is given.
(mu_filter_create): Treat last argument as stream flags.
* mailbox/filter_iconv.c (_icvt_destroy): Free the icvt_stream
structure.

* examples/mimetest.c (message_display_parts): Pass
MU_STREAM_NO_CLOSE to the invocation of mu_filter_create.
* mail/decode.c (display_submessage): Likewise.
* mailbox/attachment.c (mu_message_save_attachment): Likewise.
* mh/mh_list.c (eval_body): Likewise.
* mh/mhn.c (mhn_message_size, show_internal): Likewise.
(finish_text_msg): Likewise; call mu_stream_destroy

* imap4d/preauth.c (decode64_buf): remove unnecessary
call to mu_stream_destroy.
* libmu_auth/ldap.c (chk_md5, chk_smd5)
(chk_sha, chk_ssha): Likewise.

* mailbox/mutil.c: Fix indentation.
1 parent 77b116b1
......@@ -251,7 +251,10 @@ message_display_parts (mu_message_t msg, int indent)
printf ("%*.*sBegin\n", indent, indent, "");
mu_message_get_body (part, &body);
mu_body_get_stream (body, &str);
mu_filter_create (&str, str, encoding, 0, 0);
/* Make sure the original body stream is not closed when
str gets destroyed */
mu_filter_create (&str, str, encoding, MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_NO_CLOSE);
offset = 0;
while (mu_stream_readline (str, buf, sizeof (buf),
offset, &nbytes) == 0 && nbytes)
......
......@@ -217,7 +217,6 @@ decode64_buf (const char *name, unsigned char **pbuf, size_t *psize)
mu_stream_sequential_write (str, name, namelen);
mu_stream_read (flt, (char*) buf, sizeof buf, 0, &size);
mu_stream_destroy (&flt, NULL);
mu_stream_destroy (&str, NULL);
*pbuf = malloc (size);
if (!*pbuf)
return 1;
......
......@@ -602,7 +602,6 @@ chk_md5 (const char *db_pass, const char *pass)
mu_stream_read (flt, (char*) d1, sizeof d1, 0, NULL);
mu_stream_destroy (&flt, NULL);
mu_stream_destroy (&str, NULL);
return memcmp (md5digest, d1, sizeof md5digest) == 0 ?
0 : MU_ERR_AUTH_FAILURE;
......@@ -629,13 +628,11 @@ chk_smd5 (const char *db_pass, const char *pass)
if (!d1)
{
mu_stream_destroy (&flt, NULL);
mu_stream_destroy (&str, NULL);
return ENOMEM;
}
mu_stream_read (flt, (char*) d1, size, 0, &size);
mu_stream_destroy (&flt, NULL);
mu_stream_destroy (&str, NULL);
if (size <= 16)
{
......@@ -674,7 +671,6 @@ chk_sha (const char *db_pass, const char *pass)
mu_stream_read (flt, (char*) d1, sizeof d1, 0, NULL);
mu_stream_destroy (&flt, NULL);
mu_stream_destroy (&str, NULL);
return memcmp (sha1digest, d1, sizeof sha1digest) == 0 ?
0 : MU_ERR_AUTH_FAILURE;
......@@ -701,7 +697,6 @@ chk_ssha (const char *db_pass, const char *pass)
if (!d1)
{
mu_stream_destroy (&flt, NULL);
mu_stream_destroy (&str, NULL);
return ENOMEM;
}
......
......@@ -258,7 +258,8 @@ display_submessage (struct mime_descend_closure *closure, void *data)
/* Can we decode. */
if (mu_filter_create(&d_stream, b_stream, closure->encoding,
MU_FILTER_DECODE, MU_STREAM_READ) == 0)
MU_FILTER_DECODE,
MU_STREAM_READ|MU_STREAM_NO_CLOSE) == 0)
stream = d_stream;
else
stream = b_stream;
......
......@@ -313,7 +313,8 @@ mu_message_save_attachment (mu_message_t msg, const char *filename,
content_encoding = "7bit";
ret =
mu_filter_create (&info->stream, istream, content_encoding,
MU_FILTER_DECODE, MU_STREAM_READ);
MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_NO_CLOSE);
free (content_encoding_mem);
}
}
......
......@@ -46,6 +46,8 @@ static void
filter_destroy (mu_stream_t stream)
{
mu_filter_t filter = mu_stream_get_owner (stream);
if (!(stream->flags & MU_STREAM_NO_CLOSE))
mu_stream_destroy (&filter->stream, mu_stream_get_owner (filter->stream));
if (filter->_destroy)
filter->_destroy (filter);
if (filter->property)
......@@ -165,7 +167,7 @@ mu_filter_get_list (mu_list_t *plist)
int
mu_filter_create (mu_stream_t *pstream, mu_stream_t stream, const char *name,
int type, int direction)
int type, int flags)
{
mu_iterator_t iterator = NULL;
mu_filter_record_t filter_record = NULL;
......@@ -204,14 +206,12 @@ mu_filter_create (mu_stream_t *pstream, mu_stream_t stream, const char *name,
if (found)
{
int flags = 0;
mu_filter_t filter;
filter = calloc (1, sizeof (*filter));
if (filter == NULL)
return ENOMEM;
mu_stream_get_flags (stream, &flags);
status = mu_stream_create (pstream, flags | MU_STREAM_NO_CHECK, filter);
if (status != 0)
{
......@@ -221,7 +221,11 @@ mu_filter_create (mu_stream_t *pstream, mu_stream_t stream, const char *name,
filter->stream = stream;
filter->filter_stream = *pstream;
filter->direction = (direction == 0) ? MU_STREAM_READ : direction;
filter->direction = (flags == 0) ? MU_STREAM_READ
: (flags
& (MU_STREAM_READ |
MU_STREAM_WRITE |
MU_STREAM_RDWR));
filter->type = type;
status = mu_property_create (&(filter->property), filter);
......@@ -249,8 +253,8 @@ mu_filter_create (mu_stream_t *pstream, mu_stream_t stream, const char *name,
}
}
mu_stream_set_open (*pstream, filter_open, filter );
mu_stream_set_close (*pstream, filter_close, filter );
mu_stream_set_open (*pstream, filter_open, filter);
mu_stream_set_close (*pstream, filter_close, filter);
mu_stream_set_read (*pstream, filter_read, filter);
mu_stream_set_readline (*pstream, filter_readline, filter);
mu_stream_set_write (*pstream, filter_write, filter);
......
......@@ -114,6 +114,7 @@ _icvt_destroy (mu_stream_t stream)
s->buf = NULL;
if (s->cd != (iconv_t) -1)
iconv_close (s->cd);
free (s);
}
static int _icvt_read (mu_stream_t stream, char *optr, size_t osize,
......
......@@ -1329,8 +1329,8 @@ mu_decode_filter (mu_stream_t *pfilter, mu_stream_t input,
{
mu_stream_t cvt;
status = mu_filter_iconv_create (&cvt, filter, fromcode, tocode,
MU_STREAM_NO_CLOSE,
mu_default_fallback_mode);
MU_STREAM_NO_CLOSE,
mu_default_fallback_mode);
if (status == 0)
{
if (mu_stream_open (cvt))
......
......@@ -699,7 +699,8 @@ eval_body (struct eval_env *env)
if (encoding)
{
int rc = mu_filter_create(&dstr, input, encoding,
MU_FILTER_DECODE, MU_STREAM_READ);
MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_NO_CLOSE);
if (rc == 0)
input = dstr;
free (encoding);
......
......@@ -1122,8 +1122,9 @@ mhn_message_size (mu_message_t msg, size_t *psize)
mu_message_get_header (msg, &hdr);
_get_content_encoding (hdr, &encoding);
rc = mu_filter_create(&dstr, bstr, encoding,
MU_FILTER_DECODE, MU_STREAM_READ);
rc = mu_filter_create (&dstr, bstr, encoding,
MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_NO_CLOSE);
free (encoding);
if (rc == 0)
{
......@@ -1272,8 +1273,8 @@ show_internal (mu_message_t msg, msg_part_t part, char *encoding, mu_stream_t ou
return 0;
}
mu_body_get_stream (body, &bstr);
rc = mu_filter_create(&dstr, bstr, encoding,
MU_FILTER_DECODE, MU_STREAM_READ);
rc = mu_filter_create (&dstr, bstr, encoding,
MU_FILTER_DECODE, MU_STREAM_READ | MU_STREAM_NO_CLOSE);
if (rc == 0)
bstr = dstr;
cat_message (out, bstr);
......@@ -2052,10 +2053,12 @@ finish_text_msg (struct compose_env *env, mu_message_t *msg, int ascii)
mu_message_get_body (*msg, &body);
mu_body_get_stream (body, &input);
rc = mu_filter_create (&fstr, input, "quoted-printable",
MU_FILTER_ENCODE, MU_STREAM_READ);
MU_FILTER_ENCODE,
MU_STREAM_READ | MU_STREAM_NO_CLOSE);
if (rc == 0)
{
cat_message (output, fstr);
mu_stream_destroy (&fstr, NULL);
mu_message_unref (*msg);
*msg = newmsg;
}
......