[NSFileHandle readDataOfLength:N] with N >4096 locks up forever

Originator:joachimb
Number:rdar://6535050 Date Originated:28-Jan-2009 02:10 AM
Status:Closed Resolved:
Product:Mac OS X Product Version:10.5.6/9G55
Classification:Hang Reproducible:Always
 
28-Jan-2009 02:10 AM Joachim Bengtsson:
Summary:
Trying to read more than 4096 bytes in one go with NSFileHandle's readDataOfLength will lock up forever, never returning.

Steps to Reproduce:
1. Create a new Foundation tool.
2. Write the following in your impl file:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSFileHandle *fh = [[NSFileHandle alloc] initWithFileDescriptor:fileno(fopen("/dev/random", "r"))];
    
    NSLog(@"Reading 4096 bytes... This will succeed.");
    [fh readDataOfLength:4096];
    NSLog(@"Reading 4097 bytes... This will lock for infinity");
    [fh readDataOfLength:4097];
    NSLog(@"This will never be printed.");
    
    [fh closeFile];
    [fh release];

    [pool drain];
    return 0;
}

3. Compile and run

Expected Results:
4. See the output:
2009-01-28 10:55:06.164 NSFileHandleIsEvil[42953:10b] Reading 4096 bytes... This will succeed.
2009-01-28 10:55:06.229 NSFileHandleIsEvil[42953:10b] Reading 4097 bytes... This will lock for infinity
2009-01-28 10:55:06.230 NSFileHandleIsEvil[42953:10b] This will never be printed.

The Debugger has exited with status 0.

Actual Results:
2009-01-28 10:55:06.164 NSFileHandleIsEvil[42953:10b] Reading 4096 bytes... This will succeed.
2009-01-28 10:55:06.229 NSFileHandleIsEvil[42953:10b] Reading 4097 bytes... This will lock for infinity
< No more output, ever >

Regression:
Unknown

Notes:
This is a very serious bug. For example, the above program seems to try to read an infinite amount of data from /dev/random. This is so very wrong.

Blog entry on the subject: http://overooped.com/post/73721688/nsfilehandle-considered-harmful


'NSFileHandleIsEvil.m' and 'Vermillion.spx' were successfully uploaded

13-Jun-2009 01:46 AM Vanaja Pasumarthi :
We believe this issue has been addressed in Mac OS X SnowLeopard (Client), build 10A380.  Please verify with the build that you obtained at WWDC, and update this report with your results.

27-Jun-2009 07:46 AM Joachim Bengtsson:
This issue has been verified as resolved and can be closed.

Thanks!

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!