Fix Coverity Issues
[platform/core/location/geofence-server.git] / geofence-server / src / geofence_server_db.c
index fabe33c..b405bf2 100755 (executable)
@@ -20,7 +20,7 @@
 #include <sys/stat.h>
 #include <string.h>
 #include <bluetooth.h>
-#include <wifi.h>
+#include <wifi-manager.h>
 #include <tzplatform_config.h>
 #include <sys/types.h>
 #include <fcntl.h>
@@ -67,12 +67,13 @@ static struct {
        .handle = NULL,
 };
 
-#define SQLITE3_RETURN(ret, msg, state) \
+#define SQLITE3_RETURN(ret, msg, state, query) \
        if (ret != SQLITE_OK) { \
                LOGI_GEOFENCE("sqlite3 Error[%d] : %s", ret, msg); \
                sqlite3_reset(state); \
                sqlite3_clear_bindings(state); \
                sqlite3_finalize(state); \
+               sqlite3_free(query); \
                return FENCE_ERR_SQLITE_FAIL; \
        }
 
@@ -193,6 +194,7 @@ static inline int __geofence_manager_db_create_places_table(void)
        if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
                LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
                sqlite3_free(ddl);
+               sqlite3_free(err);
                return FENCE_ERR_SQLITE_FAIL;
        }
 
@@ -213,6 +215,7 @@ static inline int __geofence_manager_db_create_geofence_table(void)
        if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
                LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
                sqlite3_free(ddl);
+               sqlite3_free(err);
                return FENCE_ERR_SQLITE_FAIL;
        }
 
@@ -233,6 +236,7 @@ static inline int __geofence_manager_db_create_geocoordinate_table(void)
        if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
                LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
                sqlite3_free(ddl);
+               sqlite3_free(err);
                return FENCE_ERR_SQLITE_FAIL;
        }
 
@@ -253,6 +257,7 @@ static inline int __geofence_manager_db_create_wifi_data_table(void)
        if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
                LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
                sqlite3_free(ddl);
+               sqlite3_free(err);
                return FENCE_ERR_SQLITE_FAIL;
        }
 
@@ -274,6 +279,7 @@ static inline int __geofence_manager_db_create_bssid_table(void)
        if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
                LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
                sqlite3_free(ddl);
+               sqlite3_free(err);
                return FENCE_ERR_SQLITE_FAIL;
        }
 
@@ -351,6 +357,7 @@ static int __geofence_manager_db_insert_bssid_info(const int fence_id, char *bss
        }
        if (count > 0) {
                LOGI_GEOFENCE("count = %d", count);
+               sqlite3_free(query);
                return FENCE_ERR_NONE;
        }
 
@@ -362,13 +369,13 @@ static int __geofence_manager_db_insert_bssid_info(const int fence_id, char *bss
        }
 
        ret = sqlite3_bind_int(state, ++index, fence_id);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_text(state, ++index, bssid_info, -1, SQLITE_STATIC);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_text(state, ++index, ssid, -1, SQLITE_STATIC);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_step(state);
        if (ret != SQLITE_DONE) {
@@ -420,10 +427,10 @@ static int __geofence_manager_db_insert_wifi_data_info(gpointer data, gpointer u
        }
 
        ret = sqlite3_bind_int(state, ++index, *fence_id);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_text(state, ++index, wifi_info->bssid, -1, SQLITE_STATIC);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_step(state);
        if (ret != SQLITE_DONE) {
@@ -618,8 +625,8 @@ void __geofence_manager_generate_password(char *password)
        if (ret != BT_ERROR_NONE)
                LOGD_GEOFENCE("bt address get fail %d", ret);
 
-       ret = wifi_get_mac_address(&wifi_address);
-       if (ret != WIFI_ERROR_NONE)
+       ret = wifi_manager_get_mac_address(&wifi_address);
+       if (ret != WIFI_MANAGER_ERROR_NONE)
                LOGD_GEOFENCE("wifi address get fail %d", ret);
 
        if (bt_address) {
@@ -772,13 +779,13 @@ int geofence_manager_set_place_info(place_info_s *place_info, int *place_id)
        LOGD_GEOFENCE("appid[%s] access_type[%d] place_name[%s]", place_info->appid, place_info->access_type, place_info->place_name);
 
        ret = sqlite3_bind_int(state, ++index, place_info->access_type);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_text(state, ++index, place_info->place_name, -1, SQLITE_STATIC);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_text(state, ++index, place_info->appid, -1, SQLITE_STATIC);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_step(state);
        if (ret != SQLITE_DONE) {
@@ -820,25 +827,25 @@ int geofence_manager_set_common_info(fence_common_info_s *fence_info, int *fence
                return FENCE_ERR_PREPARE;
        }
 
-       LOGD_GEOFENCE("place_id[%d], enable[%d], appid[%s] geofence_type[%d] access_type[%d] running_status[%d]", fence_info->place_id, fence_info->enable, fence_info->appid, fence_info->running_status, fence_info->type, fence_info->access_type, fence_info->place_id);
+       LOGD_GEOFENCE("place_id[%d], enable[%d], appid[%s] geofence_type[%d] access_type[%d] running_status[%d]", fence_info->place_id, fence_info->enable, fence_info->appid, fence_info->type, fence_info->access_type, fence_info->running_status);
 
        ret = sqlite3_bind_int(state, ++index, fence_info->place_id);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_int(state, ++index, fence_info->enable);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_text(state, ++index, fence_info->appid, -1, SQLITE_STATIC);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_int(state, ++index, fence_info->type);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_int(state, ++index, fence_info->access_type);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_int(state, ++index, fence_info->running_status);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_step(state);
        if (ret != SQLITE_DONE) {
@@ -1130,7 +1137,7 @@ int geofence_manager_set_geocoordinate_info(int fence_id, geocoordinate_info_s *
        }
 
        ret = sqlite3_bind_int(state, ++index, fence_id);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
 #ifdef SUPPORT_ENCRYPTION
        if (password == NULL)
@@ -1142,34 +1149,39 @@ int geofence_manager_set_geocoordinate_info(int fence_id, geocoordinate_info_s *
        ret = sqlite3_bind_text(state, ++index, data_name_lat, -1, SQLITE_STATIC);
 
        /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->latitude);*/
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = snprintf(data_name_lon, MAX_DATA_NAME, "%lf", geocoordinate_info->longitude);
        if (ret < 0) {
                LOGD_GEOFENCE("ERROR: String will be truncated");
+               sqlite3_free(query);
+               sqlite3_finalize(state);
                return FENCE_ERR_STRING_TRUNCATED;
        }
 
        ret = sqlite3_bind_text(state, ++index, data_name_lon, -1, SQLITE_STATIC);
        /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->longitude);*/
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = snprintf(data_name_rad, MAX_DATA_NAME, "%lf", geocoordinate_info->radius);
        if (ret < 0) {
                LOGD_GEOFENCE("ERROR: String will be truncated");
+               sqlite3_free(query);
+               sqlite3_finalize(state);
                return FENCE_ERR_STRING_TRUNCATED;
        }
 
        ret = sqlite3_bind_text(state, ++index, data_name_rad, -1, SQLITE_STATIC);
        /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->radius);*/
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_bind_text(state, ++index, geocoordinate_info->address, -1, SQLITE_STATIC);
-       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
+       SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
 
        ret = sqlite3_step(state);
        if (ret != SQLITE_DONE) {
                LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
+               sqlite3_free(query);
                sqlite3_finalize(state);
                return FENCE_ERR_SQLITE_FAIL;
        }
@@ -1217,6 +1229,10 @@ int geofence_manager_get_geocoordinate_info(int fence_id, geocoordinate_info_s *
        }
 
        *geocoordinate_info = (geocoordinate_info_s *)g_malloc0(sizeof(geocoordinate_info_s));
+       if (*geocoordinate_info ==  NULL) {
+               sqlite3_finalize(state);
+               sqlite3_free(query);
+       }
        g_return_val_if_fail(*geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
 
 #ifdef SUPPORT_ENCRYPTION
@@ -1314,6 +1330,10 @@ int geofence_manager_get_ap_info(const int fence_id, GList **ap_list)
                        break;
                }
                wifi_info = g_slice_new0(wifi_info_s);
+               if (wifi_info == NULL) {
+                       sqlite3_free(query2);
+                       sqlite3_finalize(state);
+               }
                g_return_val_if_fail(wifi_info, -1);
                if (wifi_info) {
                        bssid = (const char *) sqlite3_column_text(state, 1);
@@ -1359,7 +1379,11 @@ int geofence_manager_get_place_info(int place_id, place_info_s **place_info)
                return FENCE_ERR_SQLITE_FAIL;
        }
        *place_info = (place_info_s *)g_malloc0(sizeof(place_info_s));
-       g_return_val_if_fail(*place_info, FENCE_ERR_INVALID_PARAMETER);
+       if (*place_info == NULL) {
+               sqlite3_free(query);
+               sqlite3_finalize(state);
+       }
+       g_return_val_if_fail(*place_info, FENCE_ERR_INTERNAL);
 
        data_name = (char *)sqlite3_column_text(state, ++index);
        if (!data_name || !strlen(data_name))
@@ -1471,6 +1495,10 @@ int geofence_manager_get_bssid_info(const int fence_id, bssid_info_s **bssid_inf
                        break;
                }
                bssid_info_from_db = g_slice_new0(bssid_info_s);
+               if (bssid_info_from_db == NULL) {
+                       sqlite3_free(query2);
+                       sqlite3_finalize(state);
+               }
                g_return_val_if_fail(bssid_info_from_db, -1);
                if (bssid_info_from_db) {
                        bssid = (const char *)sqlite3_column_text(state, 1);
@@ -2040,6 +2068,10 @@ int geofence_manager_get_access_type(int fence_id, int place_id, access_type_e *
 
        LOGD_GEOFENCE("current fence id is [%d]", fence_id);
        LOGD_GEOFENCE("current place id is [%d]", place_id);
+
+       if (query == NULL)
+               return FENCE_ERR_INVALID_PARAMETER;
+
        ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
        if (ret != SQLITE_OK) {
                LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
@@ -2411,6 +2443,7 @@ int geofence_manager_reset(void)
                return FENCE_ERR_SQLITE_FAIL;
        }
        sqlite3_reset(state);
+       sqlite3_finalize(state);
        sqlite3_free(query_two);
 
        char *query_three = sqlite3_mprintf("UPDATE sqlite_sequence SET seq = 0 where name = %Q;", menu_table[FENCE_MAIN_TABLE]);
@@ -2527,6 +2560,8 @@ int geofence_manager_get_count_by_params(const char *app_id, geofence_type_e fen
 
        if (*count <= 0) {
                LOGI_GEOFENCE("ERROR: count = %d", *count);
+               sqlite3_finalize(state);
+               sqlite3_free(query);
                return FENCE_ERR_COUNT;
        } else {
                LOGI_GEOFENCE("count[%d]", *count);
@@ -2623,6 +2658,8 @@ int geofence_manager_get_count_of_fences(int *count)
 
        if (*count < 0) {
                LOGI_GEOFENCE("ERROR: count = %d", *count);
+               sqlite3_finalize(state);
+               sqlite3_free(query);
                return FENCE_ERR_COUNT;
        } else {
                LOGI_GEOFENCE("count[%d]", *count);
@@ -2661,6 +2698,8 @@ int geofence_manager_get_place_count_by_placeid(int place_id, int *count)
 
        if (*count < 0) {
                LOGI_GEOFENCE("ERROR: place count = %d", *count);
+               sqlite3_finalize(state);
+               sqlite3_free(query);
                return FENCE_ERR_COUNT;
        } else {
                LOGI_GEOFENCE("place count[%d]", *count);