mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org, musl@lists.openwall.com
Cc: Rich Felker <dalias@aerifal.cx>, Eric Blake <eblake@redhat.com>,
	Isaac Dunham <idunham@lavabit.com>
Subject: Re: musl, fdopen test
Date: Tue, 19 Jun 2012 13:03:38 +0200	[thread overview]
Message-ID: <3323564.O6CdZoD93U@linuix> (raw)
In-Reply-To: <20120619025232.GL163@brightrain.aerifal.cx>

Rich Felker wrote:
> > >> Replacement of fdopen, because of
> > >>   checking whether fdopen sets errno... no
> > > 
> > > There was one bug here (failure to set errno when mode string was
> > > invalid) but I don't think that's the case gnulib was testing for. It
> > > seems gnulib wants an error for the "may fail" when the fd is invalid.

Indeed, the possibility that fdopen(invalid fd, ...) can succeed is supported
by POSIX. When looking at the gnulib documentation
  doc/posix-functions/fdopen.texi
and the unit test
  tests/test-fdopen.c
it appears that it was not the intent of gnulib to prohibit this behaviour.
Rather, musl is the first platform to exhibit this behaviour, and gnulib's
intent was to make sure that fdopen(invalid fd, ...)
  1. does not crash,
  2. sets errno when it fails.

Eric Blake replied:
> > The 'EBADF may fail' condition is rather weak.  And since glibc
> > guarantees a definite failure, it is nicer to program to the glibc
> > interface that guarantees immediate failure on a bad fd at fdopen() time
> > than it is to deal with the surprises that result when fdopen() succeeds
> > but later attempts to use the stream fail.  Perhaps it might be worth

The glibc documentation contains this warning:

     In some other systems, `fdopen' may fail to detect that the modes
     for file descriptor do not permit the access specified by
     `opentype'.  The GNU C library always checks for this.

So, I think few programmers will explicitly want to exploit this glibc
specific behaviour.

Rich Felker replied:
> The only real-world situation I can think of where you'd care that
> fdopen detect EBADF is when you've just called a function that
> allocates the file descriptor and directly passed it to fdopen without
> first checking the return value. For instance:
> 
> FILE *f = fdopen(open(pathname, O_RDWR|O_CLOEXEC), "rb+");
> 
> instead of:
> 
> int fd = open(pathname, O_RDWR|O_CLOEXEC);
> if (fd<0) goto error;
> FILE *f = fdopen(fd, "rb+");
> 
> The former is rather lazy programming, but maybe gnulib intends to
> support this kind of programming.

No, gnulib does not intend to encourage this kind of lazy programming.

> My thought in having musl skip the test is to maximize performance of
> fdopen, assuming you might be using it in a situation like on a newly
> accept()ed network connection where every syscall counts (think
> multi-threaded httpd). For read-only fdopen, no syscalls are needed,

Sounds reasonable.

Here's a proposed patch to remove gnulib's unintentional requirement.


2012-06-19  Bruno Haible  <bruno@clisp.org>

	fdopen: Allow implementations that don't reject invalid fd arguments.
	* m4/fdopen.m4 (gl_FUNC_FDOPEN): Let the test pass if fdopen(-1,...)
	succeeds.
	Reported by Rich Felker <dalias@aerifal.cx>.

--- m4/fdopen.m4.orig	Tue Jun 19 13:00:23 2012
+++ m4/fdopen.m4	Tue Jun 19 13:00:05 2012
@@ -1,4 +1,4 @@
-# fdopen.m4 serial 2
+# fdopen.m4 serial 3
 dnl Copyright (C) 2011-2012 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -25,10 +25,8 @@
   FILE *fp;
   errno = 0;
   fp = fdopen (-1, "r");
-  if (fp != NULL)
+  if (fp == NULL && errno == 0)
     return 1;
-  if (errno == 0)
-    return 2;
   return 0;
 }]])],
           [gl_cv_func_fdopen_works=yes],



  reply	other threads:[~2012-06-19 11:03 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20120609230541.47eac2de@newbook>
     [not found] ` <4FD55156.7050302@cs.ucla.edu>
     [not found]   ` <20120611182202.1ee4d019@newbook>
2012-06-17 22:49     ` musl bugs found through gnulib Bruno Haible
2012-06-17 23:54       ` Rich Felker
2012-06-18  8:21         ` Szabolcs Nagy
2012-06-18 13:02           ` John Spencer
2012-06-18 14:55             ` Rich Felker
2012-06-18 15:26               ` Szabolcs Nagy
2012-06-18 16:00                 ` Rich Felker
2012-06-19 13:26               ` John Spencer
2012-06-18  0:16       ` [musl] " idunham
2012-06-19  0:11       ` Rich Felker
2012-06-19  2:07         ` [musl] " Eric Blake
2012-06-19  2:52           ` Rich Felker
2012-06-19 11:03             ` Bruno Haible [this message]
2012-06-19 11:09               ` musl, fdopen test Jim Meyering
2012-06-20 20:52                 ` Bruno Haible
2012-06-19 10:45         ` musl, printf out-of-memory test Bruno Haible
2012-06-19 19:16           ` Rich Felker
2012-06-19 20:04             ` Bruno Haible
2012-06-19 20:08               ` Rich Felker
2012-06-19 21:17                 ` Bruno Haible
2012-06-20  1:52                   ` Rich Felker
2012-06-20  7:30                     ` Szabolcs Nagy
2012-06-20  9:35                     ` Bruno Haible
2012-06-20 11:00                       ` Jim Meyering
2012-06-21 19:58                         ` Tom Tromey
2012-06-20  3:04       ` Re: musl bugs found through gnulib Rich Felker
2012-06-20  4:10         ` [musl] " Eric Blake
2012-06-20 13:27           ` Rich Felker
2012-06-20  7:32         ` Szabolcs Nagy
2012-06-22 10:39         ` grantpt test Bruno Haible
2012-07-02 22:33         ` [musl] Re: musl bugs found through gnulib Pádraig Brady
2012-06-20 19:28       ` Rich Felker
2012-06-21  2:21         ` Rich Felker
2012-06-21  8:52           ` [musl] " Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3323564.O6CdZoD93U@linuix \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=dalias@aerifal.cx \
    --cc=eblake@redhat.com \
    --cc=idunham@lavabit.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).