Skip to content

Commit

Permalink
Fix crash when using locale missing on system
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanna K committed Jul 6, 2016
1 parent 001c6c3 commit eade7b7
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions libqalculate/Calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ Calculator::Calculator() {
XOR_str = "XOR";
XOR_str_len = OR_str.length();

saved_locale = strdup(setlocale(LC_NUMERIC, ""));
char *current_lc_numeric = setlocale(LC_NUMERIC, "");
if(current_lc_numeric) saved_locale = strdup(current_lc_numeric);
else saved_locale = NULL;
struct lconv *lc = localeconv();
if(!lc) {
setlocale(LC_NUMERIC, "C");
lc = localeconv();
}
#ifdef HAVE_STRUCT_LCONV_INT_N_CS_PRECEDES
place_currency_code_before = lc->int_p_cs_precedes;
#else
Expand All @@ -321,7 +327,7 @@ Calculator::Calculator() {
DOT_S = ".";
COMMA_STR = ",";
COMMA_S = ",;";
}
}
setlocale(LC_NUMERIC, "C");

NAME_NUMBER_PRE_S = "_#";
Expand Down Expand Up @@ -1043,7 +1049,7 @@ string Calculator::localToString() const {
return _(" to ");
}
void Calculator::setLocale() {
setlocale(LC_NUMERIC, saved_locale);
if(saved_locale) setlocale(LC_NUMERIC, saved_locale);
lconv *locale = localeconv();
if(strcmp(locale->decimal_point, ",") == 0) {
DOT_STR = ",";
Expand Down

0 comments on commit eade7b7

Please sign in to comment.