public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: cygwin-patches@cygwin.com
Subject: Re: fix off-by-one in dup2
Date: Thu, 05 Dec 2013 13:45:00 -0000	[thread overview]
Message-ID: <52A08372.7080402@redhat.com> (raw)
In-Reply-To: <20131204175108.GB2590@ednor.casa.cgf.cx>

[-- Attachment #1: Type: text/plain, Size: 2102 bytes --]

On 12/04/2013 10:51 AM, Christopher Faylor wrote:

>>>> One question, though.  Assuming start is == size, then the current code
>>>> in CVS extends the fd table by only 1.  If that happens often, the
>>>> current code would have to call ccalloc/memcpy/cfree a lot.  Wouldn't
>>>> it in fact be better to extend always by at least NOFILE_INCR, and to
>>>> extend by (1 + start - size) only if start is > size + NOFILE_INCR?
>>>> Something like
>>>>
>>>>  size_t extendby = (start >= size + NOFILE_INCR) ? 1 + start - size : NOFILE_INCR;
>>>>

Always increasing by a minimum of NOFILE_INCR is wrong in one case - we
should never increase beyond OPEN_MAX_MAX (currently 3200).  dup2(0,
3199) should succeed (unless it fails with EMFILE due to rlimit, but we
already know that our handling of setrlimit(RLIMIT_NOFILE) is still a
bit awkward); but dup2(0, 3200) must always fail with EBADF.  I think
the code in CVS is still wrong: we want to increase to the larger of the
value specified by the user or NOFILE_INCR to minimize repeated calloc,
but we also need to cap the increase to be at most OPEN_MAX_MAX
descriptors, to avoid having a table larger than what the rest of our
code base will support.

Not having NOFILE_INCR free slots after a user allocation is not fatal;
it means that the first allocation to a large number will not have tail
padding, but the next allocation to fd+1 will allocate NOFILE_INCR slots
rather than just one.  My original idea of MAX(NOFILE_INCR, start -
size) expresses that.

>>
>> That might be helpful.  Tcsh, for instance, always dup's it's std
>> descriptors to the new fds 15-19.  If it does so in this order, it would
>> have to call extend 5 times.
> 
> dtable.h:#define NOFILE_INCR    32
> 
> It shouldn't extend in that scenario.  The table starts with 32
> elements.

Rather, the table starts with 256 elements; which is why dup2 wouldn't
crash until dup'ing to 256 or greater before I started touching this.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]

  parent reply	other threads:[~2013-12-05 13:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-25 23:26 Eric Blake
2013-10-15 14:06 ` Christopher Faylor
2013-10-15 20:45   ` Yaakov (Cygwin/X)
2013-10-15 22:34     ` Christopher Faylor
2013-10-16  7:40       ` Yaakov (Cygwin/X)
2013-11-23 13:19   ` Eric Blake
2013-12-04  9:32 ` Corinna Vinschen
2013-12-04 11:36   ` Corinna Vinschen
2013-12-04 12:04     ` Corinna Vinschen
2013-12-04 17:00       ` Christopher Faylor
2013-12-04 17:23         ` Corinna Vinschen
2013-12-04 17:51           ` Christopher Faylor
2013-12-04 19:44             ` Corinna Vinschen
2013-12-05 13:45             ` Eric Blake [this message]
2013-12-05 19:56               ` Christopher Faylor

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=52A08372.7080402@redhat.com \
    --to=eblake@redhat.com \
    --cc=cygwin-patches@cygwin.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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).