diff options
| author | Alexey Samsonov <samsonov@google.com> | 2013-03-14 11:10:23 +0000 |
|---|---|---|
| committer | Alexey Samsonov <samsonov@google.com> | 2013-03-14 11:10:23 +0000 |
| commit | 83e7622df61865859a854fe410c38c2e6a6741e1 (patch) | |
| tree | fc586c615bae5f3a37e7b35e3efb5ac9b3e2f564 | |
| parent | ae9076457e59044487a16dd9c2e6ece0d156905b (diff) | |
| download | bcm5719-llvm-83e7622df61865859a854fe410c38c2e6a6741e1.tar.gz bcm5719-llvm-83e7622df61865859a854fe410c38c2e6a6741e1.zip | |
[Sanitizer] Write a slightly better implementation of GetEnv() function on Windows
llvm-svn: 177051
| -rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_win.cc | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc index d62721942a9..c6573419b3e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc @@ -111,19 +111,38 @@ void *MapFileToMemory(const char *file_name, uptr *buff_size) { UNIMPLEMENTED(); } -const char *GetEnv(const char *name) { - static char env_buffer[32767] = {}; +static const kMaxEnvNameLength = 128; +static const kMaxEnvValueLength = 32767; - // Note: this implementation stores the result in a static buffer so we only - // allow it to be called just once. - static bool called_once = false; - if (called_once) - UNIMPLEMENTED(); - called_once = true; +namespace { + +struct EnvVariable { + char name[kMaxEnvNameLength]; + char value[kMaxEnvValueLength]; +}; + +} // namespace - DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer)); - if (rv > 0 && rv < sizeof(env_buffer)) - return env_buffer; +static const int kEnvVariables = 5; +static EnvVariable env_vars[kEnvVariables]; +static int num_env_vars; + +const char *GetEnv(const char *name) { + // Note: this implementation caches the values of the environment variables + // and limits their quantity. + for (int i = 0; i < num_env_vars; i++) { + if (0 == internal_strcmp(name, env_vars[i].name)) + return env_vars[i].value; + } + CHECK_LT(num_env_vars, kEnvVariables); + DWORD rv = GetEnvironmentVariableA(name, env_vars[num_env_vars].value, + kMaxEnvValueLength); + if (rv > 0 && rv < kMaxEnvValueLength) { + CHECK_LT(internal_strlen(name), kMaxEnvNameLength); + internal_strncpy(env_vars[num_env_vars].name, name, kMaxEnvNameLength); + num_env_vars++; + return env_vars[num_env_vars - 1].value; + } return 0; } |

