Index: src/OFPollKernelEventObserver.m ================================================================== --- src/OFPollKernelEventObserver.m +++ src/OFPollKernelEventObserver.m @@ -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; Index: src/OFSelectKernelEventObserver.m ================================================================== --- src/OFSelectKernelEventObserver.m +++ src/OFSelectKernelEventObserver.m @@ -47,37 +47,46 @@ @implementation OFSelectKernelEventObserver - (instancetype)init { self = [super init]; + @try { #ifdef OF_AMIGAOS - _maxFD = 0; + _maxFD = 0; #else # ifndef OF_WINDOWS - if (_cancelFD[0] >= (int)FD_SETSIZE) - @throw [OFInitializationFailedException - exceptionWithClass: self.class]; + if (_cancelFD[0] >= (int)FD_SETSIZE) + @throw [OFInitializationFailedException + exceptionWithClass: self.class]; # endif - FD_ZERO(&_readFDs); - FD_ZERO(&_writeFDs); - FD_SET(_cancelFD[0], &_readFDs); - - if (_cancelFD[0] > INT_MAX) - @throw [OFOutOfRangeException exception]; - - _maxFD = (int)_cancelFD[0]; + FD_ZERO(&_readFDs); + FD_ZERO(&_writeFDs); + FD_SET(_cancelFD[0], &_readFDs); + + if (_cancelFD[0] > INT_MAX) + @throw [OFOutOfRangeException exception]; + + _maxFD = (int)_cancelFD[0]; #endif + } @catch (id e) { + [self release]; + @throw e; + } return self; } - (void)addObjectForReading: (id )object { int fd = object.fileDescriptorForReading; - if (fd < 0 || fd > INT_MAX - 1) + if (fd < 0) + @throw [OFObserveFailedException exceptionWithObserver: self + errNo: EBADF]; + + if (fd > INT_MAX - 1) @throw [OFOutOfRangeException exception]; #ifndef OF_WINDOWS if (fd >= (int)FD_SETSIZE) @throw [OFOutOfRangeException exception]; @@ -93,11 +102,15 @@ - (void)addObjectForWriting: (id )object { int fd = object.fileDescriptorForWriting; - if (fd < 0 || fd > INT_MAX - 1) + if (fd < 0) + @throw [OFObserveFailedException exceptionWithObserver: self + errNo: EBADF]; + + if (fd > INT_MAX - 1) @throw [OFOutOfRangeException exception]; #ifndef OF_WINDOWS if (fd >= (int)FD_SETSIZE) @throw [OFOutOfRangeException exception]; @@ -116,11 +129,12 @@ /* TODO: Adjust _maxFD */ int fd = object.fileDescriptorForReading; if (fd < 0) - @throw [OFOutOfRangeException exception]; + @throw [OFObserveFailedException exceptionWithObserver: self + errNo: EBADF]; #ifndef OF_WINDOWS if (fd >= (int)FD_SETSIZE) @throw [OFOutOfRangeException exception]; #endif @@ -135,11 +149,13 @@ /* TODO: Adjust _maxFD */ int fd = object.fileDescriptorForWriting; if (fd < 0) - @throw [OFOutOfRangeException exception]; + @throw [OFObserveFailedException exceptionWithObserver: self + errNo: EBADF]; + #ifndef OF_WINDOWS if (fd >= (int)FD_SETSIZE) @throw [OFOutOfRangeException exception]; #endif