talloc_tos in shadow_copy2_insert_string

Andrew Klaassen clawsoon at yahoo.com
Tue Mar 27 16:35:01 MDT 2012


--- On Tue, 3/27/12, Jeremy Allison <jra at samba.org> wrote:

> Again, this is simple C semantics, nothing talloc related.
> 
> Your function:
> 
> static int do_the_clone_of_smb_fname(struct
> vfs_handle_struct *handle,
>                
> TALLOC_CTX *mem_ctx,
>                
> struct smb_filename *smb_fname,
>                
> struct smb_filename *clientFname)
> 
> is passed a *COPY* of the clientFname pointer (which
> is NULL), and then you talloc something and assign
> it into that copy of the pointer on the stack, which
> is discarded (although not freed) when the function
> returns.

Thinking back, I see where I went wrong: I did some early tests with talloc_asprintf_append() that led me to believe that the original pointer was being updated by the callee function.  But going back to those experiments I can see I got lucky (or unlucky, for the purposes of learning how things actually work) because I wasn't triggering a realloc.  Trigger a realloc with a large appended string and the original pointer still points to the original string while the copied pointer points to the new string, exactly as I'd expect from your explanation.

Thanks for taking the time to write a clear, concise explanation for me.

> I suggest you look up the details of pointer semantics in C.

I knew I should've read my copy of K+R ~before~ starting.  I'm surprised I've gotten this far without knowing what I'm doing (with "this far" being "compiling and crashing". :-)

> What you're trying to do is :
> 
> static int do_the_clone_of_smb_fname(struct
> vfs_handle_struct *handle,
>                
> TALLOC_CTX *mem_ctx,
>                
> struct smb_filename *smb_fname,
>                
> struct smb_filename **pp_clientFname)
> 
> Note the double ** denoting a double indirected pointer.
> Assign to *pp_clientFname inside the function and you'll
> get the talloc'ed value back.

If I've understood correctly, this will allow me to pass a copy of the address of the clientFname pointer to the callee.  Since the callee function now has the address of the pointer (rather than the address that the pointer points to), it can update the value of the pointer to point to any new memory addresses that it allocates.  When the callee is done, the copy of the pointer's address goes away, but the value of the pointer continues to point to the newly allocated memory.

> Also note that a talloc_free'd pointer is not automatically
> nulled.

Good to know.

Thanks again for taking the time to write these basic C explanations for me when you could easily just brush me off.  It's very much appreciated.

BTW, Greg Whynott, from way back in the SGI days (he said he used to log into your... Octane? Onyx?), says hi.

Andrew




More information about the samba-technical mailing list