From 9fe3814a2ef0be8e5b693fb0fa42064b33d0ae45 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Wed, 18 Feb 2026 10:00:46 +0100 Subject: Fix memory leak, add volatile, reduce duplication - config.c: free partial strdup on configAppend failure - common.c: consolidate loadTrustStore error paths with goto - server_lds.c, server_register.c: make running volatile, remove non-async-signal-safe call from signal handler - server_register.c: extract LdsClientParams + makeLdsClientConfig to deduplicate the register/deregister client config setup --- src/common.c | 59 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'src/common.c') diff --git a/src/common.c b/src/common.c index 8d7d651..39a2a68 100644 --- a/src/common.c +++ b/src/common.c @@ -74,6 +74,7 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) return -1; } + int rc = -1; size_t capacity = 8; size_t count = 0; char **paths = malloc (capacity * sizeof (char *)); @@ -81,8 +82,7 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "loadTrustStore: out of memory"); - closedir (dir); - return -1; + goto cleanup; } struct dirent *entry; @@ -94,22 +94,6 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) if (nameLen < 5 || strcmp (name + nameLen - 4, ".der") != 0) continue; - /* Build full path: dirPath/name */ - size_t dirLen = strlen (dirPath); - size_t fullLen = dirLen + 1 + nameLen + 1; - char *full = malloc (fullLen); - if (!full) - { - UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "loadTrustStore: out of memory"); - for (size_t i = 0; i < count; i++) - free (paths[i]); - free (paths); - closedir (dir); - return -1; - } - snprintf (full, fullLen, "%s/%s", dirPath, name); - if (count == capacity) { capacity *= 2; @@ -118,30 +102,41 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "loadTrustStore: out of memory"); - free (full); - for (size_t i = 0; i < count; i++) - free (paths[i]); - free (paths); - closedir (dir); - return -1; + goto cleanup; } paths = tmp; } + /* Build full path: dirPath/name */ + size_t dirLen = strlen (dirPath); + size_t fullLen = dirLen + 1 + nameLen + 1; + char *full = malloc (fullLen); + if (!full) + { + UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, + "loadTrustStore: out of memory"); + goto cleanup; + } + snprintf (full, fullLen, "%s/%s", dirPath, name); + paths[count++] = full; } - closedir (dir); - - if (count == 0) + rc = 0; + if (count > 0) { - free (paths); - return 0; + *outPaths = paths; + *outSize = count; + paths = NULL; + count = 0; } - *outPaths = paths; - *outSize = count; - return 0; +cleanup: + for (size_t i = 0; i < count; i++) + free (paths[i]); + free (paths); + closedir (dir); + return rc; } void -- cgit v1.2.3