bugservice: Fix memory leak 67/319767/1
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Thu, 31 Oct 2024 08:26:49 +0000 (09:26 +0100)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Thu, 31 Oct 2024 15:24:35 +0000 (16:24 +0100)
Change-Id: Id7d93792c4f21e56de488fe59bb08fd0c75f13f7

packaging/crash-worker.spec
src/bugreport-service/bugreport-service.c
src/bugreport-service/diagnostics/diagnostics_dump.c
src/bugreport-service/systemdump.c
src/shared/config.c
src/shared/config.h

index 35bf25e298a9c092dcf405961a3004b526d205b2..f30372d0925aed6815bcae5148447ad77348aad5 100644 (file)
@@ -16,7 +16,7 @@
 
 Name:           crash-worker
 Summary:        Coredump handler and report generator for Tizen
-Version:        8.0.5
+Version:        8.0.6
 Release:        1
 Group:          Framework/system
 License:        MIT
index 6000f060eaa9d5f669b6cdc24f0b0966f7d61fc9..dc26a06f1ff0ecc7fb59291d3a2332ca18fd4eb2 100644 (file)
@@ -34,6 +34,7 @@
 #include "bugreport-service.h"
 
 #include "crash-manager/crash-manager.h"
+#include "shared/config.h"
 #include "shared/bugreport-util.h"
 #include "shared/log.h"
 #include "shared/util.h"
@@ -53,6 +54,9 @@
 #define TIMEOUT_INTERVAL_SEC 60
 #define TIMEOUT_LIVEDUMP_SEC 50
 
+static config_t config;
+const config_t *g_config = NULL;
+
 static GMainLoop *loop;
 static GMutex timeout_mutex;
 static guint timeout_id;
@@ -514,14 +518,20 @@ static bool dbus_init(void)
 
 int main(void)
 {
+       int res = EXIT_SUCCESS;
        /* Have consinsent umask across invocations - from shell, bugreport-service, kernel */
        umask(DEFAULT_UMASK);
 
+       if (config_init(&config, CRASH_MANAGER_CONFIG_PATH)) {
+               g_config = &config;
+       }
+
        loop = g_main_loop_new(NULL, false);
 
        if (!dbus_init()) {
                g_main_loop_unref(loop);
-               return EXIT_FAILURE;
+               res = EXIT_FAILURE;
+               goto exit;
        }
 
        g_mutex_init(&timeout_mutex);
@@ -537,5 +547,12 @@ int main(void)
        g_mutex_clear(&timeout_mutex);
        g_main_loop_unref(loop);
 
-       return EXIT_SUCCESS;
+
+exit:
+       if (g_config) {
+               config_free(&config);
+               g_config = NULL;
+       }
+
+       return res;
 }
index 8daed4e92695cb37b6c6e1e8347461ae24ce8df3..cc79e42fbe3403bb48b2d622644a389dbe2b3aed 100644 (file)
@@ -34,6 +34,8 @@
 #include "shared/log.h"
 #include "shared/util.h"
 
+extern const config_t *g_config;
+
 enum BUGREPORT_REPORT_TYPE { BR_UNKNOWN, BR_BUGREPORT, BR_CRASHINFO, BR_CRASHINFO_JSON, BR_COREDUMP_TAR, BR_FULLREPORT, BR_PID };
 
 struct report_file {
@@ -140,12 +142,7 @@ static struct report_file* get_reports(size_t *count)
        struct report_file *list = NULL;
        *count = 0;
 
-       config_t config;
-       char *crash_root_path = NULL;
-       if (config_init(&config, CRASH_MANAGER_CONFIG_PATH))
-               crash_root_path = config.crash_root_path;
-       else
-               crash_root_path = CRASH_ROOT_PATH;
+       char *crash_root_path = config_get_crash_root_path(g_config);
 
        char crash_path[PATH_MAX];
        char live_path[PATH_MAX];
index 0aca439a124572cc364ac2b372b3e877937b7f27..583bf09758944876dab884b402435aaf08805002 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <libbugreport.h>
+#include <limits.h>
 
 #include "bugreport-service.h"
 
@@ -34,6 +35,8 @@
 
 #define DUMP_SYSTEMSTATE_TIMEOUT_MS 1000*60*2
 
+extern const config_t *g_config;
+
 struct systemstate_info {
        char *report_name;
        char *log_path;
@@ -74,17 +77,10 @@ static bool prepare_paths(struct systemstate_info *sinfo)
        time_t time_info = time(NULL);
        localtime_r(&time_info, &loc_tm);
        strftime(date, sizeof(date), "%Y%m%d%H%M%S", &loc_tm);
-       config_t config;
-       const char *crash_root_path;
-       if (config_init(&config, CRASH_MANAGER_CONFIG_PATH))
-               crash_root_path = config.crash_root_path;
-       else
-               crash_root_path = CRASH_ROOT_PATH;
-
-       char *crash_temp_path;
-       if (asprintf(&crash_temp_path, "%s/temp", crash_root_path) == -1) {
-               goto out_of_memory;
-       }
+       const char *crash_root_path = config_get_crash_root_path(g_config);
+
+       char crash_temp_path[PATH_MAX];
+       snprintf(crash_temp_path, sizeof(crash_temp_path)-1, "%s/temp", crash_root_path);
 
        if (!make_dir(crash_temp_path, DEFAULT_CRASH_DIR_PERM))
                return false;
index b5c7b3dc8eca0035780c71013275b7e64f3a244f..aece9c4ccc55df8ff2d9dd4bc1465db68386be3a 100644 (file)
@@ -302,3 +302,11 @@ void config_free(config_t *c)
 
        memset(c, 0, sizeof(*c));
 }
+
+char *config_get_crash_root_path(const config_t *config)
+{
+       if (config) {
+               return config->crash_root_path;
+       }
+       return CRASH_ROOT_PATH;
+}
index a9ead12c92329eacd50fe4c3d41571559eec3e63..eaffbb773c9266f8f778db97608b52f6183f8c25 100644 (file)
@@ -56,6 +56,7 @@ bool config_init(config_t *c, const char *const path);
 void config_free(config_t *c);
 
 bool config_is_path_excluded(config_t *c, const char *const path);
+char *config_get_crash_root_path(const config_t *config);
 
 #ifdef __cplusplus
 }