Fix incorrect heap buffer access and initializations 91/237591/2 submit/tizen/20200710.083234
authorDewal Agarwal <d1.agarwal@samsung.com>
Wed, 1 Jul 2020 11:22:20 +0000 (16:52 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Fri, 10 Jul 2020 08:08:34 +0000 (13:38 +0530)
- This patch reverts "ASAN Issue fixes"
- Removes changes by "[Non-ACR][Fix Warnings]" as it is not required

Change-Id: I3a1d5dd424ca9f111dd8b724fa508af0a8637b9d

src/sync-service/SyncManager_RepositoryEngine.cpp

index 4d540544c6b8f2c8b769ea7ac18f97cf2ad761e9..071105ce31f82b4c2c280ab134a38ecc3db67392 100644 (file)
@@ -1694,6 +1694,7 @@ static char *UserValue = NULL;
 
 
 static void bundIterCb(const char *key, const char *value, void *user_data) {
+       LOG_LOGD("Callback");
        UserKey = const_cast<char *>(key);
        UserValue = const_cast<char *>(value);
 }
@@ -1701,8 +1702,6 @@ static void bundIterCb(const char *key, const char *value, void *user_data) {
 int
 RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, int UserId, const char *AppStatus, int JobId,
                                                const char *SyncType, int AccountId, const char *JobName, int Interval, int Option, bundle *UserData) {
-       bool user_data_available = false;
-
        sqlite3 *db = NULL;
        db = db_open();
        if (!db) {
@@ -1725,13 +1724,16 @@ RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, i
                char noRetry[16] = { 0, };
                snprintf(noRetry, sizeof(noRetry) - 1, "%d", (Option & SYNC_OPTION_NO_RETRY));
 
+               UserKey = NULL; UserValue = NULL;
                if (UserData) {
+                       LOG_LOGD("Bundle Iterate");
                        bundle_iterate(UserData, bundIterCb, NULL);
-               } else {
-                       UserKey = strdup("");
-                       UserValue = strdup("");
-                       user_data_available = true;
                }
+               char empty_string[2] = {'\0'};
+               if (UserKey == NULL)
+                       UserKey = empty_string;
+               if (UserValue == NULL)
+                       UserValue = empty_string;
 
                char *search_query = NULL;
                char *insert_query = NULL;
@@ -1745,11 +1747,6 @@ RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, i
                        LOG_LOGD("prepare : search query[%s] ret[%d] err_msg[%s]", search_query, ret, sqlite3_errmsg(db));
                        sqlite3_finalize(stmt);
                        sqlite3_free(search_query);
-                       if (user_data_available && UserKey && UserValue) {
-                               free(UserKey);
-                               free(UserValue);
-                               UserKey = UserValue = NULL;
-                       }
                        db_close(db);
                        LOG_LOGD("sync-manager DB close");
                        return -1;
@@ -1770,11 +1767,6 @@ RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, i
                                sqlite3_finalize(stmt);
                                sqlite3_free(search_query);
                                sqlite3_free(insert_query);
-                               if (user_data_available && UserKey && UserValue) {
-                                       free(UserKey);
-                                       free(UserValue);
-                                       UserKey = UserValue = NULL;
-                               }
                                db_close(db);
                                LOG_LOGD("sync-manager DB close");
                                return -1;
@@ -1787,11 +1779,6 @@ RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, i
                                sqlite3_finalize(stmt);
                                sqlite3_free(search_query);
                                sqlite3_free(insert_query);
-                               if (user_data_available && UserKey && UserValue) {
-                                       free(UserKey);
-                                       free(UserValue);
-                                       UserKey = UserValue = NULL;
-                               }
                                db_close(db);
                                LOG_LOGD("sync-manager DB close");
                                return -1;
@@ -1804,11 +1791,6 @@ RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, i
                        sqlite3_reset(stmt);
                        sqlite3_finalize(stmt);
                        sqlite3_free(search_query);
-                       if (user_data_available && UserKey && UserValue) {
-                               free(UserKey);
-                               free(UserValue);
-                               UserKey = UserValue = NULL;
-                       }
                        db_close(db);
                        LOG_LOGD("sync-manager DB close");
                        return UpdateSyncJobToDB(SvcAppId, UserId, AppStatus, JobId, SyncType, AccountId, JobName, Interval, Option, UserData);
@@ -1817,11 +1799,6 @@ RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, i
                        sqlite3_reset(stmt);
                        sqlite3_finalize(stmt);
                        sqlite3_free(search_query);
-                       if (user_data_available && UserKey && UserValue) {
-                               free(UserKey);
-                               free(UserValue);
-                               UserKey = UserValue = NULL;
-                       }
                        db_close(db);
                        LOG_LOGD("sync-manager DB close");
                        return -1;
@@ -1834,12 +1811,6 @@ RepositoryEngine::InsertSyncJobToDB(const char *SvcAppId, const char *PkgName, i
                LOG_LOGD("Insert sync job fail : invalid service-app-id");
        }
 
-       if (user_data_available && UserKey && UserValue) {
-               free(UserKey);
-               free(UserValue);
-               UserKey = UserValue = NULL;
-       }
-
        db_close(db);
 
        LOG_LOGD("sync-manager DB close");
@@ -1911,8 +1882,6 @@ RepositoryEngine::UpdateAppStatusToDB(const char *SvcAppId, const char *PkgName,
 int
 RepositoryEngine::UpdateSyncJobToDB(const char *SvcAppId, int UserId, const char *AppStatus, int JobId, const char *SyncType,
                                                int AccountId, const char *JobName, int Interval, int Option, bundle *UserData) {
-       bool user_data_available = false;
-
        sqlite3 *db = NULL;
        db = db_open();
        if (!db) {
@@ -1935,28 +1904,25 @@ RepositoryEngine::UpdateSyncJobToDB(const char *SvcAppId, int UserId, const char
                char noRetry[16] = { 0, };
                snprintf(noRetry, sizeof(noRetry) - 1, "%d", Option & SYNC_OPTION_NO_RETRY);
 
+               UserKey = NULL; UserValue = NULL;
                if (UserData) {
+                       LOG_LOGD("Bundle Iterate");
                        bundle_iterate(UserData, bundIterCb, NULL);
-               } else {
-                       UserKey = strdup("");
-                       UserValue = strdup("");
-                       user_data_available = true;
                }
 
+               char empty_string[2] = {'\0'};
+               if (UserKey == NULL)
+                       UserKey = empty_string;
+               if (UserValue == NULL)
+                       UserValue = empty_string;
+
                char *update_query = NULL;
                string SvcPkgId = SyncManager::GetInstance()->GetPkgIdByAppId(SvcAppId);
-
-               char *svc_pkg_id = new char[SvcPkgId.size() + 1];
-               copy(SvcPkgId.begin(), SvcPkgId.end(), svc_pkg_id);
-               svc_pkg_id[SvcPkgId.size() + 1] = '\0';
-
                update_query = sqlite3_mprintf("UPDATE %Q SET AppStatus=%Q, SyncJobId=%Q, AccountId=%Q, Interval=%Q, "
                                                                                "Expedit=%Q, NoRetry=%Q, UserDataKey=%Q, UserDataValue=%Q "
                                                                                "WHERE SvcAppId=%Q AND PkgName=%Q AND UserId=%Q AND SyncType=%Q AND JobName=%Q",
                                                                                SYNC_JOB_TABLE, AppStatus, jobId, accId, interval, expedit, noRetry, UserKey, UserValue,
-                                                                               SvcAppId, svc_pkg_id, uid, SyncType, JobName);
-
-               delete[] svc_pkg_id;
+                                                                               SvcAppId, SvcPkgId.c_str(), uid, SyncType, JobName);
 
                sqlite3_stmt *stmt = NULL;
                int ret = SQLITE_OK;
@@ -1966,11 +1932,6 @@ RepositoryEngine::UpdateSyncJobToDB(const char *SvcAppId, int UserId, const char
                        LOG_LOGD("prepare : update query[%s] ret[%d] err_msg[%s]", update_query, ret, sqlite3_errmsg(db));
                        sqlite3_finalize(stmt);
                        sqlite3_free(update_query);
-                       if (user_data_available && UserKey && UserValue) {
-                               free(UserKey);
-                               free(UserValue);
-                               UserKey = UserValue = NULL;
-                       }
                        db_close(db);
                        LOG_LOGD("sync-manager DB close");
                        return -1;
@@ -1984,11 +1945,6 @@ RepositoryEngine::UpdateSyncJobToDB(const char *SvcAppId, int UserId, const char
                        sqlite3_reset(stmt);
                        sqlite3_finalize(stmt);
                        sqlite3_free(update_query);
-                       if (user_data_available && UserKey && UserValue) {
-                               free(UserKey);
-                               free(UserValue);
-                               UserKey = UserValue = NULL;
-                       }
                        db_close(db);
                        LOG_LOGD("sync-manager DB close");
                        return -1;
@@ -2004,12 +1960,6 @@ RepositoryEngine::UpdateSyncJobToDB(const char *SvcAppId, int UserId, const char
                LOG_LOGD("update sync job fail : invalid service-app-id");
        }
 
-       if (user_data_available && UserKey && UserValue) {
-               free(UserKey);
-               free(UserValue);
-               UserKey = UserValue = NULL;
-       }
-
        db_close(db);
 
        LOG_LOGD("sync-manager DB close");