From 923c0a1db9905d7d727b13eaf4174e66699e940c Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Mon, 9 Dec 2019 16:23:21 +0900 Subject: [PATCH] Fix static analysis issue - Fix possibility of memory leak - Fix possibility of null pointer dereference - Check return value Change-Id: I1876315e232be63f252548898cdcbe6b7345e1ce Signed-off-by: Ilho Kim --- parser/src/pkgmgr_parser_deprecated.c | 12 ++++++++++ src/pkgmgrinfo_appinfo.c | 13 +++++++++++ src/pkgmgrinfo_certinfo.c | 41 ++++++++++++++++++++++++++++------- src/pkgmgrinfo_pkginfo.c | 5 +++++ src/pkgmgrinfo_plugininfo.c | 2 +- 5 files changed, 64 insertions(+), 9 deletions(-) diff --git a/parser/src/pkgmgr_parser_deprecated.c b/parser/src/pkgmgr_parser_deprecated.c index 080e909..d717409 100644 --- a/parser/src/pkgmgr_parser_deprecated.c +++ b/parser/src/pkgmgr_parser_deprecated.c @@ -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); } diff --git a/src/pkgmgrinfo_appinfo.c b/src/pkgmgrinfo_appinfo.c index ad93c86..b50e08d 100644 --- a/src/pkgmgrinfo_appinfo.c +++ b/src/pkgmgrinfo_appinfo.c @@ -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); diff --git a/src/pkgmgrinfo_certinfo.c b/src/pkgmgrinfo_certinfo.c index 6f3de1f..3b346bc 100644 --- a/src/pkgmgrinfo_certinfo.c +++ b/src/pkgmgrinfo_certinfo.c @@ -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; } diff --git a/src/pkgmgrinfo_pkginfo.c b/src/pkgmgrinfo_pkginfo.c index a8ba178..dc3048a 100644 --- a/src/pkgmgrinfo_pkginfo.c +++ b/src/pkgmgrinfo_pkginfo.c @@ -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, diff --git a/src/pkgmgrinfo_plugininfo.c b/src/pkgmgrinfo_plugininfo.c index 01ff505..4e96dd4 100644 --- a/src/pkgmgrinfo_plugininfo.c +++ b/src/pkgmgrinfo_plugininfo.c @@ -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; -- 2.7.4