Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/path_util.c
| Show All 26 Lines | |||||
| #include <string.h> | #include <string.h> | ||||
| #include "DNA_listBase.h" | #include "DNA_listBase.h" | ||||
| #include "BLI_fileops.h" | #include "BLI_fileops.h" | ||||
| #include "BLI_fnmatch.h" | #include "BLI_fnmatch.h" | ||||
| #include "BLI_path_util.h" | #include "BLI_path_util.h" | ||||
| #include "BLI_string.h" | #include "BLI_string.h" | ||||
| #include "BLI_string_utf8.h" | |||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #ifdef WIN32 | #ifdef WIN32 | ||||
| # include "utf_winfunc.h" | # include "utf_winfunc.h" | ||||
| # include "utfconv.h" | # include "utfconv.h" | ||||
| # include <io.h> | # include <io.h> | ||||
| # ifdef _WIN32_IE | # ifdef _WIN32_IE | ||||
| # undef _WIN32_IE | # undef _WIN32_IE | ||||
| ▲ Show 20 Lines • Show All 1,259 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| /** | /** | ||||
| * Get an env var, result has to be used immediately. | * Get an env var, result has to be used immediately. | ||||
| * | * | ||||
| * On windows getenv gets its variables from a static copy of the environment variables taken at | * On windows getenv gets its variables from a static copy of the environment variables taken at | ||||
| * process start-up, causing it to not pick up on environment variables created during runtime. | * process start-up, causing it to not pick up on environment variables created during runtime. | ||||
| * This function uses an alternative method to get environment variables that does pick up on | * This function uses an alternative method to get environment variables that does pick up on | ||||
| * runtime environment variables. | * runtime environment variables. The result will be UTF-8 encoded. | ||||
| */ | */ | ||||
| const char *BLI_getenv(const char *env) | const char *BLI_getenv(const char *env) | ||||
| { | { | ||||
| #ifdef _MSC_VER | #ifdef _MSC_VER | ||||
| static char buffer[32767]; /* 32767 is the total size of the environment block on windows*/ | const char *result = NULL; | ||||
| if (GetEnvironmentVariableA(env, buffer, sizeof(buffer))) { | static wchar_t buffer[32768]; /* 32767 is the maximum size of the environment variable on | ||||
| return buffer; | windows, reserve one more character for the zero terminator. */ | ||||
| wchar_t *env_16 = alloc_utf16_from_8(env, 0); | |||||
| if (env_16) { | |||||
| 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); | |||||
| free(res_utf8); | |||||
LazyDodo: Seems like we both missed the memleak here, if the buffer was valid, but didn't fit in the… | |||||
| result = (const char *)buffer; | |||||
| } | |||||
| } | } | ||||
| else { | |||||
| return NULL; | |||||
| } | } | ||||
| return result; | |||||
| #else | #else | ||||
| return getenv(env); | return getenv(env); | ||||
| #endif | #endif | ||||
| } | } | ||||
| /** | /** | ||||
| * Ensures that the parent directory of *name exists. | * Ensures that the parent directory of *name exists. | ||||
| * | * | ||||
| ▲ Show 20 Lines • Show All 677 Lines • Show Last 20 Lines | |||||
Seems like we both missed the memleak here, if the buffer was valid, but didn't fit in the static buffer, unlikely to happen, but still sloppy. fixed in rB0dbbcaf1e6bb