shithub: riscv

Download patch

ref: 543c35debaba0e3fc2538503adea228cd0a28465
parent: 4cef9b12fd98d3d93894e3267c47f36775560702
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Nov 21 12:17:54 EST 2019

fix ref882 reference parsing.

we were getting nils in the list when there were many references.
this fixes and simplifies the copying loop and makes the code rhyme.

--- a/sys/src/cmd/upas/fs/dat.h
+++ b/sys/src/cmd/upas/fs/dat.h
@@ -120,7 +120,7 @@
 	char	*unixheader;
 	char	*unixfrom;
 	char	*date822;
-	char	*references[Nref];
+	char	*references[Nref]; /* nil terminated unless full */
 
 	/* mime info */
 	char	*charset;		
--- a/sys/src/cmd/upas/fs/mbox.c
+++ b/sys/src/cmd/upas/fs/mbox.c
@@ -880,7 +880,7 @@
 ref822(Message *m, Header *h, char*, char *p)
 {
 	char **a, *s, *f[Nref + 1];
-	int i, j, k, n;
+	int i, j, n;
 
 	s = strdup(skipwhite(p + h->len));
 	n = getfields(s, f, nelem(f), 1, "<> \n\t\r,");
@@ -889,26 +889,24 @@
 	n = uniqarray(f, n, 0);
 	a = m->references;
 	for(i = 0; i < Nref; i++)
-		if(a[i] == 0)
+		if(a[i] == nil)
 			break;
 	/*
 	 * if there are too many references, drop from the beginning
 	 * of the list.
 	 */
-	j = i + n - Nref;
-	if(j > 0){
-		if(j > Nref)
-			j = Nref;
-		for(k = 0; k < j; k++)
-			free(a[k]);
-		memmove(a, a + j, sizeof a[0]*(Nref - j));
-		memset(a + j, 0, Nref - j);
-		i -= j;
+	if(i + n > Nref){
+		for(i = 0; i < n; i++)
+			free(a[i]);
+		for(i = n; i < Nref; i++)
+			a[i - n] = a[i];
 	}
 	for(j = 0; j < n;)
 		a[i++] = strdup(f[j++]);
+	n = uniqarray(a, i, 1);
+	if(n < Nref)
+		a[n] = nil;
 	free(s);
-	uniqarray(a, i, 1);
 	return (char*)~0;
 }