Change asprintf() to strndup()/snprintf() 47/47947/3 tizen_3.0.m1_mobile tizen_3.0.m1_tv accepted/tizen/mobile/20150910.110202 accepted/tizen/tv/20150910.110209 accepted/tizen/wearable/20150910.110220 submit/tizen/20150910.092752 submit/tizen_common/20151023.083358 submit/tizen_common/20151026.085049 tizen_3.0.m1_mobile_release tizen_3.0.m1_tv_release
authorMinje Ahn <minje.ahn@samsung.com>
Thu, 10 Sep 2015 07:56:41 +0000 (16:56 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Thu, 10 Sep 2015 08:29:33 +0000 (17:29 +0900)
Change-Id: I23f48a4a3873b7c4f99e0fd8ed3fcd49b777d9e7
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
lib/media-util-register.c
src/common/media-common-utils.c
src/scanner/media-scanner-scan.c

index 1738907..8d73085 100755 (executable)
@@ -59,24 +59,21 @@ static char* __media_get_path(uid_t uid)
 {
        char *result_psswd = NULL;
        struct group *grpinfo = NULL;
-       if(uid == getuid())
-       {
-               result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
+       if (uid == getuid()) {
+               result_psswd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
                grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
+               if (grpinfo == NULL) {
                        MSAPI_DBG_ERR("getgrnam(users) returns NULL !");
                        return NULL;
                }
-       }
-       else
-       {
+       } else {
                struct passwd *userinfo = getpwuid(uid);
-               if(userinfo == NULL) {
+               if (userinfo == NULL) {
                        MSAPI_DBG_ERR("getpwuid(%d) returns NULL !", uid);
                        return NULL;
                }
                grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
+               if (grpinfo == NULL) {
                        MSAPI_DBG_ERR("getgrnam(users) returns NULL !");
                        return NULL;
                }
@@ -85,7 +82,7 @@ static char* __media_get_path(uid_t uid)
                        MSAPI_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               asprintf(&result_psswd, "%s", userinfo->pw_dir);
+               result_psswd = strndup(userinfo->pw_dir, strlen(userinfo->pw_dir));
        }
 
        return result_psswd;
@@ -93,23 +90,32 @@ static char* __media_get_path(uid_t uid)
 
 static bool _is_valid_path(const char *path, uid_t uid)
 {
+       bool ret = false;
        int length_path;
        char * user_path = NULL;
-       
+
        if (path == NULL)
                return false;
 
        user_path = __media_get_path(uid);
+       if (user_path == NULL)
+               return false;
+
        length_path = strlen(user_path);
 
        if (strncmp(path, user_path, length_path) == 0) {
-               return true;
+               ret = true;
        } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
-               return true;
+               ret = true;
        } else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
-               return true;
-       } else
-               return false;
+               ret = true;
+       } else {
+               ret = false;
+       }
+
+       MS_SAFE_FREE(user_path);
+
+       return ret;
 }
 
 static int _check_dir_path(const char *dir_path, uid_t uid)
index 3fe7418..4bba92d 100755 (executable)
@@ -96,25 +96,22 @@ static char* __media_get_path(uid_t uid)
        struct group *grpinfo = NULL;
        int err = 0;
 
-       if(uid == getuid())
-       {
-               result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
+       if (uid == getuid()) {
+               result_psswd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
                grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
+               if (grpinfo == NULL) {
                        MS_DBG_ERR("getgrnam(users) returns NULL !");
                        MS_SAFE_FREE(result_psswd);
                        return NULL;
                }
-       }
-       else
-       {
+       } else {
                struct passwd *userinfo = getpwuid(uid);
-               if(userinfo == NULL) {
+               if (userinfo == NULL) {
                        MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
                        return NULL;
                }
                grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
+               if (grpinfo == NULL) {
                        MS_DBG_ERR("getgrnam(users) returns NULL !");
                        return NULL;
                }
@@ -123,11 +120,7 @@ static char* __media_get_path(uid_t uid)
                        MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               err = asprintf(&result_psswd, "%s", userinfo->pw_dir);
-               if(err < 0) {
-                       MS_DBG_ERR("asprintf failed [%d]", err);
-                       return NULL;
-               }
+               result_psswd = strndup(userinfo->pw_dir, strlen(userinfo->pw_dir));
        }
 
        return result_psswd;
@@ -136,22 +129,31 @@ static char* __media_get_path(uid_t uid)
 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
 {
        int length_path;
+       int ret = MS_MEDIA_ERR_NONE;
        char * user_path = NULL;
 
        if (path == NULL)
-               return false;
+               return MS_MEDIA_ERR_INVALID_PATH;
 
        user_path = __media_get_path(uid);
+       if (user_path == NULL)
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+
        length_path = strlen(user_path);
 
        if (strncmp(path, user_path, length_path) == 0) {
-               return MS_STORAGE_INTERNAL;
+               ret = MS_STORAGE_INTERNAL;
        } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
-               return MS_STORAGE_EXTERNAL;
+               ret = MS_STORAGE_EXTERNAL;
+       } else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
+               ret = MS_STORAGE_EXTERNAL_USB;
        } else {
-               free(user_path);
-               return MS_MEDIA_ERR_INVALID_PATH;
-    }
+               ret = MS_MEDIA_ERR_INVALID_PATH;
+       }
+
+       MS_SAFE_FREE(user_path);
+
+       return ret;
 }
 
 int ms_strappend(char *res, const int size, const char *pattern,
index 62e871f..5ae6a35 100755 (executable)
@@ -86,26 +86,24 @@ static char* __msc_get_path(uid_t uid);
 
 static char* __msc_get_path(uid_t uid)
 {
+       int result_size = 0;
        char *result_psswd = NULL;
        struct group *grpinfo = NULL;
-       if(uid == getuid())
-       {
-               result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
+       if (uid == getuid()) {
+               result_psswd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
                grpinfo = getgrnam("users");
                if(grpinfo == NULL) {
                        MS_DBG_ERR("getgrnam(users) returns NULL !");
                        return NULL;
                }
-    }
-       else
-       {
+       } else {
                struct passwd *userinfo = getpwuid(uid);
-               if(userinfo == NULL) {
+               if (userinfo == NULL) {
                        MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
                        return NULL;
                }
                grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
+               if (grpinfo == NULL) {
                        MS_DBG_ERR("getgrnam(users) returns NULL !");
                        return NULL;
                }
@@ -114,7 +112,10 @@ static char* __msc_get_path(uid_t uid)
                        MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
-               asprintf(&result_psswd, "%s/%s", userinfo->pw_dir, MEDIA_CONTENT_PATH);
+               result_size = strlen(userinfo->pw_dir) + strlen(MEDIA_CONTENT_PATH) + 2;
+
+               MS_MALLOC(result_psswd, result_size);
+               snprintf(result_psswd, result_size, "%s/%s", userinfo->pw_dir, MEDIA_CONTENT_PATH);
        }
 
        return result_psswd;
@@ -964,6 +965,7 @@ static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s
 
 static bool __msc_is_valid_path(const char *path, uid_t uid)
 {
+       bool ret = false;
        char *usr_path = NULL;
 
        if (path == NULL)
@@ -974,16 +976,18 @@ static bool __msc_is_valid_path(const char *path, uid_t uid)
                return false;
 
        if (strncmp(path, usr_path, strlen(usr_path)) == 0) {
-               MS_SAFE_FREE(usr_path);
-               return true;
+               ret = true;
        } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
-               MS_SAFE_FREE(usr_path);
-               return true;
+               ret = true;
+       } else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
+               ret = true;
        } else {
-               MS_SAFE_FREE(usr_path);
-               return false;
+               ret = false;
        }
-       return true;
+
+       MS_SAFE_FREE(usr_path);
+
+       return ret;
 }
 
 static int __msc_check_file_path(const char *file_path, uid_t uid)