nsswitch: Fix memory leak in test_wbc_groups()
authorAndreas Schneider <asn@samba.org>
Wed, 22 Jun 2016 06:48:12 +0000 (08:48 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Fri, 24 Jun 2016 00:01:19 +0000 (02:01 +0200)
Found by cppcheck.

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Uri Simchoni <uri@samba.org>
nsswitch/libwbclient/tests/wbclient.c

index a4c1751db7d288fa4c3039557f423cb27bfabf8a..94a932f852675c2487da6bdbd84c15373ccd389b 100644 (file)
@@ -388,43 +388,80 @@ fail:
 
 static bool test_wbc_groups(struct torture_context *tctx)
 {
+       wbcErr ret = false;
        const char *domain_name = NULL;
        uint32_t num_groups;
-       const char **groups;
-       int i;
-       struct wbcInterfaceDetails *details;
+       const char **groups = NULL;
+       uint32_t i;
+       struct wbcInterfaceDetails *details = NULL;
+       char *domain = NULL;
+       char *name = NULL;
+       char *sid_string = NULL;
 
        torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
                              "%s", "wbcInterfaceDetails failed");
 
        domain_name = talloc_strdup(tctx, details->netbios_domain);
+       torture_assert_goto(tctx,
+                           domain_name != NULL,
+                           ret,
+                           fail,
+                           "Failed to allocate domain_name");
        wbcFreeMemory(details);
+       details = NULL;
 
-       torture_assert_wbc_ok(tctx, wbcListGroups(domain_name, &num_groups, &groups),
-                             "wbcListGroups in %s failed", domain_name);
-       torture_assert(tctx, !(num_groups > 0 && !groups),
-                      "wbcListGroups returned invalid results");
+       torture_assert_wbc_ok_goto_fail(tctx,
+                                       wbcListGroups(domain_name, &num_groups, &groups),
+                                       "wbcListGroups in %s failed",
+                                       domain_name);
+       torture_assert_goto(tctx,
+                           !(num_groups > 0 && !groups),
+                           ret,
+                           fail,
+                           "wbcListGroups returned invalid results");
 
        for (i=0; i < MIN(num_groups,100); i++) {
-
                struct wbcDomainSid sid;
                enum wbcSidType name_type;
-               char *domain;
-               char *name;
-               char *sid_string;
 
-               torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, groups[i], &sid, &name_type),
-                                     "wbcLookupName for %s failed", domain_name);
+               torture_assert_wbc_ok_goto_fail(tctx,
+                                               wbcLookupName(domain_name,
+                                                             groups[i],
+                                                             &sid,
+                                                             &name_type),
+                                               "wbcLookupName for %s failed",
+                                               domain_name);
                wbcSidToString(&sid, &sid_string);
-               torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
-                                     "wbcLookupSid of %s failed", sid_string);
+               torture_assert_wbc_ok_goto_fail(tctx,
+                                               wbcLookupSid(&sid,
+                                                            &domain,
+                                                            &name,
+                                                            &name_type),
+                                               "wbcLookupSid of %s failed",
+                                               sid_string);
+               torture_assert_goto(tctx,
+                                   name,
+                                   ret,
+                                   fail,
+                                   "wbcLookupSid returned no name");
+
+               wbcFreeMemory(domain);
+               domain = NULL;
+               wbcFreeMemory(name);
+               name = NULL;
                wbcFreeMemory(sid_string);
-               torture_assert(tctx, name,
-                       "wbcLookupSid returned no name");
+               sid_string = NULL;
        }
+
+       ret = true;
+fail:
+       wbcFreeMemory(details);
        wbcFreeMemory(groups);
+       wbcFreeMemory(domain);
+       wbcFreeMemory(name);
+       wbcFreeMemory(sid_string);
 
-       return true;
+       return ret;
 }
 
 static bool test_wbc_trusts(struct torture_context *tctx)