Fix static analysis issue 94/219694/8
authorIlho Kim <ilho159.kim@samsung.com>
Mon, 9 Dec 2019 07:23:21 +0000 (16:23 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Tue, 10 Dec 2019 08:18:03 +0000 (17:18 +0900)
- Fix possibility of memory leak
- Fix possibility of null pointer dereference
- Check return value

Change-Id: I1876315e232be63f252548898cdcbe6b7345e1ce
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
parser/src/pkgmgr_parser_deprecated.c
src/pkgmgrinfo_appinfo.c
src/pkgmgrinfo_certinfo.c
src/pkgmgrinfo_pkginfo.c
src/pkgmgrinfo_plugininfo.c

index 080e909..d717409 100644 (file)
@@ -402,6 +402,10 @@ static void __ps_process_mime(gpointer data, gpointer user_data)
        snprintf(ad->mime, sizeof(ad->mime), "%s", mime);
 
        appcontrol = calloc(1, sizeof(appcontrol_x));
+       if (appcontrol == NULL) {
+               _LOGD("Malloc Failed\n");
+               return;
+       }
        if (strlen(ad->operation))
                appcontrol->operation = strdup(ad->operation);
        if (strlen(ad->uri))
@@ -422,6 +426,10 @@ static void __ps_process_uri(gpointer data, gpointer user_data)
                g_list_foreach(ad->mimes, __ps_process_mime, user_data);
        } else {
                appcontrol = calloc(1, sizeof(appcontrol_x));
+               if (appcontrol == NULL) {
+                       _LOGD("Malloc Failed\n");
+                       return;
+               }
                if (strlen(ad->operation))
                        appcontrol->operation = strdup(ad->operation);
                appcontrol->uri = strdup(ad->uri);
@@ -443,6 +451,10 @@ static void __ps_process_operation(gpointer data, gpointer user_data)
                g_list_foreach(ad->mimes, __ps_process_mime, user_data);
        } else {
                appcontrol = calloc(1, sizeof(appcontrol_x));
+               if (appcontrol == NULL) {
+                       _LOGD("Malloc Failed\n");
+                       return;
+               }
                appcontrol->operation = strdup(ad->operation);
                ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
        }
index ad93c86..b50e08d 100644 (file)
@@ -786,8 +786,21 @@ static int _pkgmgrinfo_get_appinfo(const char *appid, uid_t uid,
        }
 
        info->app_info = (application_x *)g_hash_table_lookup(list, appid);
+       if (!info->app_info || !info->app_info->package) {
+               _LOGD("appinfo for [%s] is not existed for user [%d]",
+                               appid, uid);
+               g_hash_table_destroy(list);
+               free(locale);
+               return PMINFO_R_ENOENT;
+       }
        info->locale = locale;
        info->package = strdup(info->app_info->package);
+       if (!info->package) {
+               _LOGE("out of memory");
+               g_hash_table_destroy(list);
+               free(locale);
+               return PMINFO_R_ERROR;
+       }
 
        /* just free list only */
        g_hash_table_steal(list, (gconstpointer)appid);
index 6f3de1f..3b346bc 100644 (file)
@@ -583,9 +583,24 @@ static int _pkginfo_save_cert_index_info(sqlite3 *db, char *cert_info[])
                if (cert_info[i] == NULL)
                        continue;
                idx = 1;
-               sqlite3_bind_text(stmt, idx++, cert_info[i], -1, SQLITE_STATIC);
-               sqlite3_bind_text(stmt, idx++, cert_info[i], -1, SQLITE_STATIC);
-               sqlite3_bind_text(stmt, idx++, cert_info[i], -1, SQLITE_STATIC);
+               ret = sqlite3_bind_text(stmt, idx++, cert_info[i], -1, SQLITE_STATIC);
+               if (ret != SQLITE_OK) {
+                       _LOGE("bind failed: %s", sqlite3_errmsg(db));
+                       sqlite3_finalize(stmt);
+                       return PMINFO_R_ERROR;
+               }
+               ret = sqlite3_bind_text(stmt, idx++, cert_info[i], -1, SQLITE_STATIC);
+               if (ret != SQLITE_OK) {
+                       _LOGE("bind failed: %s", sqlite3_errmsg(db));
+                       sqlite3_finalize(stmt);
+                       return PMINFO_R_ERROR;
+               }
+               ret = sqlite3_bind_text(stmt, idx++, cert_info[i], -1, SQLITE_STATIC);
+               if (ret != SQLITE_OK) {
+                       _LOGE("bind failed: %s", sqlite3_errmsg(db));
+                       sqlite3_finalize(stmt);
+                       return PMINFO_R_ERROR;
+               }
 
                ret = sqlite3_step(stmt);
                if (ret != SQLITE_DONE) {
@@ -639,14 +654,18 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
 
        if (_pkginfo_save_cert_index_info(db, info->cert_info)) {
                _LOGE("failed to save cert index info, rollback now");
-               sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               ret = sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               if (ret != SQLITE_OK)
+                       LOGE("Rollback is failed. error(%s)", sqlite3_errmsg(db));
                sqlite3_close_v2(db);
                return PMINFO_R_ERROR;
        }
 
        if (_pkginfo_save_cert_info(db, pkgid, info->cert_info)) {
                _LOGE("failed to save cert info, rollback now");
-               sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               ret = sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               if (ret != SQLITE_OK)
+                       LOGE("Rollback is failed. error(%s)", sqlite3_errmsg(db));
                sqlite3_close_v2(db);
                return PMINFO_R_ERROR;
        }
@@ -654,7 +673,9 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
        ret = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to commit transaction, rollback now");
-               sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               ret = sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               if (ret != SQLITE_OK)
+                       LOGE("Rollback is failed. error(%s)", sqlite3_errmsg(db));
                sqlite3_close_v2(db);
                return PMINFO_R_ERROR;
        }
@@ -750,7 +771,9 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
 
        if (_pkginfo_delete_certinfo(db, pkgid)) {
                _LOGE("failed to delete certinfo of %s, rollback now", pkgid);
-               sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               ret = sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               if (ret != SQLITE_OK)
+                       LOGE("Rollback is failed. error(%s)", sqlite3_errmsg(db));
                sqlite3_close_v2(db);
                return PMINFO_R_ERROR;
        }
@@ -758,7 +781,9 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
        ret = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to commit transaction, rollback now");
-               sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               ret = sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
+               if (ret != SQLITE_OK)
+                       LOGE("Rollback is failed. error(%s)", sqlite3_errmsg(db));
                sqlite3_close_v2(db);
                return PMINFO_R_ERROR;
        }
index a8ba178..dc3048a 100644 (file)
@@ -253,6 +253,11 @@ static int _pkginfo_get_privilege(sqlite3 *db, const char *pkgid,
 
        while (sqlite3_step(stmt) == SQLITE_ROW) {
                privilege = calloc(1, sizeof(privilege_x));
+               if (!privilege) {
+                       LOGE("failed to alloc memory");
+                       sqlite3_finalize(stmt);
+                       return PMINFO_R_ERROR;
+               }
                _save_column_str(stmt, 0, &privilege->value);
                _save_column_str(stmt, 1, &privilege->type);
                *privileges = g_list_append(*privileges,
index 01ff505..4e96dd4 100644 (file)
@@ -125,11 +125,11 @@ API int pkgmgrinfo_plugininfo_foreach_plugininfo(const char *pkgid,
                if (ret != 0)
                        break;
        }
-       g_list_free_full(plugin_list, _free_plugin);
 
 catch:
        sqlite3_finalize(stmt);
        sqlite3_close_v2(db);
+       g_list_free_full(plugin_list, _free_plugin);
 
        return ret;