Fix oal_bt_deinit crash issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-otp / bt-otpserver.c
index 7693b12..f44f487 100644 (file)
@@ -33,6 +33,7 @@
 #include "bt-otpserver.h"
 #include "bluetooth-api.h"
 
+#include <cynara-client.h>
 
 #undef LOG_TAG
 #define LOG_TAG "BLUETOOTH_OTP"
@@ -511,6 +512,7 @@ void _bt_otp_start_write_on_fd()
        FILE *fp;
        char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
        int length;
+       char err_msg[256] = {0, };
 
        snprintf(file_path, BT_FILE_PATH_MAX_LEN, "%s%s",
                                        directory, selected_object->name);
@@ -518,7 +520,8 @@ void _bt_otp_start_write_on_fd()
 
        fp = fopen(file_path, "r");
        if (!fp) {
-               BT_DBG("fopen() failed : %s", strerror(errno));
+               cynara_strerror(errno, err_msg, sizeof(err_msg));
+               BT_ERR("fopen() failed : %s", err_msg);
                return;
        }
 
@@ -569,6 +572,7 @@ static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
        gsize len = 0;
        int written;
        int fd;
+       char err_msg[256] = {0, };
 
        BT_DBG("");
 
@@ -622,7 +626,8 @@ static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
                BT_DBG("file_path = [%s]", file_path);
                fp = fopen(file_path, "w");
                if (!fp) {
-                       BT_DBG("fopen() failed : %s", strerror(errno));
+                       cynara_strerror(errno, err_msg, sizeof(err_msg));
+                       BT_ERR("fopen() failed : %s", err_msg);
                        goto fail;
                }
                oacp_op->fp = fp;
@@ -810,6 +815,9 @@ fail:
                                _bt_otp_start_write_on_fd();
                        else if (oacp_op->opcode == OACP_WRITE)
                                _bt_otp_start_read_on_fd();
+               } else {
+                       /* Close fd if oacp_op is NULL */
+                       close(fd);
                }
                g_dbus_method_invocation_return_value(invocation, NULL);
        }
@@ -1072,7 +1080,7 @@ int _bt_otp_send_launch_request(char *absolute_path)
 {
        void *handle;
        char *error;
-       int ret;
+       int ret = BLUETOOTH_ERROR_NONE;
 
        /* check ARCH 64 or 32*/
        if (!access(FILEPATH_ARCH_64, 0)) {
@@ -1091,7 +1099,7 @@ int _bt_otp_send_launch_request(char *absolute_path)
        dlerror();      /* Clear any existing error */
 
        int (*fun)(char *) = (int (*)(char *))dlsym(handle,
-                                       "bt_app_control_send_launch_request");
+                       "bt_app_control_send_launch_request");
 
        if ((error = dlerror()) != NULL)  {
                BT_ERR("Can not load symbol : %s", dlerror());
@@ -1099,7 +1107,8 @@ int _bt_otp_send_launch_request(char *absolute_path)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       ret = fun(absolute_path);
+       if (fun)
+               ret = fun(absolute_path);
        dlclose(handle);
 
        return ret;
@@ -1196,6 +1205,10 @@ void _bt_otp_restore_old_object()
 
        index = 0;
        object = _bt_otp_client_find_object(otp_object_list, prev_obj_id, &index);
+       if (!object) {
+               BT_ERR("Object is NULL");
+               return;
+       }
        oacp_create = FALSE;
        update_obj_metadata_charc_value(object);
        selected_object = object;
@@ -1620,6 +1633,7 @@ int _bt_otp_obj_name_write_cb(char *value, int len)
        char new_abs_filepath[BT_FILE_PATH_MAX_LEN] = {0, };
        int ret = BLUETOOTH_ERROR_NONE;
        FILE *fp = NULL;
+       char err_msg[256] = {0, };
 
        object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
        if (!object)
@@ -1642,7 +1656,8 @@ int _bt_otp_obj_name_write_cb(char *value, int len)
 
                fp = fopen(new_abs_filepath, "a");
                if (!fp) {
-                       BT_DBG("fopen() failed : %s", strerror(errno));
+                       cynara_strerror(errno, err_msg, sizeof(err_msg));
+                       BT_ERR("fopen() failed : %s", err_msg);
                        ret = BLUETOOTH_ATT_ERROR_INTERNAL;
                        goto fail;
                }
@@ -1808,6 +1823,9 @@ int _bt_otp_read_cb(const char *obj_path, char **value, int *len, uint16_t offse
 
                *len = info->value_length - offset;
                *value = (char *)malloc(sizeof(char)*(*len));
+               if (*value == NULL)
+                       return BLUETOOTH_ATT_ERROR_INTERNAL;
+
                memcpy(*value, info->char_value, *len);
 
                return BLUETOOTH_ATT_ERROR_NONE;