Fixing klocwork bugs
authorZofia Abramowska <z.abramowska@samsung.com>
Thu, 21 Nov 2013 11:36:30 +0000 (12:36 +0100)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 30 Jan 2014 12:01:44 +0000 (13:01 +0100)
[Issue#] N/A
[Bug] Memory leaks, null pointer dereferrences.
[Cause] N/A
[Solution] Fixing memory leaks with C++ standard classes, checking
null values of pointers
[Verification] Build. Tests passes for libsmack, ss-password,
ss-client-smack and ss-dbus should not change for worse.

Change-Id: Ie0ab2ea7745118f1129f9eaef80b433cc4c3624e

tests/libsmack-tests/test_cases.cpp
tests/security-server-tests/security_server_tests_client_smack.cpp
tests/security-server-tests/security_server_tests_dbus.cpp
tests/security-server-tests/security_server_tests_password.cpp

index 92c54e6..23d0e74 100644 (file)
@@ -1370,15 +1370,12 @@ RUNNER_TEST_SMACK(smack11_saving_loading_rules)
 static void smack_set_another_label_for_self(void)
 {
     static int number = time(NULL);
-    int result;
-    char *smack_label;
 
     number++;
-    result = asprintf(&smack_label, "s%d", number);
-    RUNNER_ASSERT_MSG_BT(result > 0, "asprintf failed");
-    result = smack_set_label_for_self(smack_label);
+    std::string smack_label("s" + std::to_string(number));
+
+    int result = smack_set_label_for_self(smack_label.c_str());
     RUNNER_ASSERT_MSG_BT(result == 0, "smack_set_label_for_self(" << smack_label << ") failed");
-    free(smack_label);
 }
 
 static void smack_unix_sock_server(int sock)
index ae6ebf9..4202fe2 100644 (file)
@@ -953,13 +953,10 @@ RUNNER_TEST_NOSMACK(tc18_security_server_get_smacklabel_cookie_nosmack) {
     label_ss = security_server_get_smacklabel_cookie(cookie_ptr.get());
     RUNNER_ASSERT_MSG_BT(label_ss != NULL, "Error in getting label by cookie");
 
-    //Check if label_ss is correct, that is only one NULL character.
-    if (label_ss[0] != '\0') {
-        free(label_ss);
-        RUNNER_ASSERT_MSG_BT(label_ss[0] == '\0', "label_ss was not an empty string.");
-    }
-
+    std::string label(label_ss);
     free(label_ss);
+    RUNNER_ASSERT_MSG_BT(!label.empty(), "label_ss was not an empty string.");
+
 }
 
 ////////////////////
index 204d157..a4b9ae0 100644 (file)
@@ -128,31 +128,31 @@ RUNNER_MULTIPROCESS_TEST_SMACK(tc01_smack_context_from_DBus)
         dbus_pending_call_unref(pending);
 
         ret = dbus_message_iter_init(msg, &iter);
-        if (0 == ret) {
-            RUNNER_ASSERT_MSG_BT(0 == ret, "Message has no arguments");
-        } else {
-            dbus_message_iter_recurse(&iter, &var);
-            while (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_INVALID) {
-                dbus_message_iter_recurse(&var, &var_iter);
-                while(dbus_message_iter_get_arg_type(&var_iter) != DBUS_TYPE_INVALID) {
-                    dbus_message_iter_recurse(&var_iter, &var_value);
-                    switch(dbus_message_iter_get_arg_type(&var_value)) {
-                        case DBUS_TYPE_STRING:
-                            dbus_message_iter_get_basic(&var_value, &smack_context);
-                            break;
-                        default:
-                            ;
-                    }
-                    dbus_message_iter_next(&var_iter);
+        RUNNER_ASSERT_MSG_BT(0 != ret, "Message has no arguments");
+
+        dbus_message_iter_recurse(&iter, &var);
+
+        while (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_INVALID) {
+            dbus_message_iter_recurse(&var, &var_iter);
+            while(dbus_message_iter_get_arg_type(&var_iter) != DBUS_TYPE_INVALID) {
+                dbus_message_iter_recurse(&var_iter, &var_value);
+                switch(dbus_message_iter_get_arg_type(&var_value)) {
+                    case DBUS_TYPE_STRING:
+                        dbus_message_iter_get_basic(&var_value, &smack_context);
+                        break;
+                    default:
+                        ;
                 }
-                dbus_message_iter_next(&var);
+                dbus_message_iter_next(&var_iter);
             }
+            dbus_message_iter_next(&var);
         }
 
         // free reply and close connection
         dbus_message_unref(msg);
         dbus_connection_unref(conn);
 
+        RUNNER_ASSERT_BT(smack_context != NULL);
         ret = strcmp(smack_context, subject_parent);
         RUNNER_ASSERT_MSG_BT(0 == ret,
             "Context mismatch! context from dbus: " << smack_context);
@@ -315,6 +315,7 @@ RUNNER_MULTIPROCESS_TEST_NOSMACK(tc01_smack_context_from_DBus_nosmack)
         dbus_message_unref(msg);
         dbus_connection_unref(conn);
 
+        RUNNER_ASSERT_BT(smack_context != NULL);
         ret = strcmp(smack_context, subject_parent);
         RUNNER_ASSERT_MSG_BT(ret == 0, "Context mismatch. Context " << smack_context);
 
index 804dfa4..dd3ecb3 100644 (file)
@@ -305,17 +305,15 @@ RUNNER_TEST(tc14_security_server_set_pwd_current_pwd_too_long_input_param)
 
     // TEST
     sleep(1);
-    char* long_password = (char*) malloc(5001);
-    long_password[5000] = '\0';
-    memset(long_password, 'A', 5000);
-    ret = security_server_set_pwd(TEST_PASSWORD,long_password, 10, 10);
+    std::string lng_pwd(5000, 'A');
+    ret = security_server_set_pwd(TEST_PASSWORD,lng_pwd.c_str(), 10, 10);
     RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_INPUT_PARAM, "ret = " << ret);
 }
 
 /**
  * Check empty password.
  */
-RUNNER_TEST(tc15_security_server_chk_pwd_shortest_password)
+RUNNER_TEST(tc15_security_server_chk_pwd_empty_password)
 {
     int ret;
     unsigned int attempt, max_attempt, expire_sec;
@@ -607,10 +605,8 @@ RUNNER_TEST(tc27_security_server_chk_pwd_too_long_password)
     RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_SUCCESS, "ret = " << ret);
 
     // TEST
-    char* long_password = (char*) malloc(5001);
-    long_password[5000] = '\0';
-    memset(long_password, 'A', 5000);
-    ret = security_server_chk_pwd(long_password,  &attempt, &max_attempt, &expire_sec);
+    std::string lng_pwd(5000, 'A');
+    ret = security_server_chk_pwd(lng_pwd.c_str(),  &attempt, &max_attempt, &expire_sec);
     RUNNER_ASSERT_MSG_BT(ret == SECURITY_SERVER_API_ERROR_INPUT_PARAM, "ret = " << ret);
 }