aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/qutil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2020-10-21 21:29:28 +0200
committerJay Berkenbilt <ejb@ql.org>2020-10-21 22:42:51 +0200
commit98f6c00dad96d3150a9b969a0ee67addc78ac5f0 (patch)
tree2c1455f3b208275aecc1395453f3396a85eb7800 /libtests/qutil.cc
parentef127001b36b42042874812e0d06dccf92cdb229 (diff)
downloadqpdf-98f6c00dad96d3150a9b969a0ee67addc78ac5f0.tar.zst
Protect numeric conversion against user's locale (fixes #459)
Diffstat (limited to 'libtests/qutil.cc')
-rw-r--r--libtests/qutil.cc31
1 files changed, 31 insertions, 0 deletions
diff --git a/libtests/qutil.cc b/libtests/qutil.cc
index 935cdfc2..abfefce0 100644
--- a/libtests/qutil.cc
+++ b/libtests/qutil.cc
@@ -10,6 +10,7 @@
#include <limits.h>
#include <assert.h>
#include <fstream>
+#include <locale>
#ifdef _WIN32
# include <io.h>
@@ -80,8 +81,38 @@ void test_to_ull(char const* str, unsigned long long wanted, bool error)
test_to_number(str, wanted, error, QUtil::string_to_ull);
}
+static void set_locale()
+{
+ try
+ {
+ // First try a locale known to put commas in numbers.
+ std::locale::global(std::locale("en_US.UTF-8"));
+ }
+ catch (std::runtime_error&)
+ {
+ try
+ {
+ // If that fails, fall back to the user's default locale.
+ std::locale::global(std::locale(""));
+ }
+ catch (std::runtime_error& e)
+ {
+ // Ignore this error on Windows without MSVC. We get
+ // enough test coverage on other platforms, and mingw
+ // seems to have limited locale support (as of
+ // 2020-10).
+#if ! defined(_WIN32) || defined(_MSC_VER)
+ throw e;
+#endif
+ }
+ }
+}
+
void string_conversion_test()
{
+ // Make sure the code produces consistent results even if we load
+ // a non-C locale.
+ set_locale();
std::cout << QUtil::int_to_string(16059) << std::endl
<< QUtil::int_to_string(16059, 7) << std::endl
<< QUtil::int_to_string(16059, -7) << std::endl