stack overflow bug in ofstream::operator<<

Corinna Vinschen corinna-cygwin@cygwin.com
Tue Jun 28 15:56:00 GMT 2005


On Jun 28 16:42, Dave Korn wrote:
> ... 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

I had something like that as a local patch but that's a bit dangerous.
Imagine a process using lots of stack space before calling writev and
there's only, say, 32K left.  It would potentially get a SEGV since
alloca is used while it would still work when using malloc.  Of course
malloc could fail for a process using lots of malloc'ed space.  However,
malloc should at least not SEGV but just return with a NULL pointer...

So, I guess I'll just check it in using malloc.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          mailto:cygwin@cygwin.com
Red Hat, Inc.



More information about the Cygwin-developers mailing list