aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c59
1 files changed, 27 insertions, 32 deletions
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