From f7469b506efca7aa155899a9398b7ce490dd4ba1 Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Fri, 17 Jul 2009 22:00:05 +0200 Subject: [PATCH] fix saving bodies --- ham.c | 52 ++++++++++++++++++---------------------------------- int.c | 7 ++++--- umpf.h | 1 + 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/ham.c b/ham.c index 6b68404..f8f96bd 100644 --- a/ham.c +++ b/ham.c @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -79,10 +80,9 @@ get_body(void) { char* buf; struct email* b; - int c, fd, cannot_save = 0; + int c, fd; int will_save = 0; int i = 0; - int j = 0; int curbufsize = MAIL_LEN; char* tmpfile; int res; @@ -97,23 +97,21 @@ get_body(void) while ((c = getchar()) != EOF){ buf[i++] = c; if (i >= curbufsize - 1) { - if (cannot_save) { - buf = xrealloc(buf, curbufsize *= 2); - continue; - } tmpfile = xstrdup("/tmp/umpf.XXXXXX"); fd = mkstemp(tmpfile); /* cannot create tmpfile, try to continue reading */ - if (fd < 0) { - buf = xrealloc(buf, curbufsize *= 2); - cannot_save = 1; - free_string(tmpfile); - continue; - } else { + if (fd < 0) + bye(EX_TEMPFAIL, "%m"); + else { will_save = 1; b->body = NULL; b->tmpfile = tmpfile; b->fd = fd; + res = write(fd, buf, MAIL_LEN); + if (res < MAIL_LEN) { + unlink(b->tmpfile); + bye(EX_TEMPFAIL, "%m"); + } break; } } @@ -121,6 +119,7 @@ get_body(void) b->body_len = i; /* save rest of the body to the tmpfile */ if (will_save) { + int j = 0; while ((c = getchar()) != EOF){ buf[j++] = c; b->body_len++; @@ -128,32 +127,17 @@ get_body(void) j = 0; res = write(fd, buf, MAIL_LEN); if (res < MAIL_LEN) { - int missing = MAIL_LEN - res; - curbufsize = 2*b->body_len; - buf = xrealloc(buf, curbufsize); - // no point to check length here - read(fd, buf, b->body_len - missing); unlink(b->tmpfile); - b->tmpfile = NULL; - b->fd = -1; - b->body = buf; - break; + bye(EX_TEMPFAIL, "%m"); } } } - /* could not write all the body to the tmpfile, try to read - the rest */ - if (b->body) { - while ((c = getchar()) != EOF){ - buf[j++] = c; - b->body_len++; - if (i >= curbufsize - 1) { - buf = xrealloc(buf, curbufsize *= 2); - } - } + res = write(fd, buf, j); + if (res < j) { + unlink(b->tmpfile); + bye(EX_TEMPFAIL, "%m"); } } - return b; } @@ -277,13 +261,13 @@ write_email_to_fd(int fd, struct email* email) struct hlist* ph; LIST_FOREACH(ph, email->headers){ written = write(fd, ph->name, strlen(ph->name)); - if (written < strlen(ph->name)) + if (written < (ssize_t) strlen(ph->name)) return 1; written = write(fd, ":", 1); if (written < 1) return 1; written = write(fd, ph->value, strlen(ph->value)); - if (written < strlen(ph->value)) + if (written < (ssize_t) strlen(ph->value)) return 1; written = write(fd, "\n", 1); if (written < 1) diff --git a/int.c b/int.c index d4360c6..e1c906f 100644 --- a/int.c +++ b/int.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "cond.tab.h" #include "umpf.h" @@ -17,7 +18,7 @@ #define INT_TO_STRING_LEN ((sizeof(int)*4*CHAR_BIT)/10 + 6) -static void __attribute__ ((noreturn)) +void __attribute__ ((noreturn)) bye(int code, char* msg, ...) { va_list args; @@ -391,7 +392,7 @@ deliver(char* where, int copy, struct list* hash) if (!copy) { if (res) - bye(-res, "%m"); + bye(EX_TEMPFAIL, "%m"); else bye(0, NULL); } @@ -426,7 +427,7 @@ end: destroy_email(em); if (!copy) { if (res) - bye(-res, "%m"); + bye(EX_TEMPFAIL, "%m"); else bye(0, NULL); } diff --git a/umpf.h b/umpf.h index 87f5477..7947ef3 100644 --- a/umpf.h +++ b/umpf.h @@ -204,6 +204,7 @@ void save_current_headers(struct list* hash); void print_vars(struct list* hash); void interp(struct list* ins, struct list* hash); void free_string(char* c); +void __attribute__ ((noreturn)) bye(int code, char* msg, ...); /* ham.c */ char* default_mailbox; -- 2.39.2