shithub: front

Download patch

ref: 2742165cfb013df15ba69f211678e32209a6c03e
parent: 00f50e30f9b10dfd7920d2744e56c35e6b67a5f4
author: qwx <qwx@sciops.net>
date: Mon Aug 18 12:00:52 EDT 2025

devenv: fix off-by-one in nul to space replacement (thanks cinap_lenrek)

e->value may be nul-terminated, which is taken into account
by e->len. commit 8f3b47e092eb07e20238089a047f2993242d3302
unintentionally replaced that nul with a space before
appending another nul. variables such as $mouseport would
have a space character appended to their value and be no
longer recognized in scripts like /rc/bin/screenrc.
instead, check for any terminating nul characters and
skip them.

--- a/sys/src/9/port/devenv.c
+++ b/sys/src/9/port/devenv.c
@@ -504,7 +504,7 @@
 	Egrp *eg = &confegrp;
 	Evalue *e;
 	char *p, *q;
-	int i, n;
+	int i, n, m;
 
 	rlock(eg);
 	n = 1;
@@ -528,12 +528,16 @@
 		memmove(q, e->name, n);
 		q += n;
 		memmove(q, e->value, e->len);
-		q[e->len] = 0;
-		for(n=0; n<e->len; n++){
+		for(m = e->len; m > 0; m--){
+			if(q[m-1] != '\0')
+				break;
+		}
+		for(n = 0; n < m; n++){
 			if(q[n] == '\0')
 				q[n] = ' ';
 		}
-		q += n+1;
+		q[m] = 0;
+		q += m+1;
 	}
 	*q = '\0';
 	runlock(eg);
--