Report-an-Apple-bug Friday! 7

2006-03-17 03:13:00 UTC

So much for filing these earlier in the week. I spent the whole week pounding on the test cases.

This bug is kevent read events’ .data is random when st_size > ULONG_MAX. It was filed on 2006-03-17 at 01:55.


When the size of a regular file exceeds ULONG_MAX, the value of the event’s data member can be truncated.

Steps to Reproduce:

  1. Create a kqueue.
  2. Open a file for reading.
  3. Add a kevent description to the queue from step 1 with EVFILT_READ and the fd from step 2.
  4. Poll the queue for events.

Expected Results:

The data member of the returned kevent structure, indicating the amount of data between the file’s current position and the end of file, is capped to ULONG_MAX to work around truncation issues.

Actual Results:

The data member of the returned kevent structure, indicating the naked size of the file, may be truncated or appear negative.


None known.


The st_size member of struct stat is an off_t (a long long), which is 8 bytes. The data member of struct kevent is an intptr_t (a long), which is 4 bytes.

kevent appears to simply assign the .st_size value directly to in all read events. This produces the exhibited effect: only the low 4 bytes are copied; the high 4 bytes are thrown away. If the .st_size value exceeds ULONG_MAX, then the resulting 4-byte value would appear random if you didn’t know what was going on.

The other problem is that, unless I’m wrong about what a ‘vnode’ is, this behavior does not match the manpage’s description:


Takes a file descriptor as the identifier, and returns whenever there is data available to read. The behavior of the filter is slightly different depending on the descriptor type.

Returns when the file pointer is not at the end of file. data contains the offset from current position to end of file, and may be negative.

.data only contains the offset to the end of file when the open file’s position is at the start of the file, since at that time, that value is equal to the length of the file.

.data should always contain the offset, unless that value exceeds ULONG_MAX, in which case it should be ULONG_MAX (that is to say, .data should be capped to ULONG_MAX).


  • Don’t use kevent.
  • Ignore and only use results from fstat/read/fread.
  • Use mmap.

A test case is included.

(Note: GeoCities is being cranky today. The test case will be up eventually.)

UPDATE 2007-08-02: I’ve updated the test case to fix a bug. Here’s kevent-read-bug r2.

Technorati tags: ,

3 Responses to “Report-an-Apple-bug Friday! 7”

  1. jkp Says:

    kevent == buggy as hell, my kernel panicing bug is still not fixed :(

  2. Mac-arena the Bored Zo Says:

    What’s the rdar:// URL?

  3. jkp Says:

    rdar://4450881 :)

Leave a Reply

Do not delete the second sentence.