diff options
author | Andreas Schuh <andreas.schuh.84@gmail.com> | 2014-03-17 21:19:35 +0000 |
---|---|---|
committer | Andreas Schuh <andreas.schuh.84@gmail.com> | 2014-03-17 21:19:35 +0000 |
commit | 8d3797cd15371328c8e0fc5f71060fb9ad4e40c7 (patch) | |
tree | b4cb2554442ada0ff31ce27a199daaf76586efa1 /src/util.h | |
parent | 8d93bca20a0700c81fadda5849cb73d7be8d91ba (diff) | |
download | gflags-8d3797cd15371328c8e0fc5f71060fb9ad4e40c7.tar.gz gflags-8d3797cd15371328c8e0fc5f71060fb9ad4e40c7.tar.bz2 gflags-8d3797cd15371328c8e0fc5f71060fb9ad4e40c7.zip |
Fix VS security warnings using SafeGetEnv and SafeFOpen utility functions.
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -324,6 +324,33 @@ inline std::string StringPrintf(const char* format, ...) { return output; } +inline bool SafeGetEnv(const char *varname, std::string &valstr) +{ +#if defined(_MSC_VER) && _MSC_VER >= 1400 + char *val; + size_t sz; + if (_dupenv_s(&val, &sz, varname) != 0 || !val) return false; + valstr = val; + free(val); +#else + const char * const val = getenv(varname); + if (!val) return false; + valstr = val; +#endif + return true; +} + +inline errno_t SafeFOpen(FILE **fp, const char* fname, const char *mode) +{ +#if defined(_MSC_VER) && _MSC_VER >= 1400 + return fopen_s(fp, fname, mode); +#else + assert(fp != NULL); + *fp = fopen(fname, mode); + return errno; +#endif +} + } // namespace GFLAGS_NAMESPACE |