@@ -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