shithub: gopher

Download patch

ref: d2dd69c31884b642d5efc11cf1b95b677243d4bd
parent: 3680728b631ed65201b397f4ae3e5d1b03be42f9
author: phil9 <telephil9@gmail.com>
date: Sun Jul 3 01:54:16 EDT 2022

rendermenu: discard invalid lines

	when we were rendering a menu and the line type was neither INFO nor HTML,
	we were handling it like a link. This would lead to gopher crashing when the
	line was actually had an invalid type.
	We now check whether the line type is correct or simply discard the line and
	print a warning.

--- a/gopher.c
+++ b/gopher.c
@@ -129,7 +129,7 @@
 	char *s, *f[5], *t;
 	Gmenu *m;
 	Link *n;
-	int type;
+	int type, c;
 
 	m = malloc(sizeof *m);
 	if(m==nil)
@@ -143,7 +143,7 @@
 		if(s==nil || s[0]=='.')
 			break;
 		type = seltype(s[0]);
-		getfields(s+1, f, 5, 0, "\t\r\n");
+		c = getfields(s+1, f, 5, 0, "\t\r\n");
 		switch(type){
 		case Tinfo:
 			break;
@@ -151,6 +151,10 @@
 			n = mklink(strdup(f[1]+4), nil, Thtml); /* +4 skip URL: */
 			break;
 		default:
+			if(type < 0 && c < 3){
+				fprint(2, "skipping invalid menu line '%s'\n", s);
+				continue;
+			}
 			n = mklink(netmkaddr(f[2], "tcp", f[3]), strdup(f[1]), type);
 			break;
 		}