Prevent issue fix 25/49425/2
authorHaejeong, Kim <backto.kim@samsung.com>
Tue, 13 Oct 2015 06:42:24 +0000 (15:42 +0900)
committerHaejeong, Kim <backto.kim@samsung.com>
Tue, 13 Oct 2015 06:54:19 +0000 (15:54 +0900)
Change-Id: I46f8d5f8e3ee9f9da9521c0e08f2da5ececd37d9

packaging/libmedia-service.spec
src/common/media-svc-db-utils.c
src/common/media-svc-media-folder.c
src/common/media-svc-media.c
src/common/media-svc-storage.c
src/common/media-svc-util.c
src/common/media-svc.c

index bea7900..733ee9c 100644 (file)
@@ -1,6 +1,6 @@
 Name:       libmedia-service
 Summary:    Media information service library for multimedia applications
-Version: 0.2.56
+Version: 0.2.57
 Release:    3
 Group:      System/Libraries
 License:    Apache-2.0 and public domain
index a5aae27..3d61c64 100755 (executable)
@@ -43,16 +43,17 @@ static GSList *column_list[MEDIA_SVC_DB_LIST_MAX];
 
 char *_media_svc_get_path(uid_t uid)
 {
-       char *result_psswd = NULL;
+       char *result_passwd = NULL;
        struct group *grpinfo = NULL;
        if (uid == getuid()) {
-               result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
                grpinfo = getgrnam("users");
                if (grpinfo == NULL) {
                        media_svc_error("getgrnam(users) returns NULL !");
                        return NULL;
                }
+               result_passwd = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
        } else {
+               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
                struct passwd *userinfo = getpwuid(uid);
                if (userinfo == NULL) {
                        media_svc_error("getpwuid(%d) returns NULL !", uid);
@@ -68,10 +69,11 @@ char *_media_svc_get_path(uid_t uid)
                        media_svc_error("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               asprintf(&result_psswd, "%s/%s", userinfo->pw_dir, MEDIA_CONTENT_PATH);
+               sprintf(passwd_str, "%s/%s", userinfo->pw_dir, MEDIA_CONTENT_PATH);
+               result_passwd = g_strdup(passwd_str);
        }
 
-       return result_psswd;
+       return result_passwd;
 }
 
 int __media_svc_add_table_info(const char *name, const char *triggerName, const char *eventTable, const char *actionTable, const char *viewName)
index 1405c4a..1d238e6 100755 (executable)
@@ -36,13 +36,16 @@ static int __media_svc_is_root_path(const char *folder_path, bool *is_root, uid_
 
        *is_root = FALSE;
 
-       if (strcmp(folder_path, _media_svc_get_path(uid)) == 0 ||
+       char *internal_path = _media_svc_get_path(uid);
+
+       if ((STRING_VALID(internal_path) && (strcmp(folder_path, internal_path) == 0)) ||
                strcmp(folder_path, MEDIA_ROOT_PATH_SDCARD) == 0 ||
-               strcmp(folder_path, MEDIA_ROOT_PATH_CLOUD) == 0) {
+               (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && strcmp(folder_path, MEDIA_ROOT_PATH_CLOUD) == 0)) {
                media_svc_debug("ROOT PATH [%s]", folder_path);
                *is_root = TRUE;
        }
 
+       SAFE_FREE(internal_path);
        return MS_MEDIA_ERR_NONE;
 }
 
@@ -62,7 +65,7 @@ static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is
                return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
 
-       if (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0) {
+       if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
                media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
                *is_root = TRUE;
        }
@@ -223,22 +226,27 @@ static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, const char
        char *folder_uuid = NULL;
        char parent_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
        bool folder_search_end = FALSE;
+       char *internal_path = NULL;
 
        memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
+       internal_path = _media_svc_get_path(uid);
 
-       if (strncmp(path, _media_svc_get_path(uid), strlen(_media_svc_get_path(uid))) == 0)
-               next_pos = strlen(_media_svc_get_path(uid));
+       if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
+               next_pos = strlen(internal_path);
        else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
                next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
-       else if (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0)
+       else if (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0))
                next_pos = strlen(MEDIA_ROOT_PATH_CLOUD);
        else if (strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
                next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
        else {
                media_svc_error("Invalid Path");
+               SAFE_FREE(internal_path);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
+       SAFE_FREE(internal_path);
+
        while (!folder_search_end) {
                next = strstr(path + next_pos, token);
                if (next != NULL) {
@@ -251,7 +259,7 @@ static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, const char
                        media_svc_error("[No-Error] End Path [%s]", dir_path);
                }
 
-               if (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0) {
+               if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
                        /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
                        continue;
                }
index a1fcf11..be41aa0 100755 (executable)
@@ -177,18 +177,17 @@ int _media_svc_count_record_with_path(sqlite3 *handle,  const char *storage_id,
 
 char *_media_svc_get_thumb_default_path(uid_t uid)
 {
-       char *result_psswd = NULL;
+       char *result_passwd = NULL;
        struct group *grpinfo = NULL;
        if (uid == getuid()) {
-               result_psswd = strdup(MEDIA_SVC_THUMB_DEFAULT_PATH);
                grpinfo = getgrnam("users");
                if (grpinfo == NULL) {
                        media_svc_error("getgrnam(users) returns NULL !");
-                       if(result_psswd)
-                               free(result_psswd);
                        return NULL;
                }
+               result_passwd = g_strdup(MEDIA_SVC_THUMB_DEFAULT_PATH);
        } else {
+               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
                struct passwd *userinfo = getpwuid(uid);
                if (userinfo == NULL) {
                        media_svc_error("getpwuid(%d) returns NULL !", uid);
@@ -204,10 +203,11 @@ char *_media_svc_get_thumb_default_path(uid_t uid)
                        media_svc_error("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               asprintf(&result_psswd, "%s/share/media/.thumb/thumb_default.png", userinfo->pw_dir);
+               sprintf(passwd_str, "%s/share/media/.thumb/thumb_default.png", userinfo->pw_dir);
+               result_passwd = g_strdup(passwd_str);
        }
 
-       return result_psswd;
+       return result_passwd;
 }
 
 int _media_svc_insert_item_with_data(sqlite3 *handle, const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
@@ -637,8 +637,9 @@ int _media_svc_delete_invalid_items(sqlite3 *handle, const char *storage_id, med
        }
 
        /*Delete thumbnails*/
+       char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
        for (idx = 0; idx < invalid_count; idx++) {
-               if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
+               if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (STRING_VALID(default_thumbnail_path)) && (strncmp(thumbpath_record[idx].thumbnail_path,default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
                        ret = _media_svc_remove_file(thumbpath_record[idx].thumbnail_path);
                        if (ret != MS_MEDIA_ERR_NONE) {
                                media_svc_error("fail to remove thumbnail file.");
@@ -647,6 +648,7 @@ int _media_svc_delete_invalid_items(sqlite3 *handle, const char *storage_id, med
        }
 
        SAFE_FREE(thumbpath_record);
+       SAFE_FREE(default_thumbnail_path);
 
        return MS_MEDIA_ERR_NONE;
 }
@@ -701,16 +703,20 @@ int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *storage_
 
        /*Delete thumbnails*/
        if (thumbpath_record != NULL)    {
-               for (idx = 0; idx < invalid_count; idx++) {
-                       if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
-                               ret = _media_svc_remove_file(thumbpath_record[idx].thumbnail_path);
-                               if (ret != MS_MEDIA_ERR_NONE) {
-                                       media_svc_error("fail to remove thumbnail file.");
+               char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
+               if (STRING_VALID(default_thumbnail_path)) {
+                       for (idx = 0; idx < invalid_count; idx++) {
+                               if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
+                                       ret = _media_svc_remove_file(thumbpath_record[idx].thumbnail_path);
+                                       if (ret != MS_MEDIA_ERR_NONE) {
+                                               media_svc_error("fail to remove thumbnail file.");
+                                       }
                                }
                        }
                }
 
                SAFE_FREE(thumbpath_record);
+               SAFE_FREE(default_thumbnail_path);
        }
 
        return MS_MEDIA_ERR_NONE;
index 1fdc915..33ab91e 100755 (executable)
@@ -42,11 +42,13 @@ int _media_svc_init_storage(sqlite3 *handle, uid_t uid)
        SQLITE3_FINALIZE(sql_stmt);
 
        if (storage_cnt == 0) {
+               char *internal_path = _media_svc_get_path(uid);
                sql = sqlite3_mprintf("INSERT INTO %s (storage_uuid, storage_name, storage_path, storage_type) VALUES ('%s', '%s', '%s', 0);",
-                                     MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA, _media_svc_get_path(uid));
+                                     MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA, internal_path);
 
                ret = _media_svc_sql_query(handle, sql, uid);
                sqlite3_free(sql);
+               SAFE_FREE(internal_path);
                media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
        }
 
@@ -210,7 +212,7 @@ int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage
 
        media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
 
-       remain_path = strstr(path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
+       remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
        if (remain_path != NULL)
                remain_len = strlen(remain_path);
 
index e0058e5..be3f01f 100755 (executable)
@@ -145,8 +145,6 @@ typedef enum {
        MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST          = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
 } media_svc_extracted_field_e;
 
-static char *_media_svc_get_thumb_path(uid_t uid);
-
 char *_media_info_generate_uuid(void)
 {
        uuid_t uuid_value;
@@ -568,6 +566,41 @@ static int __media_svc_get_location_value(MMHandleType tag, double *longitude, d
        return MS_MEDIA_ERR_NONE;
 }
 
+static char *__media_svc_get_thumb_path(uid_t uid)
+{
+       char *result_passwd = NULL;
+       struct group *grpinfo = NULL;
+       if (uid == getuid()) {
+               grpinfo = getgrnam("users");
+               if (grpinfo == NULL) {
+                       media_svc_error("getgrnam(users) returns NULL !");
+                       return NULL;
+               }
+               result_passwd = g_strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
+       } else {
+               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
+               struct passwd *userinfo = getpwuid(uid);
+               if (userinfo == NULL) {
+                       media_svc_error("getpwuid(%d) returns NULL !", uid);
+                       return NULL;
+               }
+               grpinfo = getgrnam("users");
+               if (grpinfo == NULL) {
+                       media_svc_error("getgrnam(users) returns NULL !");
+                       return NULL;
+               }
+               /* Compare git_t type and not group name */
+               if (grpinfo->gr_gid != userinfo->pw_gid) {
+                       media_svc_error("UID [%d] does not belong to 'users' group!", uid);
+                       return NULL;
+               }
+               sprintf(passwd_str, "%s/share/media/.thumb", userinfo->pw_dir);
+               result_passwd = g_strdup(passwd_str);
+       }
+
+       return result_passwd;
+}
+
 static int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
 {
        media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
@@ -578,11 +611,20 @@ static int _media_svc_save_image(void *image, int size, char *image_path, uid_t
        }
 
        struct statfs fs;
-       if (-1 == statfs(_media_svc_get_thumb_path(uid), &fs)) {
+       char *thumb_path = __media_svc_get_thumb_path(uid);
+       if(!STRING_VALID(thumb_path)) {
+               media_svc_error("fail to get thumb_path");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       if (-1 == statfs(thumb_path, &fs)) {
                media_svc_error("error in statfs");
+               SAFE_FREE(thumb_path);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
+       SAFE_FREE(thumb_path);
+
        long bsize_kbytes = fs.f_bsize >> 10;
 
        if ((bsize_kbytes * fs.f_bavail) < 1024) {
@@ -727,18 +769,17 @@ int _media_svc_remove_all_files_in_dir(const char *dir_path)
 
 char *_media_svc_get_thumb_internal_path(uid_t uid)
 {
-       char *result_psswd = NULL;
+       char *result_passwd = NULL;
        struct group *grpinfo = NULL;
        if (uid == getuid()) {
-               result_psswd = strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
                grpinfo = getgrnam("users");
                if (grpinfo == NULL) {
                        media_svc_error("getgrnam(users) returns NULL !");
-                       if(result_psswd)
-                               free(result_psswd);
                        return NULL;
                }
+               result_passwd = g_strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
        } else {
+               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
                struct passwd *userinfo = getpwuid(uid);
                if (userinfo == NULL) {
                        media_svc_error("getpwuid(%d) returns NULL !", uid);
@@ -754,26 +795,26 @@ char *_media_svc_get_thumb_internal_path(uid_t uid)
                        media_svc_error("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               asprintf(&result_psswd, "%s/share/media/.thumb/phone", userinfo->pw_dir);
+               sprintf(passwd_str, "%s/share/media/.thumb/phone", userinfo->pw_dir);
+               result_passwd = g_strdup(passwd_str);
        }
 
-       return result_psswd;
+       return result_passwd;
 }
 
 char *_media_svc_get_thumb_external_path(uid_t uid)
 {
-       char *result_psswd = NULL;
+       char *result_passwd = NULL;
        struct group *grpinfo = NULL;
        if (uid == getuid()) {
-               result_psswd = strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
                grpinfo = getgrnam("users");
                if (grpinfo == NULL) {
                        media_svc_error("getgrnam(users) returns NULL !");
-                       if(result_psswd)
-                               free(result_psswd);
                        return NULL;
                }
+               result_passwd = g_strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
        } else {
+               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
                struct passwd *userinfo = getpwuid(uid);
                if (userinfo == NULL) {
                        media_svc_error("getpwuid(%d) returns NULL !", uid);
@@ -789,10 +830,11 @@ char *_media_svc_get_thumb_external_path(uid_t uid)
                        media_svc_error("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               asprintf(&result_psswd, "%s/share/media/.thumb/mmc", userinfo->pw_dir);
+               sprintf(passwd_str, "%s/share/media/.thumb/mmc", userinfo->pw_dir);
+               result_passwd = g_strdup(passwd_str);
        }
 
-       return result_psswd;
+       return result_passwd;
 }
 
 static int __media_svc_check_thumb_dir(const char *thumb_dir)
@@ -832,41 +874,6 @@ ERROR:
        return -1;
 }
 
-static char *_media_svc_get_thumb_path(uid_t uid)
-{
-       char *result_psswd = NULL;
-       struct group *grpinfo = NULL;
-       if (uid == getuid()) {
-               result_psswd = strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       if(result_psswd)
-                               free(result_psswd);
-                       return NULL;
-               }
-       } else {
-               struct passwd *userinfo = getpwuid(uid);
-               if (userinfo == NULL) {
-                       media_svc_error("getpwuid(%d) returns NULL !", uid);
-                       return NULL;
-               }
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               /* Compare git_t type and not group name */
-               if (grpinfo->gr_gid != userinfo->pw_gid) {
-                       media_svc_error("UID [%d] does not belong to 'users' group!", uid);
-                       return NULL;
-               }
-               asprintf(&result_psswd, "%s/share/media/.thumb", userinfo->pw_dir);
-       }
-
-       return result_psswd;
-}
-
 int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -875,12 +882,23 @@ int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *t
        char *thumb_dir = NULL;
        char hash[255 + 1] = {0, };
        char *thumbfile_ext = NULL;
+       char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
+       char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
 
-       thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? _media_svc_get_thumb_internal_path(uid) : _media_svc_get_thumb_external_path(uid);
+       if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
+               media_svc_error("fail to get thumbnail path");
+               SAFE_FREE(internal_thumb_path);
+               SAFE_FREE(external_thumb_path);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
 
        ret = __media_svc_check_thumb_dir(thumb_dir);
        if (ret != MS_MEDIA_ERR_NONE) {
                media_svc_error("__media_svc_check_thumb_dir");
+               SAFE_FREE(internal_thumb_path);
+               SAFE_FREE(external_thumb_path);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
@@ -892,6 +910,8 @@ int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *t
        ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
        if (ret != MS_MEDIA_ERR_NONE) {
                media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
+               SAFE_FREE(internal_thumb_path);
+               SAFE_FREE(external_thumb_path);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
@@ -907,6 +927,8 @@ int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *t
                thumbfile_ext = (char *)"bmp";
        } else {
                media_svc_error("Not proper img format");
+               SAFE_FREE(internal_thumb_path);
+               SAFE_FREE(external_thumb_path);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
@@ -914,6 +936,9 @@ int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *t
        _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
        /*media_svc_debug("thumb_path is [%s]", thumb_path); */
 
+       SAFE_FREE(internal_thumb_path);
+       SAFE_FREE(external_thumb_path);
+
        return MS_MEDIA_ERR_NONE;
 }
 
@@ -2024,16 +2049,19 @@ void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
 {
        if (STRING_VALID(path)) {
-               if (strncmp(path, _media_svc_get_path(uid), strlen(_media_svc_get_path(uid))) == 0) {
+               char *internal_path = _media_svc_get_path(uid);
+               if (strncmp(path, internal_path, strlen(internal_path)) == 0) {
                        *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
-               } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
+               } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
                        *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
-               } else if (strncmp (path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
+               } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp (path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
                        *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
                } else {
                        media_svc_error("Invalid Path");
+                       SAFE_FREE(internal_path);
                        return MS_MEDIA_ERR_INVALID_PARAMETER;
                }
+               SAFE_FREE(internal_path);
        } else {
                media_svc_error("INVALID parameter");
                return MS_MEDIA_ERR_INVALID_PARAMETER;
index 10c3029..28de952 100755 (executable)
@@ -490,11 +490,13 @@ int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_sv
                }
 
                /* If old thumb path is default or not */
-               if (strncmp(old_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) == 0) {
-                       strncpy(new_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(new_thumb_path));
+               char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
+               if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
+                       strncpy(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
                } else {
                        _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
                }
+               SAFE_FREE(default_thumbnail_path);
        }
 
        if (g_media_svc_move_item_data_cnt == 1) {
@@ -577,7 +579,7 @@ int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_sv
 
        /*rename thumbnail file*/
 /*     if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
-       if ((strlen(old_thumb_path) > 0) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
+       if ((strlen(old_thumb_path) > 0) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
                ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
                if (ret != MS_MEDIA_ERR_NONE)
                        media_svc_error("_media_svc_rename_file failed : %d", ret);
@@ -730,7 +732,8 @@ int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id
        }
 
        /*Delete thumbnail*/
-       if ((strlen(thumb_path) > 0) && (strncmp(thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
+       char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
+       if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0))) {
 /*
                int thumb_count = 1;
                // Get count of media, which contains same thumbnail for music
@@ -750,6 +753,8 @@ int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id
 //             }
        }
 
+       SAFE_FREE(default_thumbnail_path);
+
        return MS_MEDIA_ERR_NONE;
 }
 
@@ -768,10 +773,15 @@ int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, const char *st
        media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
 
        if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
-               const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? MEDIA_SVC_THUMB_INTERNAL_PATH : MEDIA_SVC_THUMB_EXTERNAL_PATH;
+               char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
+               char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
+
+               const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
 
                /* remove thumbnails */
                ret = _media_svc_remove_all_files_in_dir(dirpath);
+               SAFE_FREE(internal_thumb_path);
+               SAFE_FREE(external_thumb_path);
                media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
        }
 
@@ -860,7 +870,7 @@ int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media
                        return ret;
                }
 
-               if (g_file_test(thumb_path, G_FILE_TEST_EXISTS) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
+               if (g_file_test(thumb_path, G_FILE_TEST_EXISTS) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
                        ret = _media_svc_remove_file(thumb_path);
                        if (ret != MS_MEDIA_ERR_NONE) {
                                media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
@@ -1037,13 +1047,15 @@ int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, cons
 
                if (!no_thumb) {
                        /* If old thumb path is default or not */
-                       if (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) == 0) {
-                               strncpy(media_new_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(media_new_thumb_path));
+                       char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
+                       if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
+                               strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path));
                        } else {
                                ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
                                if (ret != MS_MEDIA_ERR_NONE) {
                                        media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
                                        SAFE_FREE(replaced_path);
+                                       SAFE_FREE(default_thumbnail_path);
                                        _media_svc_sql_rollback_trans(db_handle, uid);
                                        return ret;
                                }
@@ -1052,12 +1064,15 @@ int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, cons
                                if (ret != MS_MEDIA_ERR_NONE) {
                                        media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
                                        SAFE_FREE(replaced_path);
+                                       SAFE_FREE(default_thumbnail_path);
                                        SQLITE3_FINALIZE(sql_stmt);
                                        _media_svc_sql_rollback_trans(db_handle, uid);
                                        return ret;
                                }
                        }
 
+                       SAFE_FREE(default_thumbnail_path);
+
                        /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
                }
 
@@ -1091,14 +1106,15 @@ int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, cons
                }
 
                /* Rename thumbnail file of file system */
-/*             if ((!no_thumb) && (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) */
-/*                             && (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) { */
-               if ((!no_thumb) && (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
+               char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
+               if ((!no_thumb) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
                        ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
                        if (ret != MS_MEDIA_ERR_NONE) {
                                media_svc_error("_media_svc_rename_file failed : %d", ret);
                        }
                }
+
+               SAFE_FREE(default_thumbnail_path);
        }
 
        SQLITE3_FINALIZE(sql_stmt);