code: plan9front

Download patch

ref: 1047b53efc370ad77546337a221171777b6face1
parent: 50efe18550c5f86ec99c307fe17c2b5c6d061c67
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Jan 19 14:56:38 EST 2021

ape/libap: fix _startbuf, check rfork return (thanks pixelherodev)

When _startbuf is invoked, it would crash on the second invocation
if creating a mux segment failed. This is because the first attempt
would assign the return value -1 to the global mux variable, and
the second attempt would notice that the global mux was not nil,
and would attempt to use it.

This change only assigns to the global variable if the allocation
of the segment was a success.

While we're here, we should also check the return of the rfork call.

--- a/sys/src/ape/lib/ap/plan9/_buf.c
+++ b/sys/src/ape/lib/ap/plan9/_buf.c
@@ -54,14 +54,19 @@
 	Fdinfo *f;
 	Muxbuf *b;
 	void *v;
+	Muxseg *m;
 
 	if(mux == 0){
-		_RFORK(RFREND);
-		mux = (Muxseg*)_SEGATTACH(0, "shared", 0, sizeof(Muxseg));
-		if(mux == (void*)-1){
+		if(_RFORK(RFREND) == -1){
 			_syserrno();
 			return -1;
 		}
+		m = (Muxseg*)_SEGATTACH(0, "shared", 0, sizeof(Muxseg));
+		if(m == (void*)-1){
+			_syserrno();
+			return -1;
+		}
+		mux = m;
 		/* segattach has returned zeroed memory */
 		atexit(_killmuxsid);
 	}