@@ -71,13 +71,21 @@ - (void)of_addObject: (id)object fileDescriptor: (int)fd events: (short)events { - struct pollfd *FDs = _FDs.mutableItems; - size_t count = _FDs.count; - bool found = false; + struct pollfd *FDs; + size_t count; + bool found; + + if (fd < 0) + @throw [OFObserveFailedException exceptionWithObserver: self + errNo: EBADF]; + + FDs = _FDs.mutableItems; + count = _FDs.count; + found = false; for (size_t i = 0; i < count; i++) { if (FDs[i].fd == fd) { FDs[i].events |= events; found = true; @@ -102,12 +110,19 @@ - (void)of_removeObject: (id)object fileDescriptor: (int)fd events: (short)events { - struct pollfd *FDs = _FDs.mutableItems; - size_t nFDs = _FDs.count; + struct pollfd *FDs; + size_t nFDs; + + if (fd < 0) + @throw [OFObserveFailedException exceptionWithObserver: self + errNo: EBADF]; + + FDs = _FDs.mutableItems; + nFDs = _FDs.count; for (size_t i = 0; i < nFDs; i++) { if (FDs[i].fd == fd) { FDs[i].events &= ~events;