git: 9front

Download patch

ref: 1f2e251f03086cfb17f1277dc5e8061e750432c4
parent: ec52236a0e83800edab42efbc334f99dae826b66
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);
 	}
--