From 9644b6cb50ec01c04a0d6ab17a8e39054fd8b0f8 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 28 Mar 2008 15:49:13 +0100 Subject: Add a talloc context parameter to current_timestring() to fix memleaks. current_timestring used to return a string talloced to talloc_tos(). When called by DEBUG from a TALLOC_FREE, this produced messages "no talloc stackframe around, leaking memory". For example when used from net conf. This also adds a temporary talloc context to alloc_sub_basic(). For this purpose, the exit strategy is slightly altered: a common exit point is used for success and failure. Michael (This used to be commit 16b5800d4e3a8b88bac67b2550d14e0aaaa302a9) --- source3/lib/debug.c | 10 ++++++---- source3/lib/substitute.c | 24 +++++++++++++++--------- source3/lib/time.c | 4 ++-- 3 files changed, 23 insertions(+), 15 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/debug.c b/source3/lib/debug.c index c4a0d1b47b..a76a8dbf53 100644 --- a/source3/lib/debug.c +++ b/source3/lib/debug.c @@ -1029,12 +1029,14 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line) /* Print it all out at once to prevent split syslog output. */ if( lp_debug_prefix_timestamp() ) { (void)Debug1( "[%s, %2d%s] ", - current_timestring(lp_debug_hires_timestamp()), level, - header_str); + current_timestring(debug_ctx(), + lp_debug_hires_timestamp()), + level, header_str); } else { (void)Debug1( "[%s, %2d%s] %s:%s(%d)\n", - current_timestring(lp_debug_hires_timestamp()), level, - header_str, file, func, line ); + current_timestring(debug_ctx(), + lp_debug_hires_timestamp()), + level, header_str, file, func, line ); } } diff --git a/source3/lib/substitute.c b/source3/lib/substitute.c index 6ecc3fc635..62dfdb56b5 100644 --- a/source3/lib/substitute.c +++ b/source3/lib/substitute.c @@ -548,6 +548,7 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name, fstring pidstr, vnnstr; char addr[INET6_ADDRSTRLEN]; const char *local_machine_name = get_local_machine_name(); + TALLOC_CTX *tmp_ctx = NULL; /* workaround to prevent a crash while looking at bug #687 */ @@ -561,12 +562,14 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name, DEBUG(0, ("alloc_sub_basic: Out of memory!\n")); return NULL; } - + + tmp_ctx = talloc_stackframe(); + for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { r = NULL; b = a_string; - + switch (*(p+1)) { case 'U' : r = strdup_lower(smb_name); @@ -581,7 +584,7 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name, if (r == NULL) { goto error; } - pass = Get_Pwnam_alloc(talloc_tos(), r); + pass = Get_Pwnam_alloc(tmp_ctx, r); if (pass != NULL) { a_string = realloc_string_sub( a_string, "%G", @@ -631,7 +634,7 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name, a_string = realloc_string_sub(a_string, "%R", remote_proto); break; case 'T' : - a_string = realloc_string_sub(a_string, "%T", current_timestring(False)); + a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False)); break; case 'a' : a_string = realloc_string_sub(a_string, "%a", @@ -669,17 +672,20 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name, p++; SAFE_FREE(r); - - if ( !a_string ) { - return NULL; + + if (a_string == NULL) { + goto done; } } - return a_string; + goto done; error: SAFE_FREE(a_string); - return NULL; + +done: + TALLOC_FREE(tmp_ctx); + return a_string; } /**************************************************************************** diff --git a/source3/lib/time.c b/source3/lib/time.c index f98e03197f..e5fd929d0f 100644 --- a/source3/lib/time.c +++ b/source3/lib/time.c @@ -687,7 +687,7 @@ int set_server_zone_offset(time_t t) Return the date and time as a string ****************************************************************************/ -char *current_timestring(bool hires) +char *current_timestring(TALLOC_CTX *ctx, bool hires) { fstring TimeBuf; struct timeval tp; @@ -739,7 +739,7 @@ char *current_timestring(bool hires) } #endif } - return talloc_strdup(talloc_tos(), TimeBuf); + return talloc_strdup(ctx, TimeBuf); } -- cgit