aboutsummaryrefslogtreecommitdiffstats
path: root/core/macos.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2017-11-18 10:55:01 +0100
committerGravatar Lubomir I. Ivanov <neolit123@gmail.com>2017-11-22 19:22:29 +0100
commit04f38d61d7b707c3ee29e00002380d596658395e (patch)
treecc6c2ecc100a86241cffc8b4b619c10a2aec73e1 /core/macos.c
parent7a99d7e5c3b297b543a9ce1ab36e337c1a8e0bf0 (diff)
downloadsubsurface-04f38d61d7b707c3ee29e00002380d596658395e.tar.gz
Clean up system_default_filename()
In the old implementation there were two static C-style strings, filename and path, which were initialized to NULL and filled on first call of the function (i.e. singletons). There is no sense in having two static variables indicating whether this function was called previously. Moreover, there is no point in remembering filename accross function calls, because it is not used once path is set to a non-NULL value. Therefore, make the filename variable non-static and calculate it only on first invocation (as indicated by a NULL path). Moreover, free() the filename variable after its use to fix a memory leak of the old code. The windows code is slightly different in that the temporary filename is not dynamically allocated. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/macos.c')
-rw-r--r--core/macos.c11
1 files changed, 5 insertions, 6 deletions
diff --git a/core/macos.c b/core/macos.c
index b16225c78..37bbd5d25 100644
--- a/core/macos.c
+++ b/core/macos.c
@@ -81,18 +81,17 @@ const char *system_default_directory(void)
const char *system_default_filename(void)
{
- static char *filename = NULL;
- if (!filename) {
+ static const char *path = NULL;
+ if (!path) {
const char *user = getenv("LOGNAME");
if (same_string(user, ""))
user = "username";
- filename = calloc(strlen(user) + 5, 1);
+ char *filename = calloc(strlen(user) + 5, 1);
strcat(filename, user);
strcat(filename, ".xml");
- }
- static const char *path = NULL;
- if (!path)
path = system_default_path_append(filename);
+ free(filename);
+ }
return path;
}