On 3/17/10 3:39 PM, "Graham Leggett" <minfrin@sharp.fm> wrote:

>On 18 Mar 2010, at 12:18 AM, Ivan Novick wrote:

>> I am doing some testing with APR version 1.2.12
>>
>> When adding to a hash table using apr_hash_set ... If memory can not
>> be
>> allocated what is the expected behavior?
>>
>> I am seeing apr_hash_set calls expand_array.
>>
>> expand_array core dumps if memory can not be allocated.
>>
>> Is this expected?  Is there a way to get an error code for a failed
>> insert
>> to a table rather than a coredump?

>This is definitely wrong, APR should return APR_ENOMEM if memory
>cannot be allocated.

>More specifically, the APR pools code gives the caller (you) the
>choice of behaviour when the memory allocation fails. You can choose
>to have APR call a function of your choice, or you can choose APR to
>return APR_ENOMEM.

>httpd chooses to call a function that terminates the server child when
>out of memory, and as a result httpd makes no attempt to cater for out
>of memory conditions, but other callers of APR don't have to, and APR
>itself should definitely respect APR_ENOMEM.


When you say this wrong, do you mean the code is wrong or that the analysis of what the code does is wrong?

In the code below, if alloc_array returns NULL, this should core dump.  In fact this code returns void so I don’t see how it could be passing back information about failed memory allocation with a return code.

Cheers,
Ivan

/*
 * Expanding a hash table
 */

static void expand_array(apr_hash_t *ht)
{
    apr_hash_index_t *hi;
    apr_hash_entry_t **new_array;
    unsigned int new_max;

    new_max = ht->max * 2 + 1;
    new_array = alloc_array(ht, new_max);
    for (hi = apr_hash_first(NULL, ht); hi; hi = apr_hash_next(hi)) {
        unsigned int i = hi->this->hash & new_max;
        hi->this->next = new_array[i];
        new_array[i] = hi->this;
    }   
    ht->array = new_array;
    ht->max = new_max;
}