summaryrefslogtreecommitdiff
path: root/misc.h
diff options
context:
space:
mode:
Diffstat (limited to 'misc.h')
-rw-r--r--misc.h9
1 files changed, 8 insertions, 1 deletions
diff --git a/misc.h b/misc.h
index e8b8013..ac1cbda 100644
--- a/misc.h
+++ b/misc.h
@@ -548,13 +548,20 @@ inline void SecureWipeArray(T *buf, size_t n)
}
// this function uses wcstombs(), which assumes that setlocale() has been called
-static std::string StringNarrow(const wchar_t *str)
+static std::string StringNarrow(const wchar_t *str, bool throwOnError = true)
{
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe.
#endif
size_t size = wcstombs(NULL, str, 0);
+ if (size == -1)
+ {
+ if (throwOnError)
+ throw InvalidArgument("StringNarrow: wcstombs() call failed");
+ else
+ return std::string();
+ }
std::string result(size, 0);
wcstombs(&result[0], str, size);
return result;