device: Fix not clearing error on update_bredr_services
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 27 Oct 2021 19:53:34 +0000 (12:53 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 11 Mar 2022 13:38:37 +0000 (19:08 +0530)
When trying to loads contents from file using g_key_file_load_from_file
it may cause an error which needs to be cleared since the gerr is
reused, also make sure the file is always created upfront to avoid
having such error when storage has not been initialized.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/device.c

index 7b31e16..e99824a 100644 (file)
@@ -7901,22 +7901,28 @@ static void update_bredr_services(struct browse_req *req, sdp_list_t *recs)
 
        snprintf(sdp_file, PATH_MAX, STORAGEDIR "/%s/cache/%s", srcaddr,
                                                                dstaddr);
+       create_file(sdp_file, 0600);
 
        sdp_key_file = g_key_file_new();
        if (!g_key_file_load_from_file(sdp_key_file, sdp_file, 0, &gerr)) {
                error("Unable to load key file from %s: (%s)", sdp_file,
                                                                gerr->message);
-               g_error_free(gerr);
+               g_clear_error(&gerr);
+               g_key_file_free(sdp_key_file);
+               sdp_key_file = NULL;
        }
 
        snprintf(att_file, PATH_MAX, STORAGEDIR "/%s/%s/attributes", srcaddr,
                                                                dstaddr);
+       create_file(att_file, 0600);
 
        att_key_file = g_key_file_new();
        if (!g_key_file_load_from_file(att_key_file, att_file, 0, &gerr)) {
                error("Unable to load key file from %s: (%s)", att_file,
                                                                gerr->message);
-               g_error_free(gerr);
+               g_clear_error(&gerr);
+               g_key_file_free(att_key_file);
+               att_key_file = NULL;
        }
 
        for (seq = recs; seq; seq = seq->next) {
@@ -7971,12 +7977,11 @@ next:
        if (sdp_key_file) {
                data = g_key_file_to_data(sdp_key_file, &length, NULL);
                if (length > 0) {
-                       create_file(sdp_file, 0600);
                        if (!g_file_set_contents(sdp_file, data, length,
                                                                &gerr)) {
                                error("Unable set contents for %s: (%s)",
                                                sdp_file, gerr->message);
-                               g_error_free(gerr);
+                               g_clear_error(&gerr);
                        }
                }
 
@@ -7987,12 +7992,11 @@ next:
        if (att_key_file) {
                data = g_key_file_to_data(att_key_file, &length, NULL);
                if (length > 0) {
-                       create_file(att_file, 0600);
                        if (!g_file_set_contents(att_file, data, length,
                                                                &gerr)) {
                                error("Unable set contents for %s: (%s)",
                                                att_file, gerr->message);
-                               g_error_free(gerr);
+                               g_clear_error(&gerr);
                        }
                }