Add a talloc context parameter to current_timestring() to fix memleaks.
authorMichael Adam <obnox@samba.org>
Fri, 28 Mar 2008 14:49:13 +0000 (15:49 +0100)
committerMichael Adam <obnox@samba.org>
Fri, 28 Mar 2008 15:34:51 +0000 (16:34 +0100)
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
source3/lib/substitute.c
source3/lib/time.c
source3/libsmb/trusts_util.c
source3/rpcclient/cmd_spoolss.c
source3/smbd/change_trust_pw.c
source3/web/swat.c

index c4a0d1b47be36c5a61b5b54efe9b7d40d6fe51dd..a76a8dbf539ac562e1596d7f6d8f2611fdb294b8 100644 (file)
@@ -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 );
                }
        }
 
index 6ecc3fc63596071ad3baf6dda80dd742bfc81fbe..62dfdb56b56c3d4829decd223c493081ec3a68db 100644 (file)
@@ -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;
 }
 
 /****************************************************************************
index f98e03197f210315d52b2045e6985a58af4915f5..e5fd929d0f3a9af3d326f377361af70e5f3b9c8f 100644 (file)
@@ -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);
 }
 
 
index c079fb149a8bc40ee339627a42008f77efad174b..8c2f69cee364387fdb97f833820a88e81ed79fbb 100644 (file)
@@ -123,7 +123,7 @@ NTSTATUS trust_pw_change_and_store_it(struct rpc_pipe_client *cli, TALLOC_CTX *m
        
        if (NT_STATUS_IS_OK(nt_status)) {
                DEBUG(3,("%s : trust_pw_change_and_store_it: Changed password.\n", 
-                        current_timestring(False)));
+                        current_timestring(debug_ctx(), False)));
                /*
                 * Return the result of trying to write the new password
                 * back into the trust account file.
index 5e0f694cf97d5796a14e62b29f65d1c27f0e8ba5..7530ab191128e6e8fa2068bae95b4d933417c812 100644 (file)
@@ -1966,13 +1966,15 @@ static WERROR cmd_spoolss_setprinterdata(struct rpc_pipe_client *cli,
        PRINTER_INFO_CTR ctr;
        PRINTER_INFO_0 info;
        REGISTRY_VALUE value;
+       TALLOC_CTX *tmp_ctx = talloc_stackframe();
 
        /* parse the command arguements */
        if (argc < 5) {
                printf ("Usage: %s <printer> <string|binary|dword|multistring>"
                        " <value> <data>\n",
                        argv[0]);
-               return WERR_INVALID_PARAM;
+               result = WERR_INVALID_PARAM;
+               goto done;
        }
 
        slprintf(servername, sizeof(servername)-1, "\\\\%s", cli->cli->desthost);
@@ -2000,7 +2002,8 @@ static WERROR cmd_spoolss_setprinterdata(struct rpc_pipe_client *cli,
 
        if (value.type == REG_NONE) {
                printf("Unknown data type: %s\n", argv[2]);
-               return WERR_INVALID_PARAM;
+               result =  WERR_INVALID_PARAM;
+               goto done;
        }
 
        /* get a printer handle */
@@ -2019,7 +2022,7 @@ static WERROR cmd_spoolss_setprinterdata(struct rpc_pipe_client *cli,
         if (!W_ERROR_IS_OK(result))
                 goto done;
                
-       printf("%s\n", current_timestring(True));
+       printf("%s\n", current_timestring(tmp_ctx, True));
        printf("\tchange_id (before set)\t:[0x%x]\n", info.change_id);
 
        /* Set the printer data */
@@ -2105,11 +2108,12 @@ static WERROR cmd_spoolss_setprinterdata(struct rpc_pipe_client *cli,
         if (!W_ERROR_IS_OK(result))
                 goto done;
                
-       printf("%s\n", current_timestring(True));
+       printf("%s\n", current_timestring(tmp_ctx, True));
        printf("\tchange_id (after set)\t:[0x%x]\n", info.change_id);
 
 done:
        /* cleanup */
+       TALLOC_FREE(tmp_ctx);
        if (opened_hnd)
                rpccli_spoolss_close_printer(cli, mem_ctx, &pol);
 
index fc58e97ea78863f2114e34b59791196fdd83ec32..4773eeff8644d44e658ccdffe1198e6163cb44ac 100644 (file)
@@ -90,7 +90,7 @@ NTSTATUS change_trust_account_password( const char *domain, const char *remote_m
 failed:
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0,("%s : change_trust_account_password: Failed to change password for domain %s.\n", 
-                       current_timestring(False), domain));
+                       current_timestring(debug_ctx(), False), domain));
        }
        else
                DEBUG(5,("change_trust_account_password: sucess!\n"));
index bb418db04bf929a536b9d5698067d4ecaf084e9d..6d8f4cae06f9761f087815dc4b403214ebc0ea82 100644 (file)
@@ -438,11 +438,15 @@ static bool load_config(bool save_def)
 ****************************************************************************/
 static void write_config(FILE *f, bool show_defaults)
 {
+       TALLOC_CTX *ctx = talloc_stackframe();
+
        fprintf(f, "# Samba config file created using SWAT\n");
        fprintf(f, "# from %s (%s)\n", cgi_remote_host(), cgi_remote_addr());
-       fprintf(f, "# Date: %s\n\n", current_timestring(False));
+       fprintf(f, "# Date: %s\n\n", current_timestring(ctx, False));
        
        lp_dump(f, show_defaults, iNumNonAutoPrintServices);
+
+       TALLOC_FREE(ctx);
 }
 
 /****************************************************************************