Fix a issue of static analysis
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_pkginfo.c
index 5add4b0..b5cac35 100644 (file)
@@ -86,6 +86,16 @@ static void __destroy_each_node(gpointer data, gpointer user_data)
        node = NULL;
 }
 
+static void __destroy_metadata_node(gpointer data)
+{
+       pkgmgrinfo_metadata_node_x *node = (pkgmgrinfo_metadata_node_x *)data;
+       if (node->key)
+               free(node->key);
+       if (node->value)
+               free(node->value);
+       free(node);
+}
+
 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data)
 {
        ret_if(data == NULL);
@@ -220,7 +230,7 @@ static const char join_privilege_info[] =
        "  ON pi.package=package_privilege_info.package";
 
 static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
-               const char *locale, char **query, GList **bind_params)
+               const char *locale, uid_t uid, char **query, GList **bind_params)
 {
        int joined = 0;
        char buf[MAX_QUERY_LEN] = { '\0' };
@@ -232,19 +242,19 @@ static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
        if (filter == NULL)
                return PMINFO_R_OK;
 
-       len += strlen(" WHERE 1=1 ");
        strncat(buf, " WHERE 1=1 ", MAX_QUERY_LEN - len - 1);
+       len += strlen(" WHERE 1=1 ");
        for (list = filter->list; list; list = list->next) {
-               joined |= __get_filter_condition(list->data, &condition,
+               joined |= __get_filter_condition(list->data, uid, &condition,
                                bind_params);
                if (condition == NULL)
                        continue;
 
-               len += strlen(" AND ");
                strncat(buf, " AND ", MAX_QUERY_LEN - len - 1);
+               len += strlen(" AND ");
 
-               len += strlen(condition);
                strncat(buf, condition, sizeof(buf) - len - 1);
+               len += strlen(condition);
                free(condition);
                condition = NULL;
        }
@@ -366,7 +376,7 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
        author_x *author = NULL;
        GList *bind_params = NULL;
        sqlite3 *db;
-       sqlite3_stmt *stmt;
+       sqlite3_stmt *stmt = NULL;
        pkgmgrinfo_filter_x *tmp_filter = NULL;
        bool is_check_storage = true;
 
@@ -374,7 +384,7 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
        if (dbpath == NULL)
                return PMINFO_R_ERROR;
 
-       ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
+       ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
        if (ret != SQLITE_OK) {
                _LOGD("failed to open db: %d", ret);
                free(dbpath);
@@ -419,7 +429,7 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
        strncat(query, query_from_clause, MAX_QUERY_LEN - query_len - 1);
        query_len += strlen(query_from_clause);
 
-       ret = _get_filtered_query(tmp_filter, locale, &constraints, &bind_params);
+       ret = _get_filtered_query(tmp_filter, locale, uid, &constraints, &bind_params);
        if (ret != PMINFO_R_OK) {
                LOGE("Failed to get WHERE clause");
                goto catch;
@@ -445,9 +455,8 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                info = calloc(1, sizeof(package_x));
                if (info == NULL) {
                        LOGE("out of memory");
-                       sqlite3_finalize(stmt);
-                       sqlite3_close_v2(db);
-                       return PMINFO_R_ERROR;
+                       ret = PMINFO_R_ERROR;
+                       goto catch;
                }
                idx = 0;
                _save_column_str(stmt, idx++, &info->package);
@@ -455,6 +464,7 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                                        (gconstpointer)info->package)) {
                        free(info->package);
                        free(info);
+                       info = NULL;
                        continue;
                }
                _save_column_str(stmt, idx++, &info->version);
@@ -488,10 +498,8 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                        /* TODO : author should be retrieved at package_localized_info */
                        author = calloc(1, sizeof(author_x));
                        if (author == NULL) {
-                               pkgmgrinfo_basic_free_package(info);
-                               sqlite3_finalize(stmt);
-                               sqlite3_close_v2(db);
-                               return PMINFO_R_ERROR;
+                               ret = PMINFO_R_ERROR;
+                               goto catch;
                        }
                        _save_column_str(stmt, idx++, &author->text);
                        _save_column_str(stmt, idx++, &author->email);
@@ -504,10 +512,8 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                        _save_column_str(stmt, idx++, &tmp_record);
 
                        if (_add_label_info_into_list(locale, tmp_record, &info->label)) {
-                               pkgmgrinfo_basic_free_package(info);
-                               sqlite3_finalize(stmt);
-                               sqlite3_close_v2(db);
-                               return PMINFO_R_ERROR;
+                               ret = PMINFO_R_ERROR;
+                               goto catch;
                        }
                }
 
@@ -515,10 +521,8 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                        tmp_record = NULL;
                        _save_column_str(stmt, idx++, &tmp_record);
                        if (_add_icon_info_into_list(locale, tmp_record, &info->icon)) {
-                               pkgmgrinfo_basic_free_package(info);
-                               sqlite3_finalize(stmt);
-                               sqlite3_close_v2(db);
-                               return PMINFO_R_ERROR;
+                               ret = PMINFO_R_ERROR;
+                               goto catch;
                        }
                }
 
@@ -527,20 +531,16 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                        _save_column_str(stmt, idx++, &tmp_record);
                        if (_pkginfo_add_description_info_into_list(locale, tmp_record,
                                        &info->description)) {
-                               pkgmgrinfo_basic_free_package(info);
-                               sqlite3_finalize(stmt);
-                               sqlite3_close_v2(db);
-                               return PMINFO_R_ERROR;
+                               ret = PMINFO_R_ERROR;
+                               goto catch;
                        }
                }
 
                if (flag & PMINFO_PKGINFO_GET_PRIVILEGE) {
                        if (_pkginfo_get_privilege(db, info->package,
                                                &info->privileges)) {
-                               pkgmgrinfo_basic_free_package(info);
-                               sqlite3_finalize(stmt);
-                               sqlite3_close_v2(db);
-                               return PMINFO_R_ERROR;
+                               ret = PMINFO_R_ERROR;
+                               goto catch;
                        }
                }
 
@@ -570,7 +570,8 @@ catch:
 
        g_list_free_full(bind_params, free);
        sqlite3_close_v2(db);
-       sqlite3_finalize(stmt);
+       if (stmt)
+               sqlite3_finalize(stmt);
 
        return ret;
 }
@@ -605,6 +606,7 @@ static int _pkginfo_get_filtered_foreach_pkginfo(uid_t uid,
                ret = pkgmgrinfo_pkginfo_filter_create((void *)&tmp_filter);
                if (ret != PMINFO_R_OK) {
                        _LOGE("Failed to create filter");
+                       g_hash_table_destroy(list);
                        return PMINFO_R_ERROR;
                }
        }
@@ -1096,6 +1098,24 @@ API int pkgmgrinfo_pkginfo_get_zip_mount_file(pkgmgrinfo_pkginfo_h handle, char
        return PMINFO_R_OK;
 }
 
+API int pkgmgrinfo_pkginfo_get_external_image_path(pkgmgrinfo_pkginfo_h handle, char **ext_image_path)
+{
+       pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
+
+       retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
+       retvm_if(ext_image_path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
+
+       if (info->pkg_info == NULL)
+               return PMINFO_R_ERROR;
+
+       if (info->pkg_info->external_path == NULL)
+               return PMINFO_R_ENOENT;
+
+       *ext_image_path = (char *)info->pkg_info->external_path;
+
+       return PMINFO_R_OK;
+}
+
 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
 {
        char *val;
@@ -1650,6 +1670,8 @@ API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
                g_slist_free(filter->list);
        }
 
+       g_slist_free_full(filter->list_metadata, __destroy_metadata_node);
+
        free(filter);
 
        return PMINFO_R_OK;
@@ -1822,6 +1844,7 @@ API int pkgmgrinfo_pkginfo_usr_filter_count(pkgmgrinfo_pkginfo_filter_h handle,
                                PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false);
                if (ret != PMINFO_R_OK) {
                        free(locale);
+                       g_hash_table_destroy(list);
                        return PMINFO_R_ERROR;
                }
        }
@@ -1932,7 +1955,7 @@ int __compare_package_version(const char *version, int *major,
        *major = atoi(major_str);
        *minor = atoi(minor_str);
        *macro = 0;
-       *minor = 0;
+       *nano = 0;
        macro_str = strtok_r(NULL, ".", &save_str);
        if (macro_str == NULL) {
                _LOGD("macro version is NULL");