[sword-devel] SWBuf potential problem

Daniel Glassey dglassey at gmail.com
Fri Jun 10 12:17:48 MST 2005


On 10/06/05, Troy A. Griffitts <scribe at crosswire.org> wrote:
> Hey Daniel,
>         Thanks for the catch, but we don't check for successful memory
> allocation anywhere in the engine.  I'm not sure what we'd do if we ran
> out of memory.  Gracefully degradating from such a state is a complex
> problem.  Also, our engine doesn't use try/catch/throw error handling,
> so throwing anything would cross a consistency line.  Note: not debating
> that we SHOULDN'T use exception error handling, only that we currently
> do not.

Fair enough to not throw as long as it does something. It is one thing
to not check and another thing to allow someone to overwrite bits of
memory that they shouldn't.
 
The *end = 0; is a _really_ bad idea if you aren't certain that that
memory has been allocated.

d


> Daniel Glassey wrote:
> > Hi,
> > While encountering other problems on my machine I've found a potential
> > problem with SWBuf::assureSize.
> >
> > It reallocs or mallocs the buffer but doesn't check that that has
> > succeeded before setting the 'end' of the buffer to 0.
> >
> > Patch attached that does a check. Thought I'd just check before checking it in.
> >
> > Regards,
> > Daniel
> >
> >
> > ------------------------------------------------------------------------
> >
> > Index: include/swbuf.h
> > ===================================================================
> > --- include/swbuf.h   (revision 1827)
> > +++ include/swbuf.h   (working copy)
> > @@ -58,6 +58,7 @@
> >                       long size = (end - buf);
> >                       checkSize += 128;
> >                       buf = (char *)((allocSize) ? realloc(buf, checkSize) : malloc(checkSize));
> > +                     if (!buf) throw("Failed to extend SWBuf buffer");
> >                       allocSize = checkSize;
> >                       end = (buf + size);
> >                       *end = 0;



More information about the sword-devel mailing list