Coverity fixes 26/319626/1 accepted/tizen/unified/20250219.035249 accepted/tizen/unified/x/20250221.042157
authorMichal Bloch <m.bloch@samsung.com>
Thu, 13 Feb 2025 13:50:42 +0000 (14:50 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 13 Feb 2025 16:42:25 +0000 (17:42 +0100)
Change-Id: I200d8d09571b35888ee543e8c58a3ec822b685db
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
packaging/sessiond.spec
src/service/src/fs_helpers.cpp
src/service/src/os_ops.cpp

index 53fd6477b8a2ab9139d729e61d380aac60c5cd92..0dc7a59f2e87bf288579d8377360f1ecf57d1a7a 100644 (file)
@@ -1,6 +1,6 @@
 Name:       sessiond
 Summary:    Service to manage subsessions
-Version:    0.7.8.0
+Version:    0.7.8.1
 Release:    1
 Group:      System/Management
 License:    MIT
index 8658a7d6986d5c51e2b9c0ce1da01cb46ab8c041..00e964975a44f18809480962b2c1f8d461d00f7b 100644 (file)
@@ -141,8 +141,8 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
                        throw std::system_error(EEXIST, std::generic_category(),
                                "Subsession directory already exists");
 
-               const auto tmp_subsession_path = fs::path(subsession_path)
-                       .replace_filename(".tmpnew"s + subsession_path.filename().native());
+               auto tmp_subsession_path = fs::path(subsession_path);
+               tmp_subsession_path.replace_filename(".tmpnew"s + subsession_path.filename().native());
 
                fs::create_directory(tmp_subsession_path);
 
@@ -200,7 +200,7 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
                                                              | fs::perms::group_read | fs::perms::group_exec;
 
                for (auto const& entry : fs::recursive_directory_iterator(source_path)) {
-                       const auto s_path = entry.path();
+                       const auto& s_path = entry.path();
 
                        /* Do string manipulation instead of `fs::relative(source_path, entry)`,
                         * because relative() resolves symlinks which gives different behaviour. */
index 3b20f0921470ded8c2a719844f7acfc18aa5efab..fa0861bec99e877214a71702b543f2b3019d816b 100644 (file)
@@ -34,20 +34,23 @@ using namespace std::string_literals;
 std::string OS::get_home_dir_by_user_id(const int uid)
 {
        auto max_buf_size = static_cast<ssize_t>(sysconf(_SC_GETPW_R_SIZE_MAX));
-       std::runtime_error err("Couldn't get home directory for session_uid=" + std::to_string(uid));
-
        if (max_buf_size <= 0)
-               throw err;
+               throw std::runtime_error("Couldn't acquire buffer size for `getpwuid_r` system call");
 
        std::unique_ptr<char[]> str_buf(new char[max_buf_size]);
        // N.B. `new` throws `std::bad_alloc` exception if it runs out of memory,
        // so there's no need to check if it's successful here
 
-       passwd pass_buf, *pass_ptr;
-       getpwuid_r(uid, &pass_buf, str_buf.get(), max_buf_size, &pass_ptr);
+       passwd pass_buf;
+       passwd *pass_ptr = NULL;
+       const int ret = getpwuid_r(uid, &pass_buf, str_buf.get(), max_buf_size, &pass_ptr);
+       if (ret < 0)
+               throw std::system_error(errno, std::generic_category()
+                       , "Couldn't get home directory for session_uid=" + std::to_string(uid));
 
        if (!pass_ptr)
-               throw err;
+               throw std::runtime_error("Couldn't get home directory (user does not exist) "
+                       "for session_uid=" + std::to_string(uid));
 
        return std::string(pass_ptr->pw_dir);
 }
@@ -60,12 +63,20 @@ int OS::get_gid_from_name(std::string_view group_name)
 
        std::unique_ptr<char[]> str_grp_buf(new char[max_grp_buf_size]);
 
-       group pass_grp_buf, *pass_grp_ptr;
-       getgrnam_r(group_name.data(), &pass_grp_buf, str_grp_buf.get(), max_grp_buf_size, &pass_grp_ptr);
-       if (!pass_grp_ptr)
-               throw std::runtime_error("Couldn't get Unix gid for `"s
+       group pass_grp_buf;
+       group *pass_grp_ptr = NULL;
+       const int ret = getgrnam_r(group_name.data(), &pass_grp_buf, str_grp_buf.get(), max_grp_buf_size, &pass_grp_ptr);
+       if (ret < 0)
+               throw std::system_error(errno, std::generic_category()
+                       ,"Couldn't get Unix gid for `"s
                        + group_name.data()
                        + "` group");
+
+       if (pass_grp_ptr == NULL)
+               throw std::runtime_error("Couldn't get Unix gid for `"s
+                       + group_name.data()
+                       + "` group - does not exist");
+
        return pass_grp_ptr->gr_gid;
 }