summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorAndreas Schuh <andreas.schuh.84@gmail.com>2014-03-17 21:19:35 +0000
committerAndreas Schuh <andreas.schuh.84@gmail.com>2014-03-17 21:19:35 +0000
commit8d3797cd15371328c8e0fc5f71060fb9ad4e40c7 (patch)
treeb4cb2554442ada0ff31ce27a199daaf76586efa1 /src/util.h
parent8d93bca20a0700c81fadda5849cb73d7be8d91ba (diff)
downloadgflags-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.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h
index 9d6f8dc..3856365 100644
--- a/src/util.h
+++ b/src/util.h
@@ -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