Skip to content

Commit

Permalink
use threadsafe uselocal (_configthreadlocale for Windows) (lensfun#1987)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-ott committed Mar 1, 2023
1 parent 40cf31a commit bd4c56c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
38 changes: 28 additions & 10 deletions libs/lensfun/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,14 @@ lfError lfDatabase::Load (const char *errcontext, const char *data, size_t data_
};

/* Temporarily drop numeric format to "C" */
char *old_numeric = setlocale (LC_NUMERIC, NULL);
old_numeric = strdup(old_numeric);
setlocale(LC_NUMERIC,"C");
#if defined(_MSC_VER)
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
setlocale (LC_NUMERIC, "C");
#else
auto loc = uselocale((locale_t) 0); // get current local
auto nloc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
uselocale(nloc);
#endif

lfParserData pd;
memset (&pd, 0, sizeof (pd));
Expand All @@ -846,8 +851,12 @@ lfError lfDatabase::Load (const char *errcontext, const char *data, size_t data_
g_markup_parse_context_free (mpc);

/* Restore numeric format */
setlocale (LC_NUMERIC, old_numeric);
free(old_numeric);
#if defined(_MSC_VER)
_configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
#else
uselocale(loc);
freelocale(nloc);
#endif

return e;
}
Expand Down Expand Up @@ -885,9 +894,14 @@ char *lfDatabase::Save () const
lfError lfDatabase::Save (char*& xml, size_t& data_size) const
{
/* Temporarily drop numeric format to "C" */
char *old_numeric = setlocale (LC_NUMERIC, NULL);
old_numeric = strdup(old_numeric);
setlocale(LC_NUMERIC,"C");
#if defined(_MSC_VER)
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
setlocale (LC_NUMERIC, "C");
#else
auto loc = uselocale((locale_t) 0); // get current local
auto nloc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
uselocale(nloc);
#endif

GString *output = g_string_sized_new (1024);

Expand Down Expand Up @@ -1123,8 +1137,12 @@ lfError lfDatabase::Save (char*& xml, size_t& data_size) const
g_string_append (output, "</lensdatabase>\n");

/* Restore numeric format */
setlocale (LC_NUMERIC, old_numeric);
free(old_numeric);
#if defined(_MSC_VER)
_configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
#else
uselocale(loc);
freelocale(nloc);
#endif

data_size = output->len;
xml = g_string_free (output, FALSE);
Expand Down
20 changes: 14 additions & 6 deletions libs/lensfun/lens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,14 @@ void lfLens::GuessParameters ()
float minf = float (INT_MAX), maxf = float (INT_MIN);
float mina = float (INT_MAX), maxa = float (INT_MIN);

char *old_numeric = setlocale (LC_NUMERIC, NULL);
old_numeric = strdup (old_numeric);
#if defined(_MSC_VER)
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
setlocale (LC_NUMERIC, "C");
#else
auto loc = uselocale((locale_t) 0); // get current local
auto nloc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
uselocale(nloc);
#endif

if (Model && (!MinAperture || !MinFocal) &&
!strstr (Model, "adapter") &&
Expand Down Expand Up @@ -257,11 +262,14 @@ void lfLens::GuessParameters ()
if (maxa != (float)INT_MIN && !MaxAperture)
MaxAperture = maxa;

if (!MaxFocal)
MaxFocal = MinFocal;
if (!MaxFocal) MaxFocal = MinFocal;

setlocale (LC_NUMERIC, old_numeric);
free (old_numeric);
#if defined(_MSC_VER)
_configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
#else
uselocale(loc);
freelocale(nloc);
#endif
}

bool lfLens::Check ()
Expand Down

0 comments on commit bd4c56c

Please sign in to comment.