Fix issues raised by static analysis 11/193811/1
authorKonrad Lipinski <k.lipinski2@partner.samsung.com>
Mon, 26 Nov 2018 13:43:37 +0000 (14:43 +0100)
committerKonrad Lipinski <k.lipinski2@partner.samsung.com>
Mon, 26 Nov 2018 13:43:37 +0000 (14:43 +0100)
Change-Id: I8d8877f933335bf03511264576e15e75896e7411

src/client/client-security-manager.cpp
src/license-manager/agent/main.cpp
src/server/main/socket-manager.cpp
test/test_privilege_db_migration.cpp

index 95c41dc..cef785c 100644 (file)
@@ -1427,15 +1427,25 @@ static lib_retcode get_app_and_pkg_id_from_smack_label(
         return SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT;
     }
 
-    if (app_name && !appNameString.empty() && !(*app_name = strdup(appNameString.c_str()))) {
+    char *appName = nullptr;
+    if (app_name && !appNameString.empty() && !(appName = strdup(appNameString.c_str()))) {
         LogError("Memory allocation in strdup failed.");
         return SECURITY_MANAGER_ERROR_MEMORY;
     }
 
-    if (pkg_name && !(*pkg_name = strdup(pkgNameString.c_str()))) {
-        LogError("Memory allocation in strdup failed.");
-        return SECURITY_MANAGER_ERROR_MEMORY;
+    char *pkgName = nullptr;
+    if (pkg_name) {
+        if (!(pkgName = strdup(pkgNameString.c_str()))) {
+            free(appName);
+            LogError("Memory allocation in strdup failed.");
+            return SECURITY_MANAGER_ERROR_MEMORY;
+        }
+        *pkg_name = pkgName;
     }
+
+    if (app_name)
+        *app_name = appName;
+
     return SECURITY_MANAGER_SUCCESS;
 }
 
index 1b60dd7..e5035d9 100644 (file)
 #include <agent.h>
 
 static LicenseManager::Agent *s_agentPtr = nullptr;
+class SigAgentPtrSetter {
+public:
+    explicit SigAgentPtrSetter(LicenseManager::Agent *p) {
+        s_agentPtr = p;
+    }
+    ~SigAgentPtrSetter() {
+        s_agentPtr = nullptr;
+    }
+};
 
 void kill_handler(int sig UNUSED) {
     ALOGD("License manager service is going down now");
@@ -68,7 +77,7 @@ int main(int, char **) {
             ALOGE("cynara initialization failed");
             return -1;
         }
-        s_agentPtr = &agent;
+        SigAgentPtrSetter sigAgentPtrSetter{&agent};
         ret = sd_notify(0, "READY=1");
         if (ret == 0) {
             ALOGW("Agent was not configured to notify its status");
@@ -76,9 +85,7 @@ int main(int, char **) {
             ALOGE("sd_notify failed: [" << ret << "]");
         }
         agent.mainLoop();
-        s_agentPtr = nullptr;
     } catch (const std::exception &e) {
-        s_agentPtr = nullptr;
         std::string error = e.what();
         ALOGE("Exception: %s", error.c_str());
     }
index 885b7e3..ec0d6f7 100644 (file)
@@ -497,8 +497,8 @@ int SocketManager::CreateDomainSocketHelp(
 {
     int sockfd;
 
-    if (desc.serviceHandlerPath.size() >= sizeof(static_cast<sockaddr_un*>(0)->sun_path) /
-                                         sizeof(decltype(desc.serviceHandlerPath)::value_type)) {
+    static_assert(1 == sizeof(*desc.serviceHandlerPath.c_str()));
+    if (desc.serviceHandlerPath.size() >= sizeof(static_cast<sockaddr_un*>(0)->sun_path)) {
         LogError("Service handler path too long: " << desc.serviceHandlerPath.size());
         ThrowMsg(Exception::InitFailed,
                  "Service handler path too long: " << desc.serviceHandlerPath.size());
index ba450ac..b06b0fd 100644 (file)
@@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(T1510_loader_output) {
     BOOST_REQUIRE(SECURITY_MANAGER_SUCCESS == FS::overwriteFile(PRIVILEGE_DB_EXAMPLE_RULES, TEST_DB_PATH));
     BOOST_REQUIRE(!system(TEST_RULES_LOADER_CMD " | LC_ALL=C sort > /tmp/out"));
     BOOST_REQUIRE(fileContentsSame("/tmp/out", PRIVILEGE_DB_EXAMPLE_RULES_OUTPUT));
-    remove("/tmp/out");
+    BOOST_REQUIRE(!remove("/tmp/out"));
 }
 
 BOOST_AUTO_TEST_CASE(T1570_fallback_canonicity) {