[PATCH] libc_r bug: successful close(2) sets errno to ENOTTY

Jacques A. Vidrine nectar at FreeBSD.org
Mon Nov 24 09:44:59 PST 2003


On Sun, Nov 23, 2003 at 04:14:08PM +0200, Enache Adrian wrote:
> $ cc close.c -o close && ./close
> 0
> 0
> 
> $ cc close.c -lc_r -o close && ./close
> 0
> 25
> 
> $ cat close.c
> #include <errno.h>
> main()
> {
>         int fd = open("/dev/null", 1);
>         printf("%d\n", errno);
>         close(fd);
>         printf("%d\n", errno);
> }
> 
> This confuses rather badly applications which assume errno is meaningful.

The application is broken.  You must only check errno if you get an
error indication from the library call.

  <URL:http://www.opengroup.org/onlinepubs/007904975/functions/errno.html>
  IEEE Std 1003.1, 2003 Edition says, in part:
  ``The value of errno should only be examined when it is indicated to
  be valid by a function's return value.''

Cheers,
-- 
Jacques Vidrine   NTT/Verio SME      FreeBSD UNIX       Heimdal
nectar at celabo.org jvidrine at verio.net nectar at freebsd.org nectar at kth.se


More information about the freebsd-current mailing list