aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2019-12-06 11:02:59 -0500
committerJack Lloyd <[email protected]>2019-12-06 11:11:55 -0500
commitecb092d18aa6c87814bb63748d104910f4c27d6c (patch)
tree7eba92e6d0f3bd6fd834bab3a52e0d7f67ecfa7c /src/lib/utils
parent74a8685830775c1463e3eb484e2faf824e6862cd (diff)
Avoid MSVC warnings about dead code in FFI layer
Also fix warning about "insecure" getenv
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/os_utils.cpp42
-rw-r--r--src/lib/utils/os_utils.h10
2 files changed, 29 insertions, 23 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index 72de063e8..b37c06d6e 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -384,21 +384,40 @@ size_t OS::get_memory_locking_limit()
return 0;
}
-const char* OS::read_env_variable(const std::string& name)
+bool OS::read_env_variable(std::string& value_out, const std::string& name)
{
+ value_out = "";
+
if(running_in_privileged_state())
- return nullptr;
+ return false;
- return std::getenv(name.c_str());
+#if defined(BOTAN_TARGET_OS_HAS_WIN32) && defined(BOTAN_BUILD_COMPILER_IS_MSVC)
+ char val[128] = { 0 };
+ size_t req_size = 0;
+ if(getenv_s(&req_size, val, sizeof(val), name.c_str()) == 0)
+ {
+ value_out = std::string(val, req_size);
+ return true;
+ }
+#else
+ if(const char* val = std::getenv(name.c_str()))
+ {
+ value_out = val;
+ return true;
+ }
+#endif
+
+ return false;
}
size_t OS::read_env_variable_sz(const std::string& name, size_t def)
{
- if(const char* env = read_env_variable(name))
+ std::string value;
+ if(read_env_variable(value, name))
{
try
{
- const size_t val = std::stoul(env, nullptr);
+ const size_t val = std::stoul(value, nullptr);
return val;
}
catch(std::exception&) { /* ignore it */ }
@@ -614,19 +633,6 @@ int OS::run_cpu_instruction_probe(std::function<int ()> probe_fn)
if(rc != 0)
throw System_Error("run_cpu_instruction_probe sigaction restore failed", errno);
-#elif defined(BOTAN_TARGET_OS_IS_WINDOWS) && defined(BOTAN_TARGET_COMPILER_IS_MSVC)
-
- // Windows SEH
- __try
- {
- probe_result = probe_fn();
- }
- __except(::GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION ?
- EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
- {
- probe_result = -1;
- }
-
#else
BOTAN_UNUSED(probe_fn);
#endif
diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h
index 5b8c49f99..d31dcb3ba 100644
--- a/src/lib/utils/os_utils.h
+++ b/src/lib/utils/os_utils.h
@@ -95,12 +95,12 @@ size_t get_memory_locking_limit();
size_t system_page_size();
/**
-* Read the value of an environment variable. Return nullptr if
-* no such variable is set. If the process seems to be running in
-* a privileged state (such as setuid) then always returns nullptr,
-* similiar to glibc's secure_getenv.
+* Read the value of an environment variable, setting it to value_out if it
+* exists. Returns false and sets value_out to empty string if no such variable
+* is set. If the process seems to be running in a privileged state (such as
+* setuid) then always returns false and does not examine the environment.
*/
-const char* read_env_variable(const std::string& var_name);
+bool read_env_variable(std::string& value_out, const std::string& var_name);
/**
* Read the value of an environment variable and convert it to an