git: 9front

Download patch

ref: 1fd60061a8810f4f0573bb74c74590137564e08e
parent: e43190dd835bacecd41f88a9d8316b879e0e952a
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Sun Mar 10 14:35:07 EDT 2013

ape: check for invalid filedescriptors in select()

--- a/sys/src/ape/lib/ap/plan9/_buf.c
+++ b/sys/src/ape/lib/ap/plan9/_buf.c
@@ -70,6 +70,12 @@
 		return 0;
 
 	lock(&mux->lock);
+	f = &_fdinfo[fd];
+	if((f->flags&FD_BUFFERED) != 0){
+		unlock(&mux->lock);
+		return 0;
+	}
+
 	slot = mux->curfds++;
 	if(mux->curfds > INITBUFS) {
 		if(_SEGBRK(mux, mux->bufs+mux->curfds) < 0){
@@ -79,7 +85,6 @@
 		}
 	}
 
-	f = &_fdinfo[fd];
 	b = &mux->bufs[slot];
 	b->n = 0;
 	b->putnext = b->data;
@@ -288,16 +293,13 @@
 	for(i = 0; i < nfds; i++)
 		if((rfds && FD_ISSET(i, rfds)) || (efds && FD_ISSET(i, efds))){
 			f = &_fdinfo[i];
-			if(!(f->flags&FD_BUFFERED))
-				if(_startbuf(i) != 0) {
-					return -1;
-				}
-			b = f->buf;
-			if(rfds && FD_ISSET(i,rfds) && b->eof && b->n == 0)
-			if(efds == 0 || !FD_ISSET(i,efds)) {
-				errno = EBADF;		/* how X tells a client is gone */
+			if((f->flags&FD_ISOPEN) == 0){
+				errno = EBADF;
 				return -1;
 			}
+			if((f->flags&FD_BUFFERED) == 0)
+				if(_startbuf(i) != 0)
+					return -1;
 		}
 
 	/* check wfds;  for now, we'll say they are all ready */
@@ -305,6 +307,11 @@
 	if(wfds && FD_ANYSET(wfds)){
 		for(i = 0; i<nfds; i++)
 			if(FD_ISSET(i, wfds)) {
+				f = &_fdinfo[i];
+				if((f->flags&FD_ISOPEN) == 0){
+					errno = EBADF;
+					return -1;
+				}
 				n++;
 			}
 	}
--