mheily / libkqueue

kqueue(2) compatibility library
Other
236 stars 77 forks source link

FIX Include number of changelist failures in the return count of kevent #109

Closed orbitz closed 3 years ago

orbitz commented 3 years ago

Addresses issue #108

arr2036 commented 3 years ago

Have you got a reference for this behaviour?

The man pages on both macOS and FreeBSD have the following:

     The kevent() system call returns the number of events placed in the
     eventlist, up to the value given by nevents.  If an error occurs while
     processing an element of the changelist and there is enough room in the
     eventlist, then the event will be placed in the eventlist with EV_ERROR
     set in flags and the system error in data.  Otherwise, -1 will be re-
     turned, and errno will be set to indicate the error condition.  If the
     time limit expires, then kevent() returns 0.
arr2036 commented 3 years ago

What's not really clear is the behaviour if insufficient space is available. I'd guess kevent would return with event list only partially processed.

orbitz commented 3 years ago

@arr2036 the behaviour is specified in the portion you quoted. But, your comment made me look over the code again and I realize I made a mistake which I have just fixed. I only update the kevent return value if kevent succeeded.

arr2036 commented 3 years ago

@orbitz overall I agree with the change, but there were multiple other (pre-existing) errors in the code. Changing the return code on its own wasn't enough. This change would have lead to the caller reading uninitialised struct kevent entries as the pointer passed into kevent_copyout wasn't advanced by the number of entries added by kevent_copyin.

kevent_copyin also failed to set the EV_ERROR flag in the kevents it produced, which is required for EV_RECEIPT and if an error occurs.