stack overflow bug in ofstream::operator<<
Corinna Vinschen
corinna-cygwin@cygwin.com
Thu Jun 30 15:33:00 GMT 2005
On Jun 30 10:12, Christopher Faylor wrote:
> On Thu, Jun 30, 2005 at 03:56:51PM +0200, Corinna Vinschen wrote:
> >On Jun 30 15:54, Corinna Vinschen wrote:
> >> Ok. I take it then that you don't like the way I solved it. Implementing
> >> some alloca like function with exception handling is certainly more i386
> >> assembler than I understand.
> >
> >Well, my solution would work as a replacement, too:
> >
> >#define ALLOCA(siz) \
> > ({ \
> > extern unsigned long _size_of_stack_reserve__; \
> > register char *_curstack __asm__ ("%esp"); \
> > size_t size = (siz); \
> > ((size_t) ((_curstack - (_tlsbase \
> > - (size_t) &_size_of_stack_reserve__) \
> > ) / 65536) * 65536 > size) ? alloca (size) : NULL;\
> > })
>
> I don't think that _size_of_stack_reserve__ is correct since there is no
> guarantee that a stack has allocated that much space. As Dave said, I
> think that this should be something like (untested):
>
> inline void * __attribute__ ((always_inline))
> alloca_check (size_t sz)
> {
> char *esp __asm__ ("%esp");
> return (esp - sz) >= _tlstop ? alloca (sz) : NULL;
> }
>
> although I don't know if the always_inline will work if the inline
> function calls alloca. If not, then it would need to be a macro,
> similar to the way you implemented it.
It seems, neither _tlstop nor __size_of_stack_reserve__ are the right
solution. _tlstop is just the top of the commited stack region,
the value of __size_of_stack_reserve__ is the one linked into the
DLL, not the value used when creating the application. So, if somebody
linked an application with a smaller stacksize, the above code simply
SEGV's as before, so the gain is about zero.
If nobody complains within the next hour, I'll fix writev/readv by just
using malloc instead of alloca.
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