diff options
| author | Volker Lendecke <vl@samba.org> | 2025-07-29 14:50:40 +0200 |
|---|---|---|
| committer | Volker Lendecke <vl@samba.org> | 2025-09-02 08:08:29 +0000 |
| commit | 51d05da70e8461834396d361eead807127751b76 (patch) | |
| tree | 942909702f638ed54858f96025d55f68e10b3b7f /lib/talloc/talloc.c | |
| parent | 5b28822d9be81dae7a8341cc359c6e79498df241 (diff) | |
| download | samba-talloc-2.4.4.tar.gz samba-talloc-2.4.4.tar.bz2 samba-talloc-2.4.4.zip | |
lib: Add talloc_realloc_zero()talloc-2.4.4
Like talloc_realloc, zeroing out expanded memory
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Anoop C S <anoopcs@samba.org>
Diffstat (limited to 'lib/talloc/talloc.c')
| -rw-r--r-- | lib/talloc/talloc.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index 727abe77a24..ac3d26fcb52 100644 --- a/lib/talloc/talloc.c +++ b/lib/talloc/talloc.c @@ -2811,6 +2811,39 @@ _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, } /* + * realloc an array, checking for integer overflow in the array size + * and zero out potential additional memory + */ +_PUBLIC_ void *_talloc_realloc_array_zero(const void *ctx, + void *ptr, + size_t el_size, + unsigned count, + const char *name) +{ + size_t existing, newsize; + void *newptr = NULL; + + if (count >= MAX_TALLOC_SIZE / el_size) { + return NULL; + } + + existing = talloc_get_size(ptr); + newsize = el_size * count; + + newptr = _talloc_realloc(ctx, ptr, newsize, name); + if (newptr == NULL) { + return NULL; + } + + if (newsize > existing) { + size_t to_zero = newsize - existing; + memset_s(((char *)newptr) + existing, to_zero, 0, to_zero); + } + + return newptr; +} + +/* a function version of talloc_realloc(), so it can be passed as a function pointer to libraries that want a realloc function (a realloc function encapsulates all the basic capabilities of an allocation library, which is why this is useful) |
