nsswitch: Fix memory leak in test_wbc_users()
authorAndreas Schneider <asn@samba.org>
Wed, 22 Jun 2016 06:25:54 +0000 (08:25 +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 41a581e488bf5e5ae03c3bb1fd5317f79536f932..a4c1751db7d288fa4c3039557f423cb27bfabf8a 100644 (file)
@@ -286,57 +286,104 @@ static bool test_wbc_users(struct torture_context *tctx)
 {
        const char *domain_name = NULL;
        uint32_t num_users;
-       const char **users;
-       int i;
-       struct wbcInterfaceDetails *details;
+       const char **users = NULL;
+       uint32_t i;
+       struct wbcInterfaceDetails *details = NULL;
+       struct wbcDomainSid *sids = NULL;
+       char *domain = NULL;
+       char *name = NULL;
+       char *sid_string = NULL;
+       wbcErr ret = false;
 
        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, wbcListUsers(domain_name, &num_users, &users),
-               "%s", "wbcListUsers failed");
-       torture_assert(tctx, !(num_users > 0 && !users),
-               "wbcListUsers returned invalid results");
-
-       for (i=0; i < MIN(num_users,100); i++) {
+       torture_assert_wbc_ok_goto_fail(tctx,
+                                       wbcListUsers(domain_name, &num_users, &users),
+                                       "%s",
+                                       "wbcListUsers failed");
+       torture_assert_goto(tctx,
+                           !(num_users > 0 && !users),
+                           ret,
+                           fail,
+                           "wbcListUsers returned invalid results");
 
-               struct wbcDomainSid sid, *sids;
+       for (i = 0; i < MIN(num_users, 100); i++) {
+               struct wbcDomainSid sid;
                enum wbcSidType name_type;
-               char *domain;
-               char *name;
-               char *sid_string;
                uint32_t num_sids;
 
-               torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, users[i], &sid, &name_type),
-                                     "wbcLookupName of %s failed", users[i]);
-               torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
-                                        "wbcLookupName expected WBC_SID_NAME_USER");
+               torture_assert_wbc_ok_goto_fail(tctx,
+                                               wbcLookupName(domain_name, users[i], &sid, &name_type),
+                                               "wbcLookupName of %s failed",
+                                               users[i]);
+               torture_assert_int_equal_goto(tctx,
+                                             name_type, WBC_SID_NAME_USER,
+                                             ret,
+                                             fail,
+                                             "wbcLookupName expected WBC_SID_NAME_USER");
                wbcSidToString(&sid, &sid_string);
-               torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
-                                     "wbcLookupSid of %s failed", sid_string);
-               torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
-                                        "wbcLookupSid of expected WBC_SID_NAME_USER");
-               torture_assert(tctx, name,
-                       "wbcLookupSid returned no name");
+               torture_assert_wbc_ok_goto_fail(tctx,
+                                               wbcLookupSid(&sid,
+                                                            &domain,
+                                                            &name,
+                                                            &name_type),
+                                               "wbcLookupSid of %s failed",
+                                               sid_string);
+               torture_assert_int_equal_goto(tctx,
+                                             name_type, WBC_SID_NAME_USER,
+                                             ret,
+                                             fail,
+                                             "wbcLookupSid of expected WBC_SID_NAME_USER");
+               torture_assert_goto(tctx,
+                                   name,
+                                   ret,
+                                   fail,
+                                   "wbcLookupSid returned no name");
                wbcFreeMemory(domain);
+               domain = NULL;
                wbcFreeMemory(name);
-               torture_assert_wbc_ok(tctx, wbcLookupUserSids(&sid, true, &num_sids, &sids),
-                       "wbcLookupUserSids of %s failed", sid_string);
-               torture_assert_wbc_ok(
-                       tctx, wbcGetDisplayName(&sid, &domain, &name,
-                                               &name_type),
-                       "wbcGetDisplayName of %s failed", sid_string);
+               name = NULL;
+
+               torture_assert_wbc_ok_goto_fail(tctx,
+                                               wbcLookupUserSids(&sid, true, &num_sids, &sids),
+                                               "wbcLookupUserSids of %s failed", sid_string);
+               torture_assert_wbc_ok_goto_fail(tctx,
+                                               wbcGetDisplayName(&sid,
+                                                                 &domain,
+                                                                 &name,
+                                                                 &name_type),
+                                               "wbcGetDisplayName of %s failed",
+                                               sid_string);
                wbcFreeMemory(domain);
+               domain = NULL;
                wbcFreeMemory(name);
+               name = NULL;
                wbcFreeMemory(sids);
+               sids = NULL;
                wbcFreeMemory(sid_string);
+               sid_string = NULL;
        }
+
+       ret = true;
+fail:
+       wbcFreeMemory(details);
        wbcFreeMemory(users);
+       wbcFreeMemory(domain);
+       wbcFreeMemory(name);
+       wbcFreeMemory(sids);
+       wbcFreeMemory(sid_string);
 
-       return true;
+       return ret;
 }
 
 static bool test_wbc_groups(struct torture_context *tctx)