[QUICK] talloc bugs

Rusty Russell rusty at rustcorp.com.au
Wed Jul 1 11:50:13 GMT 2009


On Mon, 29 Jun 2009 09:19:20 pm tridge at samba.org wrote:
> Hi Rusty,
>
>  > I'd like to see a simple example of where talloc_reference is required,
>  > so we can get less abstract in this discussion.
>
> yep, I've found the abstractness of the discussion unhelpful.
>
> Here are two fairly simple examples:
>
>  1) in source3/lib/util_tdb.c we have tdb_wrap_open() which is used to
>  allow us to share a common underlying tdb context between two users,
>  allowing for the illusion of being able to open a tdb more than once
>  in the same process. This is a classic case of reference counting.

This is a classic case of problematic references, too.  Ignoring the fact that 
TDB should handle multiple opens, there's nothing obviously wrong with:

	tdb = tdb_wrap_open(...);
	talloc_free(tdb);

Nor:
	tdb = tdb_wrap_open(...);
	talloc_steal(newctx, tdb);

Yet both are buggy, because the reference is *exposed*.  The right answer is 
to hide it:

util_tdb.h:
struct tdb_wrap {
	struct tdb_context *tdb;
	struct tdb_wrap_info *wi;
};

util_tdb.c:
struct tdb_wrap_info {
	struct tdb_wrap_info *next, *prev;
	const char *name; /* Redundant, use tdb_name */
	struct tdb_context *tdb;
};

Once this is done, it's a short hop to open-coding the reference count anyway 
rather than using talloc_reference.

I don't think references are a fundamental part of talloc: they are useful, 
and I think helpers to allow them could be great, but I'd rather see some 
explicit demarcation of what is reference counted.  See followup code.

Cheers,
Rusty.


More information about the samba-technical mailing list