Fix static analysis issue
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_certinfo.c
index d86be31..3b346bc 100644 (file)
@@ -8,8 +8,6 @@
 #include <sqlite3.h>
 #include <glib.h>
 
-#include <db-util.h>
-
 #include "pkgmgr-info.h"
 #include "pkgmgrinfo_debug.h"
 #include "pkgmgrinfo_private.h"
@@ -125,7 +123,7 @@ API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(const char *lhs_package_id,
        }
 
        /* open unified global cert db */
-       dbpath = getUserPkgCertDBPathUID(GLOBAL_USER);
+       dbpath = getUserPkgCertDBPath();
        if (dbpath == NULL)
                return PMINFO_R_ERROR;
 
@@ -363,7 +361,7 @@ static int _pkginfo_get_certinfo(const char *pkgid, pkgmgr_certinfo_x *info)
        char *dbpath;
 
        /* open unified global cert db */
-       dbpath = getUserPkgCertDBPathUID(GLOBAL_USER);
+       dbpath = getUserPkgCertDBPath();
        if (dbpath == NULL)
                return PMINFO_R_ERROR;
 
@@ -464,6 +462,8 @@ API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_i
        retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
        pkgmgr_instcertinfo_x *certinfo = NULL;
        certinfo = (pkgmgr_instcertinfo_x *)handle;
+       if (certinfo->cert_info[cert_type])
+               free(certinfo->cert_info[cert_type]);
        (certinfo->cert_info)[cert_type] = strdup(cert_value);
        return PMINFO_R_OK;
 }
@@ -512,7 +512,13 @@ static int _pkginfo_save_cert_info(sqlite3 *db, const char *pkgid,
        }
 
        idx = 1;
-       sqlite3_bind_text(stmt, idx++, pkgid, -1, SQLITE_STATIC);
+       ret = sqlite3_bind_text(stmt, idx++, pkgid, -1, SQLITE_STATIC);
+       if (ret != SQLITE_OK) {
+               _LOGE("bind failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return PMINFO_R_ERROR;
+       }
+
        for (i = 0; i < MAX_CERT_TYPE; i++) {
                if (sqlite3_bind_text(stmt, idx++, cert_info[i], -1,
                                SQLITE_STATIC)) {
@@ -531,7 +537,13 @@ static int _pkginfo_save_cert_info(sqlite3 *db, const char *pkgid,
                        _LOGE("prepare error: %s", sqlite3_errmsg(db));
                        return PMINFO_R_ERROR;
                }
-               sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_STATIC);
+
+               if (sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_STATIC)) {
+                       _LOGE("bind error: %s", sqlite3_errmsg(db));
+                       sqlite3_finalize(stmt);
+                       return PMINFO_R_ERROR;
+               }
+
                ret = sqlite3_step(stmt);
                sqlite3_finalize(stmt);
        }
@@ -571,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) {
@@ -606,7 +633,7 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
        _check_create_cert_db();
 
        /* open unified global cert db */
-       dbpath = getUserPkgCertDBPathUID(GLOBAL_USER);
+       dbpath = getUserPkgCertDBPath();
        if (dbpath == NULL)
                return PMINFO_R_ERROR;
 
@@ -618,7 +645,7 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
        }
        free(dbpath);
 
-       ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
+       ret = sqlite3_exec(db, "BEGIN DEFERRED", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to begin transaction");
                sqlite3_close_v2(db);
@@ -627,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;
        }
@@ -642,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;
        }
@@ -717,7 +750,7 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
        }
 
        /* open unified global cert db */
-       dbpath = getUserPkgCertDBPathUID(GLOBAL_USER);
+       dbpath = getUserPkgCertDBPath();
        if (dbpath == NULL)
                return PMINFO_R_ERROR;
 
@@ -729,7 +762,7 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
        }
        free(dbpath);
 
-       ret = sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
+       ret = sqlite3_exec(db, "BEGIN DEFERRED", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("failed to begin transaction");
                sqlite3_close_v2(db);
@@ -738,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;
        }
@@ -746,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;
        }