Fix issue detected by static analysis tool 52/316252/1
authorInhong Han <inhong1.han@samsung.com>
Fri, 16 Aug 2024 07:36:42 +0000 (16:36 +0900)
committerInhong Han <inhong1.han@samsung.com>
Fri, 16 Aug 2024 07:36:42 +0000 (16:36 +0900)
Change-Id: I57ae32731a2be09560a33e27f3ab2a6d14795638

ism/modules/config/scim_simple_config.cpp
ism/modules/imengine/scim_socket_imengine.cpp
ism/src/scim_global_config.cpp
ism/src/scim_utility.cpp
ism/src/scim_utility.h

index 7200b9c..1ef387b 100644 (file)
@@ -423,6 +423,8 @@ SimpleConfig::flush()
         // Reload config to ensure user made modification won't lost.
         load_all_config ();
 
+        if (!scim_is_symlink(userconf.c_str ())) return false;
+
         std::ofstream os (userconf.c_str ());
         if (!os) return false;
 
index 7e26f5b..2a52d1d 100644 (file)
@@ -337,18 +337,20 @@ SocketIMEngineGlobal::load_icon (const String &icon)
 
         SCIM_DEBUG_IMENGINE(1) << "Creating temporary icon file: " << tempfile << "\n";
 
-        std::ofstream os (tempfile.c_str ());
-
-        if (os) {
-            os.write (bufptr, (std::streamsize)filesize);
-            os.close ();
-
-            // Check if the file is written correctly.
-            if (scim_load_file (tempfile, 0) == filesize) {
-                m_icon_repository [icon] = tempfile;
-                local_icon = tempfile;
-            } else {
-                unlink (tempfile.c_str ());
+        if (!scim_is_symlink(tempfile.c_str ())) {
+            std::ofstream os (tempfile.c_str ());
+
+            if (os) {
+                os.write (bufptr, (std::streamsize)filesize);
+                os.close ();
+
+                // Check if the file is written correctly.
+                if (scim_load_file (tempfile, 0) == filesize) {
+                    m_icon_repository [icon] = tempfile;
+                    local_icon = tempfile;
+                } else {
+                    unlink (tempfile.c_str ());
+                }
             }
         }
     }
index ef996c7..f6faf18 100644 (file)
@@ -458,18 +458,20 @@ scim_global_config_flush ()
              __config_repository.usr.erase (it->first);
     }
 
-    std::ofstream usr_os (usr_conf_file.c_str ());
+    if (!scim_is_symlink(usr_conf_file.c_str ())) {
+        std::ofstream usr_os (usr_conf_file.c_str ());
 
-    if (usr_os) {
-        for (KeyValueRepository::iterator it = __config_repository.usr.begin ();
-             it != __config_repository.usr.end (); ++it) {
-            usr_os << it->first << " = " << it->second << "\n";
-        }
-        __config_repository.updated.clear ();
+        if (usr_os) {
+            for (KeyValueRepository::iterator it = __config_repository.usr.begin ();
+                it != __config_repository.usr.end (); ++it) {
+                usr_os << it->first << " = " << it->second << "\n";
+            }
+            __config_repository.updated.clear ();
 
-        sync();
+            sync();
 
-        return true;
+            return true;
+        }
     }
 
     return false;
index 6706046..192ec5b 100644 (file)
@@ -1441,7 +1441,7 @@ EXAPI void isf_save_log (const char *fmt, ...)
         String strLogFile = scim_get_user_data_dir () + String (SCIM_PATH_DELIM_STRING) + String ("isf.log");
         int ret = stat(strLogFile.c_str(), &st);
         if (ret == 0 || (ret == -1 && errno == ENOENT)) {
-            if (st.st_size < MAX_LOG_FILE_SIZE) {
+            if (st.st_size < MAX_LOG_FILE_SIZE && !scim_is_symlink(strLogFile.c_str ())) {
                 std::ofstream isf_log_file (strLogFile.c_str (), std::ios::app);
                 isf_log_file << buf << std::endl;
                 isf_log_file.flush ();
@@ -1454,6 +1454,18 @@ EXAPI void isf_save_log (const char *fmt, ...)
     LOGD ("%s", buf);
 }
 
+EXAPI bool scim_is_symlink (const String filename)
+{
+    struct stat info;
+    if (lstat(filename.c_str (), &info) < 0) {
+        char buf_err[256];
+        LOGE("lstat failed. %s", strerror_r (errno, buf_err, sizeof (buf_err)));
+        return false;
+    }
+
+    return S_ISLNK(info.st_mode);
+}
+
 static struct timeval _t0 = {0, 0};
 static struct timeval _t1;
 
index 15dec53..b15b1a5 100644 (file)
@@ -455,6 +455,13 @@ EXAPI void scim_daemon ();
  */
 EXAPI void isf_save_log (const char *fmt, ...);
 
+/**
+ * @brief Check whether the given file is symbolic link or not.
+ *
+ * @return true if the file is a symbolic link.
+ */
+EXAPI bool scim_is_symlink (const String filename);
+
 #define ISF_SAVE_LOG(fmt, arg...) \
     do{ \
         isf_save_log ("time:%ld  pid:%d  %s  %s  " fmt, time (0), getpid (), __FILE__, __func__, ##arg); \