cvs commit: src/lib/libc/stdio fdopen.c fopen.c freopen.c

John Baldwin jhb at freebsd.org
Wed Feb 27 20:18:55 UTC 2008


On Wednesday 27 February 2008 02:02:03 pm John Baldwin wrote:
> jhb         2008-02-27 19:02:02 UTC
> 
>   FreeBSD src repository
> 
>   Modified files:
>     lib/libc/stdio       fdopen.c fopen.c freopen.c 
>   Log:
>   File descriptors are an int, but our stdio FILE object uses a short to 
hold
>   them.  Thus, any fd whose value is greater than SHRT_MAX is handled
>   incorrectly (the short value is sign-extended when converted to an int).
>   An unpleasant side effect is that if fopen() opens a file and gets a
>   backing fd that is greater than SHRT_MAX, fclose() will fail and the file
>   descriptor will be leaked.  Better handle this by fixing fopen(), 
fdopen(),
>   and freopen() to fail attempts to use a fd greater than SHRT_MAX with
>   EMFILE.
>   
>   At some point in the future we should look at expanding the file 
descriptor
>   in FILE to an int, but that is a bit complicated due to ABI issues.

To reproduce, open 32k fd's and then invoke gethostbyname() with 'files' 
enabled in nsswitch.conf for host lookups.  A fd on /etc/hosts will be leaked 
for each invocation of gethostbyname().

-- 
John Baldwin


More information about the cvs-src mailing list