From b06fc3b53c2f17cfa3f86ef1472a5778400c0fc9 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 27 Apr 2007 21:09:16 +0000 Subject: r22539: Added _strict varients of the talloc calls to return NULL on size == 0 varients. Jeremy. (This used to be commit 1ef269067ca501e2a4ded4ca8654c6a6cc26f385) --- source4/lib/talloc/talloc.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'source4/lib/talloc/talloc.c') diff --git a/source4/lib/talloc/talloc.c b/source4/lib/talloc/talloc.c index 028b44a8c7..8f7906d0d8 100644 --- a/source4/lib/talloc/talloc.c +++ b/source4/lib/talloc/talloc.c @@ -1087,6 +1087,28 @@ void *_talloc_zero(const void *ctx, size_t size, const char *name) } +/* + talloc and zero memory. + Strict version - returns NULL if size is zero. +*/ +void *_talloc_zero_strict(const void *ctx, size_t size, const char *name) +{ + void *p; + + if (unlikely(size == 0)) { + return NULL; + } + + p = _talloc_named_const(ctx, size, name); + + if (p) { + memset(p, '\0', size); + } + + return p; +} + + /* memdup with a talloc. */ @@ -1101,6 +1123,26 @@ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name return newp; } +/* + memdup with a talloc. + Strict version - returns NULL if size is zero. +*/ +void *_talloc_memdup_strict(const void *t, const void *p, size_t size, const char *name) +{ + void *newp; + + if (unlikely(size == 0)) { + return NULL; + } + + newp = _talloc_named_const(t, size, name); + if (likely(newp)) { + memcpy(newp, p, size); + } + + return newp; +} + /* strdup with a talloc */ @@ -1281,6 +1323,23 @@ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char return _talloc_named_const(ctx, el_size * count, name); } +/* + alloc an array, checking for integer overflow in the array size. + Strict version - returns NULL if count or el_size are zero. +*/ +void *_talloc_array_strict(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 */ @@ -1292,6 +1351,23 @@ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const return _talloc_zero(ctx, el_size * count, name); } +/* + alloc an zero array, checking for integer overflow in the array size + Strict version - returns NULL if count or el_size are zero. +*/ +void *_talloc_zero_array_strict(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); +} + /* realloc an array, checking for integer overflow in the array size @@ -1421,3 +1497,14 @@ int talloc_is_parent(const void *context, const void *ptr) } return 0; } + +/* + Talloc wrapper that returns NULL if size == 0. +*/ +void *talloc_strict(const void *context, size_t size, const char *name) +{ + if (unlikely(size == 0)) { + return NULL; + } + return _talloc_named_const(context, size, name); +} -- cgit