You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Sander Striker <st...@apache.org> on 2001/08/23 11:04:45 UTC
Pools behaviour, WAS: RE: cvs commit: apr-util/test testdbm.c
...
Ian>> @@ -176,17 +180,21 @@
Ian>> char *op;
Ian>> int n;
Ian>> char *line;
Ian>> + const char *use1;
Ian>> + const char *use2;
Ian>> #ifdef TIME
Ian>> long start;
Ian>> extern long time();
Ian>> #endif
Ian>>
Ian>> - if (apr_dbm_open(&db, file, act->flags, APR_OS_DEFAULT, pool)
Ian>> - != APR_SUCCESS)
Ian>> - oops("cannot open: %s", file);
Ian>>
Ian>> - if ((line = (char *) malloc(LINEMAX)) == NULL)
Ian>> - oops("%s: cannot get memory", "line alloc");
Ian>> + rv = apr_dbm_open(&db, file, act->flags, APR_OS_DEFAULT, pool);
Ian>> + if (rv != APR_SUCCESS)
Ian>> + oops(db, rv, "cannot open: %s", file);
Ian>> +
Ian>> + if ((line = (char *) apr_palloc(pool,LINEMAX)) == NULL) {
Ian>> + oops(NULL, APR_EGENERAL, "%s: cannot get memory", "line
alloc");
Ian>> + }
Greg> apr_palloc() will never return NULL. No need for such a complex test.
It won't? You mean that in httpd there is always an abortfunc present?
>From apr_pools.c:malloc_block():
blok = (union block_hdr *) DO_MALLOC(size + sizeof(union block_hdr));
if (blok == NULL) {
/* ### keep this fprintf here? */
fprintf(stderr, "Ouch! malloc failed in malloc_block()\n");
if (abortfunc != NULL) {
(void) (*abortfunc)(APR_ENOMEM);
}
return NULL;
}
And from apr_pools.c:apr_palloc():
blok = new_block(size, a->apr_abort);
a->last->h.next = blok;
a->last = blok;
#ifdef APR_POOL_DEBUG
blok->h.owning_pool = a;
#endif
#if APR_HAS_THREADS
if (alloc_mutex) {
apr_lock_release(alloc_mutex);
}
#endif
first_avail = blok->h.first_avail;
blok->h.first_avail += size;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return (void *) first_avail;
#endif
}
Oh, I see, you are saying that _without_ an abort
function it will segfault due to the marked line?
Is this reasonable behaviour? I mean a simple
change to:
if ((blok = new_block(size, a->apr_abort)) == NULL)
return NULL;
might be more intuitive than having it segfault in
apr_palloc IMHO.
Sander
Possible segfault in apr_pvsprintf
Posted by Sander Striker <st...@apache.org>.
Hi,
In addition to the possible segfault in apr_palloc
when no abort function is set and we're out of mem,
there is another possible segfault in apr_pvsprintf.
This one is even worse, because there is no abort
function.
Excerpt from apr_pools.c:psprintf_flush():
/* must try another blok */
#if APR_HAS_THREADS
apr_lock_acquire(alloc_mutex);
#endif
nblok = new_block(2 * cur_len, NULL);
#if APR_HAS_THREADS
apr_lock_release(alloc_mutex);
#endif
memcpy(nblok->h.first_avail, blok->h.first_avail, cur_len);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ps->vbuff.curpos = nblok->h.first_avail + cur_len;
/* save a byte for the NUL terminator */
ps->vbuff.endpos = nblok->h.endp - 1;
The code will segfault when out of mem occurs within psprintf_flush
at the moment at the marked line.
Sander
RE: Pools behaviour, WAS: RE: cvs commit: apr-util/test testdbm.c
Posted by Sander Striker <st...@apache.org>.
> On Thu, Aug 23, 2001 at 11:04:45AM +0200, Sander Striker wrote:
> > ...
> > Greg> apr_palloc() will never return NULL. No need for such a
> complex test.
> >
> > It won't? You mean that in httpd there is always an abortfunc present?
>
> There should be an abortfunc, yes. ISTR once looking and finding it wasn't
> set (but it should).
>
> APR and httpd and every other APR user that I'm aware of assume that
> apr_palloc and friends succeed or abort. Everything is designed and built
> that way.
>
> Thus: until we decide to change that assumption, it is useless clutter to
> check for NULL results.
Agreed, that was not the issue I was pointing out. httpd isn't doing NULL
checking for apr_palloc anywhere, that should be maintained. However,
this implies that an abort function should be set somewhere.
For applications not setting an abort function there should at least be
the oppurtunity to check for NULL results of apr_palloc.
Applications that set an abort functions can safely assume that apr_palloc
returns something valid or calls the abort function.
AFAIK httpd-2.0 _doesn't_ set an abort function. I believe Ryan said
something on this a few months back.
> > Oh, I see, you are saying that _without_ an abort
> > function it will segfault due to the marked line?
>
> I had no idea about that segfault. It is just a typical example of what I
> described above.
Not really. Pools don't enforce you to set an abort function. If it isn't
set the pools code shouldn't segfault.
> We can talk on and on about whether that is "appropriate" or not, but it
> isn't going to change in any near or mid term that I could imagine.
Well, the reason I am interested is because I am working on yet another
attempt to optimize the pools code. Justin ran some 'benchmarks' last
night,
but we need to run them again to be sure there is a performance increase.
If so I'll be posting a patch for review (it also removes the locking issues
we have regarding to the global free list).
In that new code I would like to do what is appropiate and return NULL
when we have no mem and no abort function.
Sander
Re: Pools behaviour, WAS: RE: cvs commit: apr-util/test testdbm.c
Posted by Greg Stein <gs...@lyra.org>.
On Thu, Aug 23, 2001 at 11:04:45AM +0200, Sander Striker wrote:
> ...
> Greg> apr_palloc() will never return NULL. No need for such a complex test.
>
> It won't? You mean that in httpd there is always an abortfunc present?
There should be an abortfunc, yes. ISTR once looking and finding it wasn't
set (but it should).
APR and httpd and every other APR user that I'm aware of assume that
apr_palloc and friends succeed or abort. Everything is designed and built
that way.
Thus: until we decide to change that assumption, it is useless clutter to
check for NULL results.
> Oh, I see, you are saying that _without_ an abort
> function it will segfault due to the marked line?
I had no idea about that segfault. It is just a typical example of what I
described above.
We can talk on and on about whether that is "appropriate" or not, but it
isn't going to change in any near or mid term that I could imagine.
Cheers,
-g
--
Greg Stein, http://www.lyra.org/