Commit 3e9f7763 authored by Martin Marinov's avatar Martin Marinov

Added a simple wrapper for accessing system environment variables in Base, the...

Added a simple wrapper for accessing system environment variables in Base, the getenv() function (inc in std::) are tagged as insecure. 

[git-p4: depot-paths = "//ReForm/ReForm/main/Base/": change = 13761]
parent 529bab33
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "DebUtils.hh" #include "DebUtils.hh"
#include "Base/Utils/ThrowError.hh" #include "Base/Utils/ThrowError.hh"
#include "Base/Utils/Environment.hh"
#ifdef DEB_ON #ifdef DEB_ON
...@@ -758,21 +759,9 @@ public: ...@@ -758,21 +759,9 @@ public:
void read_debug_config() void read_debug_config()
{ {
std::string flnm("reform_deb.cfg"); const auto flnm =
#ifdef WIN32 System::Environment::variable("REFORM_DEB_CONFIG", "reform_deb.cfg");
size_t size;
getenv_s(&size, nullptr, 0, "REFORM_DEB_CONFIG");
if (size > 0)
{
std::unique_ptr<char[]> bufer(new char[size]);
getenv_s(&size, bufer.get(), size, "REFORM_DEB_CONFIG");
flnm = bufer.get();
}
#else
const char* deb_flnm = getenv("REFORM_DEB_CONFIG");
if (deb_flnm != nullptr)
flnm = deb_flnm;
#endif
std::ifstream deb_stream(flnm.c_str()); std::ifstream deb_stream(flnm.c_str());
std::string line; std::string line;
while(std::getline(deb_stream, line)) while(std::getline(deb_stream, line))
......
set(my_headers set(my_headers
${CMAKE_CURRENT_SOURCE_DIR}/BaseError.hh ${CMAKE_CURRENT_SOURCE_DIR}/BaseError.hh
${CMAKE_CURRENT_SOURCE_DIR}/BaseErrorInc.hh ${CMAKE_CURRENT_SOURCE_DIR}/BaseErrorInc.hh
${CMAKE_CURRENT_SOURCE_DIR}/Environment.hh
${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.hh ${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.hh
${CMAKE_CURRENT_SOURCE_DIR}/ThrowError.hh ${CMAKE_CURRENT_SOURCE_DIR}/ThrowError.hh
PARENT_SCOPE PARENT_SCOPE
...@@ -8,6 +9,7 @@ set(my_headers ...@@ -8,6 +9,7 @@ set(my_headers
set(my_sources set(my_sources
${CMAKE_CURRENT_SOURCE_DIR}/BaseError.cc ${CMAKE_CURRENT_SOURCE_DIR}/BaseError.cc
${CMAKE_CURRENT_SOURCE_DIR}/Environment.cc
${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.cc ${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.cc
PARENT_SCOPE PARENT_SCOPE
) )
// (C) Copyright 2015 by Autodesk, Inc.
//
// The information contained herein is confidential, proprietary
// to Autodesk, Inc., and considered a trade secret as defined
// in section 499C of the penal code of the State of California.
// Use of this information by anyone other than authorized
// employees of Autodesk, Inc. is granted only under a written
// non-disclosure agreement, expressly prescribing the scope
// and manner of such use.
#include "Base/Security/Mandatory.hh"
#include "Environment.hh"
namespace System {
namespace Environment {
bool variable(const char* _vrbl_name, std::string& _vrbl)
{
#ifdef WIN32 // The Windows implementation uses the secure getenv_s()
size_t char_nmbr;
getenv_s(&char_nmbr, nullptr, 0, _vrbl_name);
if (char_nmbr == 0)
return false;
_vrbl.resize(char_nmbr);
getenv_s(&char_nmbr, &_vrbl[0], char_nmbr, _vrbl_name);
_vrbl.resize(char_nmbr - 1); // remove the trailing \0 char
#else
const char* vrbl_env = getenv("REFORM_DEB_CONFIG");
if (vrbl_env == nullptr)
return false;
_vrbl = vrbl_env;
#endif
return true;
}
}//namespace Environment
}//namespace System
// (C) Copyright 2015 by Autodesk, Inc.
//
// The information contained herein is confidential, proprietary
// to Autodesk, Inc., and considered a trade secret as defined
// in section 499C of the penal code of the State of California.
// Use of this information by anyone other than authorized
// employees of Autodesk, Inc. is granted only under a written
// non-disclosure agreement, expressly prescribing the scope
// and manner of such use.
#ifndef BASE_ENVIRONMENT_HH_INCLUDED
#define BASE_ENVIRONMENT_HH_INCLUDED
#include <string>
namespace System {
namespace Environment {
/*!
Get the value of an environment variable. This wraps around getenv() in
a portable, secure (and in future) thread-safe way. If the variable is
undefined, return an empty string.
Returns false if the variable is undefined, the input string is unchanged.
*/
bool variable(const char* _vrbl_name, std::string& _str);
//! Same as above, but returns an empty string if the variable is undefined.
inline std::string variable(const char* _vrbl_name)
{
std::string vrbl;
variable(_vrbl_name, vrbl);
return vrbl;
}
//! Same as above, but returns a default string if the variable is undefined.
inline std::string variable(const char* _vrbl_name, const char* _vrbl_dflt)
{
std::string vrbl(_vrbl_dflt);
variable(_vrbl_name, vrbl);
return vrbl;
}
}//namespace Environment
}//namespace System
#endif//REFORM_ENVIRONMENT_HH_INCLUDED
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment