From 4ab6a8ebb70bbd5d69ad1dc6196c936f01f5aaf7 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sun, 29 Apr 2007 00:09:22 +0000 Subject: r22564: Move the _strict -> _zeronull functions into lib/util.c and out of talloc at tridge's request. Jeremy. (This used to be commit da78488b86c464b6861d36398cca7524ad5906fe) --- source3/lib/util.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'source3/lib/util.c') diff --git a/source3/lib/util.c b/source3/lib/util.c index 1e64db38fc..9a22e89fe2 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -3227,3 +3227,102 @@ int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, i } return IVAL(ptr,off); } + +/**************************************************************** + talloc wrapper functions that guarentee a null pointer return + if size == 0. +****************************************************************/ + +#ifndef MAX_TALLOC_SIZE +#define MAX_TALLOC_SIZE 0x10000000 +#endif + +/* + * talloc and zero memory. + * - returns NULL if size is zero. + */ + +void *_talloc_zero_zeronull(const void *ctx, size_t size, const char *name) +{ + void *p; + + if (size == 0) { + return NULL; + } + + p = talloc_named_const(ctx, size, name); + + if (p) { + memset(p, '\0', size); + } + + return p; +} + +/* + * memdup with a talloc. + * - returns NULL if size is zero. + */ + +void *_talloc_memdup_zeronull(const void *t, const void *p, size_t size, const char *name) +{ + void *newp; + + if (size == 0) { + return NULL; + } + + newp = talloc_named_const(t, size, name); + if (newp) { + memcpy(newp, p, size); + } + + return newp; +} + +/* + * alloc an array, checking for integer overflow in the array size. + * - returns NULL if count or el_size are zero. + */ + +void *_talloc_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name) +{ + if (count >= MAX_TALLOC_SIZE/el_size) { + return NULL; + } + + if (el_size == 0 || count == 0) { + return NULL; + } + + return talloc_named_const(ctx, el_size * count, name); +} + +/* + * alloc an zero array, checking for integer overflow in the array size + * - returns NULL if count or el_size are zero. + */ + +void *_talloc_zero_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name) +{ + if (count >= MAX_TALLOC_SIZE/el_size) { + return NULL; + } + + if (el_size == 0 || count == 0) { + return NULL; + } + + return _talloc_zero(ctx, el_size * count, name); +} + +/* + * Talloc wrapper that returns NULL if size == 0. + */ +void *talloc_zeronull(const void *context, size_t size, const char *name) +{ + if (size == 0) { + return NULL; + } + return talloc_named_const(context, size, name); +} -- cgit