--- /sys/src/cmd/upas/ned/nedmail.c Mon Dec 2 23:27:20 2013 +++ /sys/src/cmd/upas/ned/nedmail.c Wed Jan 8 23:56:45 2014 @@ -1329,12 +1329,14 @@ char *nl; int left; - for (nl = buf, left = n; left >= 2 && - (nl = memchr(nl, '\r', left)) != nil; left = n - (nl - buf)) - if (nl[1] == '\n'){ - memmove(nl, nl+1, left-1); /* delete the cr */ - --n; - } + for(nl = buf, left = n; + left >= 2 && (nl = memchr(nl, '\r', left)) != nil; + left = n - (nl - buf)) + if(nl[1] == '\n'){ /* newline? delete the cr */ + --n; /* portion left is about to get smaller */ + memmove(nl, nl+1, n - (nl - buf)); + }else + nl++; return n; } @@ -1357,7 +1359,7 @@ if(interrupted) break; n = decrlf(buf, n); - if(Bwrite(&out, buf, n) <= 0) + if(n > 0 && Bwrite(&out, buf, n) <= 0) break; tot += n; } @@ -1422,8 +1424,7 @@ static Message* bestalt(Message *m) { - Message *nm; - Message *realplain, *realhtml, *realcal; + Message *nm, *realplain, *realhtml, *realcal; Ctype *cp; realplain = realhtml = realcal = nil; @@ -1474,6 +1475,8 @@ String *s; char buf[128]; + if(m == nil) + return m; if(m == &top) return ⊤ if(m->parent == &top) @@ -1526,7 +1529,6 @@ Bprint(&out, "\n!--- using plumber to display message of type %s\n", m->type); else Bprint(&out, "\n!--- cannot display messages of type %s\n", m->type); - return m; }