If compiling with Insure, mallocate a byte of memory and attach it to the
authorTim Potter <tpot@samba.org>
Thu, 4 Apr 2002 06:55:32 +0000 (06:55 +0000)
committerTim Potter <tpot@samba.org>
Thu, 4 Apr 2002 06:55:32 +0000 (06:55 +0000)
POLICY_HND structure when passing new handles back from the appropriate
cli_* functions.  When closing the policy handle free the memory.

Insure (and indeed other memory checkers) should detect handles that have
not been closed properly as memory leaks.  Unfortunately this can only be
done when the program terminates (set insure++.summarize leaks in your
.psrc file) rather than when the policy handle falls out of scope.

Looks like Jeremy has squished all the policy handle leaks at the moment
but more are bound to crop up later.
(This used to be commit 6dc80d625752f0a3ce6fd7b2278095529c6ec29f)

source3/include/rpc_misc.h
source3/libsmb/cli_lsarpc.c
source3/libsmb/cli_samr.c

index 7398b8ac57e431949f92f813d08bbbb4fc215997..e47853c2a22a905fee9159ca6b8164b06c6a54a7 100644 (file)
@@ -323,6 +323,18 @@ typedef struct lsa_policy_info
        uint16 data3;
        uint16 data4;
        uint8 data5[8];
+
+#ifdef __INSURE__
+
+       /* To prevent the leakage of policy handles mallocate a bit of
+          memory when a policy handle is created and free it when the
+          handle is closed.  This should cause Insure to flag an error
+          when policy handles are overwritten or fall out of scope without
+          being freed. */
+
+       char *marker;
+#endif
+
 } POLICY_HND;
 
 /*
index 832758df4ffc31fcbaa1b853a00bd37980685381..32168546082bd26b9cb50589c6c35bb88834f227 100644 (file)
@@ -99,6 +99,9 @@ NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *pol = r.pol;
+#ifdef __INSURE__
+               pol->marker = malloc(1);
+#endif
        }
 
  done:
@@ -160,6 +163,9 @@ NTSTATUS cli_lsa_open_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *pol = r.pol;
+#ifdef __INSURE__
+               pol->marker = (char *)malloc(1);
+#endif
        }
 
  done:
@@ -207,6 +213,9 @@ NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        /* Return output parameters */
 
        if (NT_STATUS_IS_OK(result = r.status)) {
+#ifdef __INSURE__
+               SAFE_FREE(pol->marker);
+#endif
                *pol = r.pol;
        }
 
@@ -1052,7 +1061,10 @@ NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        return result;
 }
 
-/** Fetch a DOMAIN sid. Does complete cli setup / teardown anonymously. */
+#if 0
+
+/** An example of how to use the routines in this file.  Fetch a DOMAIN
+    sid. Does complete cli setup / teardown anonymously. */
 
 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
 {
@@ -1150,4 +1162,6 @@ Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
        return ret;
 }
 
+#endif
+
 /** @} **/
index 53203e3d79ce36fa778d1b4156db90a2928b1306..85a7375f9922f48e74b1c3b8abcbd5b6d8a7c429 100644 (file)
@@ -69,6 +69,9 @@ NTSTATUS cli_samr_connect(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *connect_pol = r.connect_pol;
+#ifdef __INSURE__
+               connect_pol->marker = malloc(1);
+#endif
        }
 
  done:
@@ -114,6 +117,9 @@ NTSTATUS cli_samr_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        /* Return output parameters */
 
        if (NT_STATUS_IS_OK(result = r.status)) {
+#ifdef __INSURE__
+               SAFE_FREE(connect_pol->marker);
+#endif
                *connect_pol = r.pol;
        }
 
@@ -162,6 +168,9 @@ NTSTATUS cli_samr_open_domain(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *domain_pol = r.domain_pol;
+#ifdef __INSURE__
+               domain_pol->marker = malloc(1);
+#endif
        }
 
  done:
@@ -209,6 +218,9 @@ NTSTATUS cli_samr_open_user(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *user_pol = r.user_pol;
+#ifdef __INSURE__
+               user_pol->marker = malloc(1);
+#endif
        }
 
  done:
@@ -256,6 +268,9 @@ NTSTATUS cli_samr_open_group(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *group_pol = r.pol;
+#ifdef __INSURE__
+               group_pol->marker = malloc(1);
+#endif
        }
 
  done:
@@ -759,6 +774,9 @@ NTSTATUS cli_samr_open_alias(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(result = r.status)) {
                *alias_pol = r.pol;
+#ifdef __INSURE__
+               alias_pol->marker = malloc(1);
+#endif
        }
 
  done: