diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2017-11-18 10:55:01 +0100 |
---|---|---|
committer | Lubomir I. Ivanov <neolit123@gmail.com> | 2017-11-22 19:22:29 +0100 |
commit | 04f38d61d7b707c3ee29e00002380d596658395e (patch) | |
tree | cc6c2ecc100a86241cffc8b4b619c10a2aec73e1 /core/windows.c | |
parent | 7a99d7e5c3b297b543a9ce1ab36e337c1a8e0bf0 (diff) | |
download | subsurface-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/windows.c')
-rw-r--r-- | core/windows.c | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/core/windows.c b/core/windows.c index 029c3516e..f0b9eb22c 100644 --- a/core/windows.c +++ b/core/windows.c @@ -103,17 +103,16 @@ const char *system_default_directory(void) */ const char *system_default_filename(void) { - static wchar_t filename[UNLEN + 5] = { 0 }; - if (!*filename) { + static const char *path = NULL; + if (!path) { wchar_t username[UNLEN + 1] = { 0 }; DWORD username_len = UNLEN + 1; GetUserNameW(username, &username_len); + wchar_t filename[UNLEN + 5] = { 0 }; wcscat(filename, username); wcscat(filename, L".xml"); - } - static const char *path = NULL; - if (!path) path = system_default_path_append(filename); + } return path; } |