talloc: talloc_set_memlimit causes all reallocs to fail when used on pools. talloc_set_memlimit not enforced correctly on pools.

Arran Cudbard-Bell a.cudbardb at freeradius.org
Sat Oct 17 00:52:48 UTC 2020


>> An alternative that'd still satisfy our immediate need would be to have talloc_set_memlimit simply fail when someone tried to apply it to a pool (as you suggested), and add an optional flag that'd prevent allocations from occurring outside of the pool.
> 
> Please wrap your responses to 80 columns :-). Makes
> quoting your replies really hard :-).

Will do :)

> 
> What you're asking for is more complexity in an
> already overly complex part of the code (which
> to be honest I wasn't even sure people were
> using :-).
> 
> I think you can do what you need by allocating
> a pool as a talloc child of a context, and setting
> the memlimit on the that context.

I just tried this and it didn't work, the reallocs still fail.

This is likely because the limit needs to be the size of the pool plus 
headers.  I don't believe there's any way for the caller to know the size
of these headers, but maybe you know better :)

talloc_get_size() returns 0 when called on the ctx or the pool as the
docs suggest it should.

Do you have any idea how I could determine the correct value to
pass to talloc_set_memlimit?

-Arran

#include <talloc.h>
#include <stdio.h>

int main(int argc, char **argv)
{
        TALLOC_CTX *outer = talloc_init("test");
        TALLOC_CTX *pool = talloc_pool(outer, 1024);
        TALLOC_CTX *chunk;

        if (pool) {
                printf("Pool allocated\n");
        } else {
                printf("Pool allocation failed\n");
        }

        if (talloc_set_memlimit(outer, 1024) < 0) {
                printf("talloc_set_memlimit failed\n");
        } else {
               printf("talloc_set_memlimit success\n");
        }

        chunk = talloc_size(pool, 1);
        if (chunk) {
                printf("Chunk allocated\n");
        } else {
                printf("Chunk allocation failed\n");
        }

        chunk = talloc_realloc_size(pool, chunk, 2);
        if (chunk) {
                printf("Chunk realloced\n");
        } else {
                printf("Chunk realloc failed\n");
        }
}




More information about the samba-technical mailing list