From 5b44130afad1bb1764d986de3ef0e8e04b0e7357 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 27 Sep 2004 01:36:19 +0000 Subject: r2671: we're getting too many errors caused by the talloc_realloc() API not taking a context (so when you pass a NULL pointer you end up with memory in a top level context). Fixed it by changing the API to take a context. The context is only used if the pointer you are reallocing is NULL. (This used to be commit 8dc23821c9f54b2f13049b5e608a0cafb81aa540) --- source4/lib/charcnv.c | 8 ++------ source4/lib/registry/common/reg_interface.c | 2 +- source4/lib/registry/reg_backend_rpc/reg_backend_rpc.c | 2 +- source4/lib/socket/socket_ipv4.c | 4 ++-- source4/lib/talloc.c | 17 +++++++++-------- source4/lib/util_str.c | 5 +++-- 6 files changed, 18 insertions(+), 20 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/charcnv.c b/source4/lib/charcnv.c index 2ee2bd9bae..33c49504d8 100644 --- a/source4/lib/charcnv.c +++ b/source4/lib/charcnv.c @@ -208,11 +208,7 @@ ssize_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, outbuf = NULL; convert: destlen = destlen * 2; - if (outbuf == NULL) { - ob = talloc_array_p(ctx, char, destlen); - } else { - ob = (char *)talloc_realloc(outbuf, destlen); - } + ob = (char *)talloc_realloc(ctx, outbuf, destlen); if (!ob) { DEBUG(0, ("convert_string_talloc: realloc failed!\n")); talloc_free(outbuf); @@ -245,7 +241,7 @@ convert: destlen = destlen - o_len; /* +2 for mandetory null termination, UTF8 or UTF16 */ - *dest = (char *)talloc_realloc(ob,destlen+2); + *dest = (char *)talloc_realloc(ctx, ob, destlen+2); if (!*dest) { DEBUG(0, ("convert_string_talloc: out of memory!\n")); talloc_free(ob); diff --git a/source4/lib/registry/common/reg_interface.c b/source4/lib/registry/common/reg_interface.c index ec6188be71..ba92369194 100644 --- a/source4/lib/registry/common/reg_interface.c +++ b/source4/lib/registry/common/reg_interface.c @@ -190,7 +190,7 @@ WERROR reg_import_hive(struct registry_context *h, const char *backend, const ch /* Add hive to context */ h->num_hives++; - h->hives = talloc_realloc_p(h->hives, struct registry_hive *, h->num_hives); + h->hives = talloc_realloc_p(h, h->hives, struct registry_hive *, h->num_hives); h->hives[h->num_hives-1] = ret; return WERR_OK; diff --git a/source4/lib/registry/reg_backend_rpc/reg_backend_rpc.c b/source4/lib/registry/reg_backend_rpc/reg_backend_rpc.c index 1c887fc411..3dd73162be 100644 --- a/source4/lib/registry/reg_backend_rpc/reg_backend_rpc.c +++ b/source4/lib/registry/reg_backend_rpc/reg_backend_rpc.c @@ -90,7 +90,7 @@ WERROR rpc_list_hives (TALLOC_CTX *mem_ctx, const char *location, const char *cr int i = 0; *hives = talloc_p(mem_ctx, char *); for(i = 0; known_hives[i].name; i++) { - *hives = talloc_realloc_p(*hives, char *, i+2); + *hives = talloc_realloc_p(mem_ctx, *hives, char *, i+2); (*hives)[i] = talloc_strdup(mem_ctx, known_hives[i].name); } (*hives)[i] = NULL; diff --git a/source4/lib/socket/socket_ipv4.c b/source4/lib/socket/socket_ipv4.c index c98e5534ca..88bf611b67 100644 --- a/source4/lib/socket/socket_ipv4.c +++ b/source4/lib/socket/socket_ipv4.c @@ -184,7 +184,7 @@ static NTSTATUS ipv4_tcp_accept(struct socket_context *sock, struct socket_conte } static NTSTATUS ipv4_tcp_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx, - DATA_BLOB *blob, size_t wantlen, uint32_t flags) + DATA_BLOB *blob, size_t wantlen, uint32_t flags) { ssize_t gotlen; void *buf; @@ -235,7 +235,7 @@ static NTSTATUS ipv4_tcp_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx, } blob->length = gotlen; - blob->data = talloc_realloc(buf, gotlen); + blob->data = talloc_realloc(mem_ctx, buf, gotlen); if (!blob->data) { return NT_STATUS_NO_MEMORY; } diff --git a/source4/lib/talloc.c b/source4/lib/talloc.c index 8193f9c384..7266ff8a45 100644 --- a/source4/lib/talloc.c +++ b/source4/lib/talloc.c @@ -287,9 +287,10 @@ int talloc_free(void *ptr) /* - A talloc version of realloc + A talloc version of realloc. The context argument is only used if + ptr is NULL */ -void *_talloc_realloc(void *ptr, size_t size, const char *name) +void *_talloc_realloc(void *context, void *ptr, size_t size, const char *name) { struct talloc_chunk *tc; void *new_ptr; @@ -302,7 +303,7 @@ void *_talloc_realloc(void *ptr, size_t size, const char *name) /* realloc(NULL) is equavalent to malloc() */ if (ptr == NULL) { - return talloc_named_const(NULL, size, name); + return talloc_named_const(context, size, name); } tc = talloc_chunk_from_ptr(ptr); @@ -550,7 +551,7 @@ static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); static char *talloc_vasprintf_append(char *s, - const char *fmt, va_list ap) + const char *fmt, va_list ap) { int len, s_len; va_list ap2; @@ -564,7 +565,7 @@ static char *talloc_vasprintf_append(char *s, } len = vsnprintf(NULL, 0, fmt, ap2); - s = talloc_realloc(s, s_len + len+1); + s = talloc_realloc(NULL, s, s_len + len+1); if (!s) return NULL; VA_COPY(ap2, ap); @@ -607,13 +608,13 @@ void *talloc_array(void *ctx, size_t el_size, uint_t count, const char *name) /* realloc an array, checking for integer overflow in the array size */ -void *talloc_realloc_array(void *ptr, size_t el_size, uint_t count, const char *name) +void *talloc_realloc_array(void *ctx, void *ptr, size_t el_size, uint_t count, const char *name) { if (count == 0 || count >= MAX_TALLOC_SIZE/el_size) { return NULL; } - ptr = talloc_realloc(ptr, el_size * count); + ptr = talloc_realloc(ctx, ptr, el_size * count); if (ptr) { talloc_set_name_const(ptr, name); } @@ -632,5 +633,5 @@ void *talloc_ldb_alloc(void *context, void *ptr, size_t size) talloc_free(ptr); return NULL; } - return talloc_realloc(ptr, size); + return talloc_realloc(context, ptr, size); } diff --git a/source4/lib/util_str.c b/source4/lib/util_str.c index f8aadf8f59..0e58face16 100644 --- a/source4/lib/util_str.c +++ b/source4/lib/util_str.c @@ -1428,8 +1428,9 @@ BOOL add_string_to_array(TALLOC_CTX *mem_ctx, { char *dup_str = talloc_strdup(mem_ctx, str); - *strings = talloc_realloc(*strings, - ((*num)+1) * sizeof(**strings)); + *strings = talloc_realloc_p(mem_ctx, + *strings, + const char *, ((*num)+1)); if ((*strings == NULL) || (dup_str == NULL)) return False; -- cgit