This is the mail archive of the cygwin-developers@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: stack overflow bug in ofstream::operator<<


----Original Message----
>From: Christopher Faylor
>Sent: 28 June 2005 16:33

> On Tue, Jun 28, 2005 at 01:39:14PM +0200, Corinna Vinschen wrote:

>> We're using alloca/memcpy/write in writev so that writev is atomic.
>> If we would change that so that write is called multiple times, we'd
>> need to make write explicitely thread safe and each single write would
>> be slowed down.  We also can't use WriteFileGather here, unfortunately.
>> 
>> So, what we can do without slowing down write, is either to use malloc
>> instead of alloca, or use both, dependent on the number of bytes to be
>> written in relation to the size of the stack.
>> 
>> Analogous for readv.
> 
> We should probably just use malloc.
> 
> cgf

... perhaps only if the size is above a reasonable limit (for stack-based
objects, probably somewhere in the range 16kB and 256kB would be a suitable
dividing line), otherwise keep the alloca; that way, most cases will retain
the current efficiency, and software that wants to write 2Mb strings
probably isn't in the middle of a time-critical loop anyway!

#define STACK_MAX_OBJECT_SIZE 65536

  char *const buf = (char *) (tot > STACK_MAX_OBJECT_SIZE) ? malloc (tot) :
alloca (tot);
        .... snip ....
  int rv = write (buf, tot);
  if (tot > STACK_MAX_OBJECT_SIZE)
    free (buf);
  return rv;

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]