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.
This commit is contained in:
Ori Bernstein 2021-01-19 19:56:38 -08:00
parent 50efe18550
commit 1047b53efc

View file

@ -54,14 +54,19 @@ _startbuf(int fd)
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);
}