shithub: riscv

Download patch

ref: 9891d13887d66ecee6d6ae960df764d5b8dbc2df
parent: feca85431791736244f7e2c1505ad9976314d9b4
author: cinap_lenrek <[email protected]>
date: Fri May 18 16:25:50 EDT 2012

webfs: nstrcpy paranoia

--- a/sys/src/cmd/webfs/fns.h
+++ b/sys/src/cmd/webfs/fns.h
@@ -2,6 +2,8 @@
 void*	emalloc(int n);
 char*	estrdup(char *s);
 
+void	nstrcpy(char *to, char *from, int n);
+
 Key*	addkey(Key *h, char *key, char *val);
 Key*	delkey(Key *h, char *key);
 char*	lookkey(Key *k, char *key);
--- a/sys/src/cmd/webfs/fs.c
+++ b/sys/src/cmd/webfs/fs.c
@@ -310,7 +310,7 @@
 				Key *k;
 
 				for(k = f->client->qbody->hdr; k; k = k->next){
-					strncpy(buf, k->key, sizeof(buf));
+					nstrcpy(buf, k->key, sizeof(buf));
 					if(!strcmp(name, fshdrname(buf)))
 						break;
 				}
@@ -598,7 +598,7 @@
 	}
 	else if(!strcmp(ctl, "request")){
 		p = cl->request;
-		strncpy(p, arg, sizeof(cl->request));
+		nstrcpy(p, arg, sizeof(cl->request));
 		for(; *p && isalpha(*p); p++)
 			*p = toupper(*p);
 		*p = 0;
@@ -624,7 +624,7 @@
 			nil,
 		};
 		for(t = tab; *t; t++){
-			strncpy(buf, *t, sizeof(buf));
+			nstrcpy(buf, *t, sizeof(buf));
 			if(!strcmp(ctl, fshdrname(buf))){
 				cl->hdr = delkey(cl->hdr, *t);
 				if(arg && *arg)
--- a/sys/src/cmd/webfs/http.c
+++ b/sys/src/cmd/webfs/http.c
@@ -108,7 +108,7 @@
 	h->keep = 1;
 	h->len = 0;
 	h->fd = fd;
-	strncpy(h->addr, addr, sizeof(h->addr));
+	nstrcpy(h->addr, addr, sizeof(h->addr));
 
 	return h;
 }
@@ -441,7 +441,7 @@
 
 	incref(qbody);
 	if(qpost) incref(qpost);
-	strncpy(method, m, sizeof(method));
+	nstrcpy(method, m, sizeof(method));
 	switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
 	default:
 		return;
@@ -640,7 +640,7 @@
 					if(cistrcmp(s, "ICY"))
 						break;
 				}
-				strncpy(status, x, sizeof(status));
+				nstrcpy(status, x, sizeof(status));
 				continue;
 			}
 			if((k = parsehdr(s)) == nil)
@@ -730,7 +730,7 @@
 				qpost = nil;
 			}
 			if(cistrcmp(method, "HEAD"))
-				strncpy(method, "GET", sizeof(method));
+				nstrcpy(method, "GET", sizeof(method));
 		case 301:	/* Moved Permanently */
 		case 307:	/* Temporary Redirect */
 		case 308:	/* Resume Incomplete */
--- a/sys/src/cmd/webfs/sub.c
+++ b/sys/src/cmd/webfs/sub.c
@@ -26,6 +26,13 @@
 	return s;
 }
 
+void
+nstrcpy(char *to, char *from, int n)
+{
+	strncpy(to, from, n);
+	to[n-1] = 0;
+}
+
 Key*
 addkey(Key *h, char *key, char *val)
 {