From 96ef1b0eb2d408a4ac9f7cc1b7c1bfe53252715d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 22 Oct 2001 02:34:41 +0000 Subject: Added xmalloc - calls smb_panic on zero size or malloc fail. Added xmemdup - calls xmalloc. Made data_blob() call xmemdup. Defensive programming (I still hate the no error checking... :-). Jeremy. (This used to be commit 2cc262278f9d4892cf2485d7a73d88bc0e7559a8) --- source3/lib/util.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'source3/lib') diff --git a/source3/lib/util.c b/source3/lib/util.c index d3f07dd0a8..cd170ff3d3 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -1641,6 +1641,32 @@ int smb_mkstemp(char *template) #endif } +/***************************************************************** + malloc that aborts with smb_panic on fail or zero size. + *****************************************************************/ + +void *xmalloc(size_t size) +{ + void *p; + if (size == 0) + smb_panic("xmalloc called with zero size.\n"); + if ((p = malloc(size)) == NULL) + smb_panic("xmalloc malloc fail.\n"); + return p; +} + +/***************************************************************** + Memdup with smb_panic on fail. + *****************************************************************/ + +void *xmemdup(void *p, size_t size) +{ + void *p2; + p2 = xmalloc(size); + memcpy(p2, p, size); + return p2; +} + /***************************************************************** like strdup but for memory *****************************************************************/ @@ -1905,7 +1931,7 @@ DATA_BLOB data_blob(void *p, size_t length) return ret; } - ret.data = memdup(p, length); + ret.data = xmemdup(p, length); ret.length = length; return ret; } -- cgit