Check file before dlopen 45/232045/4
authorMinje Ahn <minje.ahn@samsung.com>
Tue, 28 Apr 2020 01:22:32 +0000 (10:22 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Tue, 28 Apr 2020 06:32:55 +0000 (15:32 +0900)
Change-Id: Ie5691150dc0ee7bd76d138a417726ad20b1af307
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
include/mm_file_debug.h
mm_file.c

index 55ee214..dddd2b9 100755 (executable)
@@ -168,6 +168,18 @@ extern "C" {
                } while (0)
 #endif
 
+#define mm_file_retv_if_fails(mode, expr, val) do { \
+                       if (!(expr)) { \
+                               return (val); \
+                       } \
+               } while (0)
+
+#define mm_file_retvm_if_fails(mode, expr, val) do { \
+                               if (!(expr)) { \
+                                       debug_error(mode, "ERROR [%s]", #expr);  \
+                                       return (val); \
+                               } \
+                       } while (0)
 
 #ifdef __cplusplus
 }
index 7f5eb37..7bb7b5b 100644 (file)
--- a/mm_file.c
+++ b/mm_file.c
@@ -291,23 +291,21 @@ static void _unload_dynamic_functions(MMFILE_FUNC_HANDLE *pHandle)
  * local functions.
  */
 static int
-_is_file_exist(const char *filename)
+_is_file_readable(const char *filename)
 {
-       int ret = FILEINFO_ERROR_NONE;
-       if (filename) {
-               const char *to_access = (strstr(filename, "file://") != NULL) ? filename + 7 : filename;
-               ret = access(to_access, R_OK);
-               if (ret < 0) {
-                       if (errno == EACCES || errno == EPERM) {
-                               debug_error(DEBUG, "Permission denied [%s]", to_access);
-                               ret = FILEINFO_ERROR_PERMISSION_DENIED;
-                       } else {
-                               debug_error(DEBUG, "Not exist file [%s]", to_access);
-                               ret = FILEINFO_ERROR_FILE_NOT_FOUND;
-                       }
+       mm_file_retvm_if_fails(DEBUG, filename, FILEINFO_ERROR_INVALID_ARGUMENT);
+
+       if (access(filename, R_OK) == -1) {
+               if (errno == EACCES || errno == EPERM) {
+                       debug_error(DEBUG, "Permission denied [%s]", filename);
+                       return FILEINFO_ERROR_PERMISSION_DENIED;
+               } else {
+                       debug_error(DEBUG, "Not exist file [%s]", filename);
+                       return FILEINFO_ERROR_FILE_NOT_FOUND;
                }
        }
-       return ret;
+
+       return FILEINFO_ERROR_NONE;
 }
 
 static int
@@ -768,15 +766,10 @@ int mm_file_create_tag_attrs(MMHandleType *tag_attrs, const char *filename)
                debug_error(DEBUG, "Invalid arguments [tag null]\n");
                return FILEINFO_ERROR_INVALID_ARGUMENT;
        }
-       if (filename == NULL) {
-               debug_error(DEBUG, "Invalid arguments [filename null]\n");
-               return FILEINFO_ERROR_INVALID_ARGUMENT;
-       }
-       if (strlen(filename) == 0)      {
-               debug_error(DEBUG, "Invalid arguments [filename size 0]\n");
-               return FILEINFO_ERROR_INVALID_ARGUMENT;
-       }
 
+       ret = _is_file_readable(filename);
+       if (ret != FILEINFO_ERROR_NONE)
+               return ret;
 
 #ifdef __MMFILE_DYN_LOADING__
        MMFILE_FUNC_HANDLE func_handle;
@@ -791,11 +784,6 @@ int mm_file_create_tag_attrs(MMHandleType *tag_attrs, const char *filename)
        /*set source file infomation*/
        MM_FILE_SET_MEDIA_FILE_SRC(src, filename);
 
-       ret = _is_file_exist(filename);
-       if (ret != FILEINFO_ERROR_NONE) {
-               goto END;
-       }
-
        /*set attrs*/
        ret = mm_attrs_new(g_tag_attrs, ARRAY_SIZE(g_tag_attrs), "tag", NULL, NULL, &attrs);
        if (ret) {
@@ -864,15 +852,10 @@ static int __create_content_attrs(MMHandleType *contents_attrs, const char *file
                debug_error(DEBUG, "Invalid arguments [contents null]\n");
                return FILEINFO_ERROR_INVALID_ARGUMENT;
        }
-       if (filename == NULL) {
-               debug_error(DEBUG, "Invalid arguments [filename null]\n");
-               return FILEINFO_ERROR_INVALID_ARGUMENT;
-       }
-       if (strlen(filename) == 0)      {
-               debug_error(DEBUG, "Invalid arguments [filename size 0]\n");
-               return FILEINFO_ERROR_INVALID_ARGUMENT;
-       }
 
+       ret = _is_file_readable(filename);
+       if (ret != FILEINFO_ERROR_NONE)
+               return ret;
 
 #ifdef __MMFILE_DYN_LOADING__
        MMFILE_FUNC_HANDLE func_handle;
@@ -897,11 +880,6 @@ static int __create_content_attrs(MMHandleType *contents_attrs, const char *file
        /*set source file infomation*/
        MM_FILE_SET_MEDIA_FILE_SRC(src, filename);
 
-       ret = _is_file_exist(filename);
-       if (ret != FILEINFO_ERROR_NONE) {
-               goto END;
-       }
-
        /*set attrs*/
        ret = mm_attrs_new(g_content_attrs, ARRAY_SIZE(g_content_attrs), "content", NULL, NULL, &attrs);
        if (ret) {
@@ -1096,11 +1074,15 @@ int mm_file_get_stream_info(const char *filename, int *audio_stream_num, int *vi
 
        debug_fenter(RELEASE);
 
-       if (filename == NULL || strlen(filename) == 0 || audio_stream_num == NULL || video_stream_num == NULL) {
+       if (audio_stream_num == NULL || video_stream_num == NULL) {
                debug_error(DEBUG, "Invalid arguments\n");
                return FILEINFO_ERROR_INVALID_ARGUMENT;
        }
 
+       ret = _is_file_readable(filename);
+       if (ret != FILEINFO_ERROR_NONE)
+               return ret;
+
 #ifdef __MMFILE_DYN_LOADING__
        MMFILE_FUNC_HANDLE func_handle;
 
@@ -1114,10 +1096,6 @@ int mm_file_get_stream_info(const char *filename, int *audio_stream_num, int *vi
        /*set source file infomation*/
        MM_FILE_SET_MEDIA_FILE_SRC(src, filename);
 
-       ret = _is_file_exist(filename);
-       if (ret != FILEINFO_ERROR_NONE) {
-               goto END;
-       }
 
        parse.type = MM_FILE_PARSE_TYPE_SIMPLE;
        ret = _get_contents_info(NULL, &src, &parse);
@@ -1135,7 +1113,6 @@ int mm_file_get_stream_info(const char *filename, int *audio_stream_num, int *vi
        *audio_stream_num = parse.audio_track_num;
        *video_stream_num = parse.video_track_num;
 
-END:
 #ifdef __MMFILE_DYN_LOADING__
        _unload_dynamic_functions(&func_handle);
 #endif
@@ -1258,6 +1235,10 @@ int mm_file_check_uhqa(const char *filename, bool *is_uhqa)
        MMFILE_PARSE_INFO parse = {0, };
        int ret = 0;
 
+       ret = _is_file_readable(filename);
+       if (ret != FILEINFO_ERROR_NONE)
+               return ret;
+
 #ifdef __MMFILE_DYN_LOADING__
        MMFILE_FUNC_HANDLE func_handle;
 
@@ -1267,24 +1248,10 @@ int mm_file_check_uhqa(const char *filename, bool *is_uhqa)
                return FILEINFO_ERROR_FILE_INTERNAL;
        }
 #endif
-       if (filename == NULL) {
-               ret = FILEINFO_ERROR_INVALID_ARGUMENT;
-               goto END;
-       } else {
-               if (strlen(filename) == 0) {
-                       ret = FILEINFO_ERROR_INVALID_ARGUMENT;
-                       goto END;
-               }
-       }
 
        /*set source file infomation*/
        MM_FILE_SET_MEDIA_FILE_SRC(src, filename);
 
-       ret = _is_file_exist(filename);
-       if (ret != FILEINFO_ERROR_NONE) {
-               goto END;
-       }
-
        /*set attrs*/
        ret = mm_attrs_new(g_content_attrs, ARRAY_SIZE(g_content_attrs), "content", NULL, NULL, &attrs);
        if (ret) {
@@ -1303,7 +1270,6 @@ int mm_file_check_uhqa(const char *filename, bool *is_uhqa)
        }
 
        mm_attrs_free(attrs);
-       attrs = NULL;
 
 END:
 #ifdef __MMFILE_DYN_LOADING__