Commit 72a5595a 72a5595aa569f4036a15a0e0c8e9366e8b7c8e9d by Sergey Poznyakoff

Bugfix.

* pop3d/retr.c (pop3d_retr): Use constant-size buffer.  This
avoids running out of memory on messages that do not contain
trailing newline.
* pop3d/top.c (pop3d_top): Likewise.
1 parent 2fa93e70
2008-08-04 Sergey Poznyakoff <gray@gnu.org.ua>
* pop3d/retr.c (pop3d_retr): Use constant-size buffer. This
avoids running out of memory on messages that do not contain
trailing newline.
* pop3d/top.c (pop3d_top): Likewise.
2008-07-31 Sergey Poznyakoff <gray@gnu.org.ua>
* libproto/include/amd.h: Fix indentation.
......
......@@ -24,13 +24,13 @@ int
pop3d_retr (const char *arg)
{
size_t mesgno, n;
char *buf;
size_t buflen = BUFFERSIZE;
char buf[BUFFERSIZE];
mu_message_t msg = NULL;
mu_attribute_t attr = NULL;
mu_stream_t stream = NULL;
off_t off;
mu_off_t off;
int prev_nl;
if ((strlen (arg) == 0) || (strchr (arg, ' ') != NULL))
return ERR_BAD_ARGS;
......@@ -50,37 +50,36 @@ pop3d_retr (const char *arg)
pop3d_outf ("+OK\r\n");
off = n = 0;
buf = malloc (buflen * sizeof (*buf));
if (buf == NULL)
pop3d_abquit (ERR_NO_MEM);
while (mu_stream_readline (stream, buf, buflen, off, &n) == 0
prev_nl = 1;
while (mu_stream_readline (stream, buf, sizeof(buf), off, &n) == 0
&& n > 0)
{
/* Nuke the trailing newline. */
if (prev_nl && buf[0] == '.')
pop3d_outf (".");
if (buf[n - 1] == '\n')
buf[n - 1] = '\0';
else /* Make room for the line. */
{
buflen *= 2;
buf = realloc (buf, buflen * sizeof (*buf));
if (buf == NULL)
pop3d_abquit (ERR_NO_MEM);
continue;
buf[n - 1] = '\0';
pop3d_outf ("%s\r\n", buf);
prev_nl = 1;
}
if (buf[0] == '.')
pop3d_outf (".%s\r\n", buf);
else
pop3d_outf ("%s\r\n", buf);
{
pop3d_outf ("%s", buf);
prev_nl = 0;
}
off += n;
}
if (!prev_nl)
pop3d_outf ("\r\n");
if (!mu_attribute_is_read (attr))
mu_attribute_set_read (attr);
pop3d_mark_retr (attr);
free (buf);
pop3d_outf (".\r\n");
return OK;
......
......@@ -31,8 +31,7 @@ pop3d_top (const char *arg)
mu_body_t body;
mu_stream_t stream;
char *mesgc, *linesc;
char *buf;
size_t buflen = BUFFERSIZE;
char buf[BUFFERSIZE];
size_t n;
off_t off;
......@@ -65,11 +64,8 @@ pop3d_top (const char *arg)
/* Header. */
mu_message_get_header (msg, &hdr);
mu_header_get_stream (hdr, &stream);
buf = malloc (buflen * sizeof (*buf));
if (buf == NULL)
pop3d_abquit (ERR_NO_MEM);
off = n = 0;
while (mu_stream_readline (stream, buf, buflen, off, &n) == 0
while (mu_stream_readline (stream, buf, sizeof(buf), off, &n) == 0
&& n > 0)
{
/* Nuke the trainline newline. */
......@@ -86,33 +82,35 @@ pop3d_top (const char *arg)
/* Lines of body. */
if (lines)
{
int prev_nl = 1;
mu_message_get_body (msg, &body);
mu_body_get_stream (body, &stream);
n = off = 0;
while (mu_stream_readline (stream, buf, buflen, off, &n) == 0
while (mu_stream_readline (stream, buf, sizeof(buf), off, &n) == 0
&& n > 0 && lines > 0)
{
/* Nuke the trailing newline. */
if (prev_nl && buf[0] == '.')
pop3d_outf (".");
if (buf[n - 1] == '\n')
buf[n - 1] = '\0';
else /* make room for the line. */
{
buflen *= 2;
buf = realloc (buf, buflen * sizeof (*buf));
if (buf == NULL)
pop3d_abquit (ERR_NO_MEM);
continue;
buf[n - 1] = '\0';
pop3d_outf ("%s\r\n", buf);
prev_nl = 1;
lines--;
}
if (buf[0] == '.')
pop3d_outf (".%s\r\n", buf);
else
pop3d_outf ("%s\r\n", buf);
lines--;
{
pop3d_outf ("%s", buf);
prev_nl = 0;
}
off += n;
}
if (!prev_nl)
pop3d_outf ("\r\n");
}
free (buf);
pop3d_outf (".\r\n");
return OK;
......