talloc_tos in shadow_copy2_insert_string

Volker Lendecke Volker.Lendecke at SerNet.DE
Tue Mar 27 09:23:52 MDT 2012


On Tue, Mar 27, 2012 at 08:16:30AM -0700, Richard Sharpe wrote:
> > In vfs_shadow_copy2.c, function shadow_copy2_insert_string, the returned string hangs
> > off a talloc_tos() context.
> >
> > Doesn't this mean that the string will be deallocated before it gets back to the calling
> > function, when the callee function's stack is destroyed?
> >
> > This is what the code has:
> >
> >        return talloc_asprintf(talloc_tos(), "/%s/%s",
> >                            ..............)
> >
> > ...and this is what I was expecting to see:
> >
> >        return talloc_asprintf(mem_ctx, "/%s/%s",
> >                            ..............)
> >
> > ...since mem_ctx is passed to the function as an available talloc context (but never used).
> >
> > What am I missing?
> 
> My understanding was that things allocated with a talloc_tos() context
> are cleaned up when the current SMB request is finished with.
> 
> However, it would be useful to find out exactly what the meaning of
> talloc_tos() is.

>From talloc_stackframe.h:

/*                                                                              
 * Implement a stack of talloc frames.                                          
 *                                                                              
 * When a new talloc stackframe is allocated with
 * talloc_stackframe(), then     
 * the TALLOC_CTX returned with talloc_tos() is reset to
 * that new               
 * frame. Whenever that stack frame is TALLOC_FREE()'ed,
 * then the reverse       
 * happens: The previous talloc_tos() is restored.                              
 *                                                                              
 * This API is designed to be robust in the sense that if
 * someone forgets to    
 * TALLOC_FREE() a stackframe, then the next outer one
 * correctly cleans up and  
 * resets the talloc_tos().                                                     
 *                                                                              
 */

The intended use is that talloc_tos() is used for anything
within a function. Returning something on talloc_tos() is
not the best style, because it means that this object will
go away once the surrounding talloc_stackframe() goes. It
will work okay if used in a controlled manner, but depending
on local circumstances alternatives might be more
understandable.

With best regards,

Volker Lendecke

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de, mailto:kontakt at sernet.de


More information about the samba-technical mailing list