shithub: riscv

Download patch

ref: 85132f197bc6cef04f707daccf15eca4c471ff50
parent: 30ab804b5878a9f4721fb1de3f03f4a0400a6fb1
author: cinap_lenrek <[email protected]>
date: Sun Jun 28 20:23:37 EDT 2020

upas/fs: move unixheader handling to parseheaders()

parsing the unixheader in mdir fetch routine is the wrong place,
as no invalid character handling has been performed yet. also
the string is not neccesarily null terminated.

avoid duplication with plan9 mbox parsing and just do it in
parseheaders(), which already handles faking the unix headers
for pop3 and imap.

--- a/sys/src/cmd/upas/fs/mbox.c
+++ b/sys/src/cmd/upas/fs/mbox.c
@@ -476,6 +476,20 @@
 	}
 }
 
+static void
+parseunix(Message *m)
+{
+	char *s, *p;
+
+	m->unixheader = smprint("%.*s", utfnlen(m->start, m->header - m->start), m->start);
+	s = m->start + 5;
+	if((p = strchr(s, ' ')) == nil)
+		return;
+	*p = 0;
+	m->unixfrom = strdup(s);
+	*p = ' ';
+}
+
 void
 parseheaders(Mailbox *mb, Message *m, int addfrom, int justmime)
 {
@@ -483,8 +497,20 @@
 	int i, i0, n;
 	uintptr a;
 
+	if(m->header == nil)
+		m->header = m->start;
+
+	/* parse unix header */
+	if(!justmime && !addfrom && m->unixheader == nil){
+		if(strncmp(m->start, "From ", 5) == 0)
+		if((e = memchr(m->start, '\n', m->end - m->start)) != nil){
+			m->header = e + 1;
+			parseunix(m);
+		}
+	}
+
 	/* parse mime headers */
-	p = m->header;
+	p = m->mheader = m->mhend = m->header;
 	i0 = 0;
 	if(justmime)
 		i0 = Mhead;
@@ -520,11 +546,6 @@
 		m->hend = p;
 		m->mhend = m->header;
 	}
-	/*
-	 * not all attachments have mime headers themselves.
-	 */
-	if(!m->mheader)
-		m->mhend = 0;
 	if(*p == '\n')
 		p++;
 	m->rbody = m->body = p;
@@ -538,9 +559,10 @@
 	 *  adding the unix header all the time screws up mime-attached
 	 *  rfc822 messages.
 	 */
-	if(!addfrom && !m->unixfrom)
+	if(!addfrom && m->unixfrom == nil) {
+		free(m->unixheader);
 		m->unixheader = nil;
-	else if(m->unixheader == nil){
+	} else if(m->unixheader == nil){
 		if(m->unixfrom && strcmp(m->unixfrom, "???") != 0)
 			p = m->unixfrom;
 		else if(m->from)
--- a/sys/src/cmd/upas/fs/mdir.c
+++ b/sys/src/cmd/upas/fs/mdir.c
@@ -22,24 +22,10 @@
 	return r != l ? -1: 0;
 }
 
-static void
-parseunix(Message *m)
-{
-	char *s, *p;
-
-	m->unixheader = smprint("%.*s", utfnlen(m->start, m->header - m->start), m->start);
-	s = m->start + 5;
-	if((p = strchr(s, ' ')) == nil)
-		return;
-	*p = 0;
-	m->unixfrom = strdup(s);
-	*p = ' ';
-}
-
 static int
 mdirfetch(Mailbox *mb, Message *m, uvlong o, ulong l)
 {
-	char buf[Pathlen], *x;
+	char buf[Pathlen];
 	Mdir *mdir;
 
 	mdir = mb->aux;
@@ -51,17 +37,6 @@
 		mdprint(mdir, "%r\n");
 		return -1;
 	}
-	if(m->header == nil)
-		m->header = m->start;
-	if(m->header == m->start)
-	if(o + l >= 36)
-	if(strncmp(m->start, "From ", 5) == 0)
-	if(x = strchr(m->start, '\n')){
-		m->header = x + 1;
-		if(m->unixfrom == nil)
-			parseunix(m);
-	}
-	m->mheader = m->mhend = m->header;
 	mdprint(mdir, "fetched [%llud, %llud]\n", o, o + l);
 	return 0;
 }
--- a/sys/src/cmd/upas/fs/plan9.c
+++ b/sys/src/cmd/upas/fs/plan9.c
@@ -50,29 +50,6 @@
 	return r;
 }
 
-static char*
-parseunix(Message *m)
-{
-	char *s, *p, *q;
-	Tm tm;
-
-	m->unixheader = smprint("%.*s", utfnlen(m->start, m->header - m->start), m->start);
-	s = m->start + 5;
-	if((p = strchr(s, ' ')) == nil)
-		return s;
-	*p = 0;
-	m->unixfrom = strdup(s);
-	*p++ = ' ';
-	if(q = strchr(p, '\n'))
-		*q = 0;
-	if(strtotm(p, &tm) < 0)
-		return p;
-	if(q)
-		*q = '\n';
-	m->fileid = (uvlong)tm2sec(&tm) << 8;
-	return 0;
-}
-
 static void
 addtomessage(Message *m, char *p, int n)
 {
@@ -215,7 +192,7 @@
 static char*
 readmbox(Mailbox *mb, Mlock *lk)
 {
-	char *p, *x, buf[Pathlen];
+	char buf[Pathlen];
 	Biobuf *in;
 	Dir *d;
 	Inbuf b;
@@ -306,12 +283,6 @@
 		}
 		if(m == nil)
 			continue;
-		m->header = m->end;
-		if(x = strchr(m->start, '\n'))
-			m->header = x + 1;
-		if(p = parseunix(m))
-			sysfatal("%s:%lld naked From in body? [%s]", mb->path, seek(Bfildes(in), 0, 1), p);
-		m->mheader = m->mhend = m->header;
 		parse(mb, m, 0, 0);
 		if(m != *l && m->deleted != Dup){
 			logmsg(m, "new");