Svace issue fix and code refactoring 33/126733/1
authorHaejeong Kim <backto.kim@samsung.com>
Tue, 25 Apr 2017 03:53:49 +0000 (12:53 +0900)
committerHaejeong Kim <backto.kim@samsung.com>
Tue, 25 Apr 2017 03:53:49 +0000 (12:53 +0900)
Change-Id: Ie9ff7735c3982102e9ff3572d5a767c11bf727d3

include/media_util_private.h
src/media_filter.c
src/media_util_private.c

index 9c01ce2..a9b4cf9 100755 (executable)
@@ -33,8 +33,7 @@ extern "C" {
 int _media_util_check_file_exist(const char *path);
 int _media_util_check_ignore_file(const char *path, bool *ignore);
 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore);
-int _media_content_replace_path_in_condition(const char *condition, char *replace_condition);
-int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition);
+int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace);
 int _media_content_replace_path(const char *path, char *replace_path);
 int _media_content_rollback_path(const char *path, char *replace_path);
 
index 76ebf4c..0197deb 100755 (executable)
@@ -938,7 +938,7 @@ int media_filter_set_condition(filter_h filter, const char *condition, media_con
 
                char new_condition[MAX_QUERY_SIZE] = {0, };
                memset(new_condition, 0, sizeof(new_condition));
-               ret = _media_content_replace_path_in_condition(condition, new_condition);
+               ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
                media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
 
                _filter->condition = strdup(new_condition);
@@ -1029,7 +1029,7 @@ int media_filter_get_condition(filter_h filter, char **condition, media_content_
                if (STRING_VALID(_filter->condition)) {
                        char new_condition[MAX_QUERY_SIZE] = {0, };
                        memset(new_condition, 0, sizeof(new_condition));
-                       ret = _media_content_rollback_path_in_condition(_filter->condition, new_condition);
+                       ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
                        media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
 
                        *condition = strdup(new_condition);
index ac36b35..e1da42a 100755 (executable)
@@ -25,6 +25,7 @@
 #endif
 
 static char *g_old_path = NULL;
+#define TIZEN_USER_CONTENT_PATH  tzplatform_getenv(TZ_USER_CONTENT)
 
 int _media_util_check_file_exist(const char *path)
 {
@@ -173,110 +174,98 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
        return MEDIA_CONTENT_ERROR_NONE;
 }
 
-int _media_content_replace_path_in_condition(const char *condition, char *replace_condition)
+int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
 {
        int ret = MEDIA_CONTENT_ERROR_NONE;
+       char old_condition[MAX_QUERY_SIZE] = {0, };
+       char new_condition[MAX_QUERY_SIZE] = {0, };
+       char *find = NULL;
+       unsigned int str_len = 0;
 
-       if (!STRING_VALID(g_old_path)) {
-               ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
+       char *find_str = NULL;
+       char *to_replace_str = NULL;
+
+       if (replace == TRUE) {  //change User session path to System session path
+               ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
                if (ret != STORAGE_ERROR_NONE) {
                        media_content_error("storage_get_directory failed");
-                       return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
                }
-       }
-
-       media_content_sec_debug("Old condition[%s]", condition);
-       if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, g_old_path) != NULL)) {
-               char *cond = strdup(condition);
-               char *repl_cond_ptr = replace_condition;
-               char *cond_ptr = cond;
 
-               if (cond_ptr == NULL) {
-                       media_content_error("memory allocation failed");
-                       return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               to_replace_str = g_strdup(TIZEN_USER_CONTENT_PATH);
+               if (!STRING_VALID(to_replace_str)) {
+                       media_content_error("Get TZ_USER_CONTENT failed");
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
                }
-               while (*cond_ptr != '\0') {
-                       if (strlen(cond_ptr) < strlen(g_old_path)) {
-                               memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
-                               break;
-                       }
-                       /* replace path only and keep other condition */
-                       if (memcmp(cond_ptr, g_old_path, strlen(g_old_path)) == 0) {
-                               memcpy(repl_cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT)));
-                               cond_ptr += strlen(g_old_path);
-                               repl_cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
-                       } else {
-                               *repl_cond_ptr = *cond_ptr;
-                               cond_ptr++;
-                               repl_cond_ptr++;
-                       }
-               }
-               SAFE_FREE(cond);
        } else {
-               snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
-       }
-
-       if (!STRING_VALID(replace_condition)) {
-               media_content_error("replace failed");
-               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-       }
-
-       media_content_sec_debug("repl cond[%s]", replace_condition);
-
-       return MEDIA_CONTENT_ERROR_NONE;
-}
-
-int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition)
-{
-       int ret = MEDIA_CONTENT_ERROR_NONE;
+               find_str = g_strdup(TIZEN_USER_CONTENT_PATH);
+               if (!STRING_VALID(find_str)) {
+                       media_content_error("Get TZ_USER_CONTENT failed");
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
+               }
 
-       if (!STRING_VALID(g_old_path)) {
-               ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
+               ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
                if (ret != STORAGE_ERROR_NONE) {
                        media_content_error("storage_get_directory failed");
-                       return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
                }
        }
 
+       memset(old_condition, 0, sizeof(old_condition));
+       memset(new_condition, 0, sizeof(new_condition));
+
        media_content_sec_debug("Old condition[%s]", condition);
-       if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, tzplatform_getenv(TZ_USER_CONTENT)) != NULL)) {
-               char *cond = strdup(condition);
-               char *repl_cond_ptr = replace_condition;
-               char *cond_ptr = cond;
 
-               if (cond_ptr == NULL) {
-                       media_content_error("memory allocation failed");
-                       return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-               }
-               while (*cond_ptr != '\0') {
-                       if (strlen(cond_ptr) < strlen(tzplatform_getenv(TZ_USER_CONTENT))) {
-                               memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
-                               break;
-                       }
-                       /* replace path only and keep other condition */
-                       if (memcmp(cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
-                               memcpy(repl_cond_ptr, g_old_path, strlen(g_old_path));
-                               cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
-                               repl_cond_ptr += strlen(g_old_path);
-                       } else {
-                               *repl_cond_ptr = *cond_ptr;
-                               cond_ptr++;
-                               repl_cond_ptr++;
-                       }
+       if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
+               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
+               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               goto ERROR;
+       }
+
+       find = strstr(new_condition, find_str);
+
+       while (find != NULL) {
+               str_len = find - new_condition;
+
+               memset(old_condition, 0, sizeof(old_condition));
+               if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
+                       media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
                }
-               SAFE_FREE(cond);
-       } else {
-               snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
+               memset(new_condition, 0, sizeof(new_condition));
+
+               snprintf(new_condition, str_len + 1, "%s", old_condition);
+
+               SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
+               SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
+
+               find = strstr(new_condition, find_str);
        }
 
+       if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
+               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
+               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               goto ERROR;
+       }
+
+       media_content_sec_debug("repl cond[%s]", replace_condition);
+
        if (!STRING_VALID(replace_condition)) {
                media_content_error("replace failed");
-               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               goto ERROR;
        }
 
-       media_content_sec_debug("repl cond[%s]", replace_condition);
+ERROR:
+       SAFE_FREE(find_str);
+       SAFE_FREE(to_replace_str);
 
-       return MEDIA_CONTENT_ERROR_NONE;
+       return ret;
 }
 
 int _media_content_replace_path(const char *path, char *replace_path)