From 0dbbcaf1e6bb8e0296a3754df380badfd372908c Mon Sep 17 00:00:00 2001 From: Ray Molenkamp Date: Fri, 11 Dec 2020 12:09:18 -0700 Subject: Fix: Fix potential memory leak in BLI_getenv Issue introduced in rB87b19b3aba0c and unlikely to occur but no reason not to have correct code. --- source/blender/blenlib/intern/path_util.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 461f8a53beb..927318a923e 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1322,12 +1322,14 @@ const char *BLI_getenv(const char *env) if (GetEnvironmentVariableW(env_16, buffer, ARRAY_SIZE(buffer))) { char *res_utf8 = alloc_utf_8_from_16(buffer, 0); // make sure the result is valid, and will fit into our temporary storage buffer - if (res_utf8 && (strlen(res_utf8) + 1) < sizeof(buffer)) { - // We are re-using the utf16 buffer here, since allocating a second static buffer to - // contain the UTF-8 version to return would be wasteful. - memcpy(buffer, res_utf8, strlen(res_utf8) + 1); + if (res_utf8) { + if (strlen(res_utf8) + 1 < sizeof(buffer)) { + // We are re-using the utf16 buffer here, since allocating a second static buffer to + // contain the UTF-8 version to return would be wasteful. + memcpy(buffer, res_utf8, strlen(res_utf8) + 1); + result = (const char *)buffer; + } free(res_utf8); - result = (const char *)buffer; } } } @@ -1529,7 +1531,8 @@ bool BLI_path_extension_check_glob(const char *str, const char *ext_fnmatch) } /** - * Does basic validation of the given glob string, to prevent common issues from string truncation. + * Does basic validation of the given glob string, to prevent common issues from string + * truncation. * * For now, only forbids last group to be a wildcard-only one, if there are more than one group * (i.e. things like "*.txt;*.cpp;*" are changed to "*.txt;*.cpp;") -- cgit v1.2.3