off-by-one problem in dtable.cc?

Joe Buehler jbuehler@hekimian.com
Mon Jun 24 16:03:00 GMT 2002


The following code in dtable::dup2() determines whether the fd table
should be expanded, and how much

   if ((size_t) newfd >= size)
    {
      int inc_size = NOFILE_INCR * ((newfd + NOFILE_INCR - 1) / NOFILE_INCR) -
                     size;
      extend (inc_size);
    }

Consider:

NOFILE_INCR is #defined to be 32.  If size is 32, and newfd is 32, then
inc_size will be: 32 * ((32 + 32 - 1) / 32) - 32 == 0, so the fdtable
will not be expanded, and interesting things will undoubtedly ensue!

I think it should be:

   if ((size_t) newfd >= size)
    {
      int inc_size = NOFILE_INCR * (newfd / NOFILE_INCR + 1) - size;
      extend (inc_size);
    }

Joe Buehler




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list