talloc_tos in shadow_copy2_insert_string

Jeremy Allison jra at samba.org
Tue Mar 27 12:01:40 MDT 2012


On Tue, Mar 27, 2012 at 10:03:17AM -0700, Andrew Klaassen wrote:
> 
> I read that code comment a few times but didn't fully understand it; thanks for the further explanation.
> 
> I've noticed this in my testing:
> 
> static void bar_alloc_string(char *barstring)
> {
>         barstring = talloc_asprintf(talloc_tos(), "%s/%s", "foo", "bar");
> }
> 
> static char *baz_alloc_string(void)
> {
>         char *bazstring;
>         bazstring = talloc_asprintf(talloc_tos(), "%s/%s", "foo", "bar");
>         return bazstring;
> }
> 
> static int vfs_tallocfoobar_stat(vfs_handle_struct *handle,
>                            struct smb_filename *smb_fname)
> {
>         char *barstring = NULL;
>         bar_alloc_string(barstring);
> 
>         DEBUG(0, ("bar string -> '%s'\n", barstring));
>         /* outputs "bar string -> '(null)'" */
>         /* Freed. */
> 
>         DEBUG(0, ("baz string -> '%s'\n", baz_alloc_string()));
>         /* outputs "baz string -> 'foo/baz'" */
>         /* Not freed. */
> }
> 
> I am right to guess based on this (before finding some proper documentation to read) that memory on a function's stack which is returned with "return" (rather than attached to a passed-in pointer) gets attached to the caller's stack, and that's why talloc isn't automatically freeing the baz result?

This is just straight C programming.

static void bar_alloc_string(char *barstring)
{
	barstring = talloc_asprintf(talloc_tos(), "%s/%s", "foo", "bar");
}

Overwrites the passed in barstring (which was NULL) with
the talloc'd value. This is then not returned (although
kept on the talloc_tos() pointer). It will be kept around
and only freed when the current talloc_tos() pointer
frame is freed.

Jeremy.


More information about the samba-technical mailing list