Commit 8f8e01b9 8f8e01b920d0601df5e60cd8060eba5909a10178 by Sergey Poznyakoff

Bugfixes

* include/mailutils/header.h (mu_header_invalidate): New proto.
* mailbox/header.c (mu_header_invalidate): New function.

* libmu_argp/muinit.c (get_canonical_name): Avoid coredumping
if argp_program_version is NULL.
* mailbox/base64.c (mu_base64_decode): Fix inconsistent return
code.
* mailbox/debug.c (mu_debug_destroy): Allow for debug->stream == NULL.

* mailbox/mapfile_stream.c (_mapfile_truncate): Incorrect size
was used when unmapping.
* mailbox/message.c (mu_message_create_copy): Use a temporary
memory stream.
(mu_message_get_body): Comment out the check for MESSAGE_INTERNAL_STREAM.
(_message_get_stream): Initialize message header and body.

* mailbox/progmailer.c (mu_progmailer_send): Check  return
from the mu_header_get_streamref.

* mailbox/stream.c (_stream_scandelim, _stream_readdelim): If
size is 0, return MU_ERR_BUFSPACE.
1 parent 85b56e79
......@@ -78,6 +78,7 @@ extern "C" {
extern int mu_header_create (mu_header_t *, const char *, size_t);
extern void mu_header_destroy (mu_header_t *);
extern int mu_header_invalidate (mu_header_t);
extern int mu_header_is_modified (mu_header_t);
extern int mu_header_clear_modified (mu_header_t);
......
# Suppress a valgrind message about use of uninitialized memory in freesa().
# This use is OK because it provides only a speedup.
{
freesa
Memcheck:Cond
fun:freesa
}
......@@ -75,8 +75,10 @@ get_canonical_name ()
{
char *name;
size_t len;
char *p = strchr (argp_program_version, ' ');
if (!p)
char *p;
if (!argp_program_version ||
!(p = strchr (argp_program_version, ' ')))
return strdup (mu_program_name);
len = p - argp_program_version;
name = malloc (len + 1);
......
......@@ -88,7 +88,7 @@ mu_base64_decode (const unsigned char *input, size_t input_len,
|| input[2] > 127 || ((input[2] != '=') && (b64val[input[2]] == -1))
|| input[3] > 127 || ((input[3] != '=')
&& (b64val[input[3]] == -1)))
return -1;
return EINVAL;
*out++ = (b64val[input[0]] << 2) | (b64val[input[1]] >> 4);
if (input[2] != '=')
{
......
......@@ -55,13 +55,16 @@ mu_debug_destroy (mu_debug_t *pdebug, void *owner)
mu_debug_t debug = *pdebug;
if (debug->owner == owner)
{
mu_off_t len = 0;
int rc = mu_stream_size (debug->stream, &len);
if (rc == 0 && len)
/* Flush leftover data */
mu_debug_printf (debug, 0, "\n");
if (debug->stream)
{
mu_off_t len = 0;
int rc = mu_stream_size (debug->stream, &len);
if (rc == 0 && len)
/* Flush leftover data */
mu_debug_printf (debug, 0, "\n");
mu_stream_destroy (&debug->stream);
mu_stream_destroy (&debug->stream);
}
if (debug->destroy)
debug->destroy (debug->data);
free (*pdebug);
......
......@@ -911,6 +911,15 @@ mu_header_size (mu_header_t header, size_t *psize)
return status;
}
int
mu_header_invalidate (mu_header_t hdr)
{
if (hdr == NULL)
return EINVAL;
mu_hdrent_free_list (hdr);
return 0;
}
static void
mu_hdrent_fixup (mu_header_t hdr, struct mu_hdrent *ent)
......
......@@ -129,7 +129,7 @@ _mapfile_truncate (mu_stream_t stream, mu_off_t len)
if (mfs->ptr == MAP_FAILED)
return EINVAL;
/* Remap. */
if (mfs->ptr && munmap (mfs->ptr, len) != 0)
if (mfs->ptr && munmap (mfs->ptr, mfs->size) != 0)
{
int err = errno;
mfs->ptr = MAP_FAILED;
......
......@@ -569,33 +569,36 @@ mu_message_create_copy (mu_message_t *to, mu_message_t from)
{
int status = 0;
mu_stream_t fromstr = NULL;
mu_stream_t tostr = NULL;
size_t n = 0;
char buf[512];
mu_stream_t tmp = NULL;
if (!to)
return MU_ERR_OUT_PTR_NULL;
if (!from)
return EINVAL;
if ((status = mu_message_create (to, NULL)))
status = mu_memory_stream_create (&tmp, MU_STREAM_RDWR|MU_STREAM_SEEK);
if (status)
return status;
mu_message_get_streamref (from, &fromstr);
mu_message_get_streamref (*to, &tostr);
status = mu_message_get_streamref (from, &fromstr);
if (status)
{
mu_stream_destroy (&tmp);
return status;
}
status = mu_stream_seek (fromstr, 0, MU_SEEK_SET, NULL);
status = mu_stream_copy (tmp, fromstr, 0);
if (status == 0)
while ((status = mu_stream_readline (fromstr, buf, sizeof (buf), &n)) == 0
&& n > 0)
mu_stream_write (tostr, buf, n, NULL);
{
status = mu_message_create (to, NULL);
if (status == 0)
mu_message_set_stream (*to, tmp, NULL);
}
mu_stream_destroy (&fromstr);
mu_stream_destroy (&tostr);
if (status)
mu_message_destroy (to, NULL);
mu_stream_destroy (&tmp);
mu_stream_destroy (&fromstr);
return status;
}
......@@ -721,14 +724,16 @@ mu_message_get_body (mu_message_t msg, mu_body_t *pbody)
int status = mu_body_create (&body, msg);
if (status != 0)
return status;
/* If a stream is already set use it to create the body stream. */
/* If a stream is already set, use it to create the body stream. */
/* FIXME: I'm not sure if the second condition is really needed */
if (msg->stream && (msg->flags & MESSAGE_INTERNAL_STREAM))
if (msg->stream/* && (msg->flags & MESSAGE_INTERNAL_STREAM)*/)
{
size_t size = 0;
mu_stream_t stream;
int flags = 0;
/* FIXME: The size cannot be used as offset, because
the headers might have been modified in between. */
status = mu_header_size (msg->header, &size);
if (status)
return status;
......@@ -790,7 +795,20 @@ _message_get_stream (mu_message_t msg, mu_stream_t *pstream, int ref)
if (msg->stream == NULL)
{
int status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
int status;
mu_header_t hdr;
mu_body_t body;
/* FIXME: Kind of a kludge: make sure the message has header
and body initialized. */
status = mu_message_get_header (msg, &hdr);
if (status)
return status;
status = mu_message_get_body (msg, &body);
if (status)
return status;
status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
if (status)
return status;
msg->flags |= MESSAGE_INTERNAL_STREAM;
......
......@@ -188,8 +188,13 @@ mu_progmailer_send (struct _mu_progmailer *pm, mu_message_t msg)
if (!pm || !msg)
return EINVAL;
mu_message_get_header (msg, &hdr);
mu_header_get_streamref (hdr, &stream);
status = mu_header_get_streamref (hdr, &stream);
if (status)
{
MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
"cannot get header stream: %s\n", mu_strerror (status));
return status;
}
MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "Sending headers...\n");
mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
while ((status = mu_stream_readline (stream, buffer, sizeof (buffer),
......@@ -225,7 +230,13 @@ mu_progmailer_send (struct _mu_progmailer *pm, mu_message_t msg)
MU_DEBUG (pm->debug, MU_DEBUG_TRACE, "Sending body...\n");
mu_message_get_body (msg, &body);
mu_body_get_streamref (body, &stream);
status = mu_body_get_streamref (body, &stream);
if (status)
{
MU_DEBUG1 (pm->debug, MU_DEBUG_ERROR,
"cannot get body stream: %s\n", mu_strerror (status));
return status;
}
mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
while ((status = mu_stream_read (stream, buffer, sizeof (buffer),
......
......@@ -610,6 +610,8 @@ _stream_scandelim (mu_stream_t stream, char *buf, size_t size, int delim,
size_t nread = 0;
size--;
if (size == 0)
return MU_ERR_BUFSPACE;
while (size)
{
char *p;
......@@ -647,6 +649,8 @@ _stream_readdelim (mu_stream_t stream, char *buf, size_t size,
size_t n = 0, rdn;
size--;
if (size == 0)
return MU_ERR_BUFSPACE;
for (n = 0;
n < size && (rc = mu_stream_read (stream, &c, 1, &rdn)) == 0 && rdn;)
{
......