From a5e8701dab3e1f5cec4444a6b0771c3a39593471 Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Thu, 10 Sep 2015 16:56:41 +0900 Subject: [PATCH] Change asprintf() to strndup()/snprintf() Change-Id: I23f48a4a3873b7c4f99e0fd8ed3fcd49b777d9e7 Signed-off-by: Minje Ahn --- lib/media-util-register.c | 38 +++++++++++++++++++++--------------- src/common/media-common-utils.c | 42 +++++++++++++++++++++------------------- src/scanner/media-scanner-scan.c | 36 +++++++++++++++++++--------------- 3 files changed, 64 insertions(+), 52 deletions(-) diff --git a/lib/media-util-register.c b/lib/media-util-register.c index 1738907..8d73085 100755 --- a/lib/media-util-register.c +++ b/lib/media-util-register.c @@ -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) diff --git a/src/common/media-common-utils.c b/src/common/media-common-utils.c index 3fe7418..4bba92d 100755 --- a/src/common/media-common-utils.c +++ b/src/common/media-common-utils.c @@ -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, diff --git a/src/scanner/media-scanner-scan.c b/src/scanner/media-scanner-scan.c index 62e871f..5ae6a35 100755 --- a/src/scanner/media-scanner-scan.c +++ b/src/scanner/media-scanner-scan.c @@ -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) -- 2.7.4