[PATCH 1/1] Add talloc_get_type_abort as a macro

Amin Azez azez at ufomechanic.net
Fri Feb 1 14:20:00 GMT 2008


This simpler patch supersedes the previous one, it allows the calling
context to define the abort action.

I really couldn't implement this in talloc.c, as there is no trivial way
of getting the error message formatted, output, and then aborting in a
way that did not impact other libtalloc clients. I spent a couple of
hours discussing it though!



Usage:

struct smb_read *io = talloc_get_type_abort(r, struct smb_read);

will abort if r is NULL or not "struct smb_read".
When aborting, a helpful error would be shown, like:

  Talloc type mismatch at xx.c:24, expected struct smb_read,
  got struct smbcli_request

Based on vlendec's talloc_get_type_abort, but implemented
as a macro so that there is no need to change the talloc
library api in any way at all, no need to link talloc.c and
the talloc library with an application specific abort method,
no need to define system wide talloc_abort handlers, and
so that the location of the offending type mismatch can be
more easily shown.

The abort behaviour defaults to
 #define TALLOC_ABORT(why) (puts(why),abort())

But the TALLOC_ABORT(why) may be defined before
talloc_get_type_abort is used. For smb applications
where smb_panic is linked, I suggest that:

 #define TALLOC_ABORT(why) smb_panic(why)

be defined in appropriate calling contexts in order to get
the error text shown in the usual way.

NOTE:
Because talloc_get_type_abort is a simple inline expression you
probably won't be able to define TALLOC_ABORT in terms
of macros that declare blocks, e.g. do { } while(0),
which would include samba's DEBUG macro.

The only bug is that the talloc_asprintf allocated error
message ought to be free'd by the ABORT function, but as that
function will typically quit the process it doesn't really matter.

Signed-off-by: Amin Azez <azez at ufomechanic.net>
---
 source/lib/talloc/talloc.h |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2ad75d0dec3fcbac3c1c48a707ba9de21aa74a8d.diff
Type: text/x-patch
Size: 973 bytes
Desc: not available
Url : http://lists.samba.org/archive/samba-technical/attachments/20080201/525e473e/2ad75d0dec3fcbac3c1c48a707ba9de21aa74a8d.bin


More information about the samba-technical mailing list