(libT) #5249 'error parsing json in some locales': fix the bug; the new locale unit tests in json-test pass now

This commit is contained in:
Jordan Lee 2013-01-24 16:33:49 +00:00
parent 887850a636
commit eb4b2d609a
2 changed files with 18 additions and 12 deletions

View File

@ -226,8 +226,10 @@ main (void)
int i;
int n;
int rv;
char lc_numeric[128];
const char * comma_locales[] = { "da_DK.UTF-8", "fr_FR.UTF-8", "ru_RU.UTF-8"};
const char * comma_locales[] = { "da_DK.UTF-8",
"fr_FR.UTF-8",
"ru_RU.UTF-8"};
const testFunc tests[] = { test_elements,
test_utf8,
@ -237,7 +239,6 @@ main (void)
test_unescape };
/* run the tests in a locale with a decimal point of '.' */
tr_strlcpy (lc_numeric, setlocale (LC_NUMERIC, NULL), sizeof (lc_numeric));
setlocale (LC_NUMERIC, "C");
if ((rv = runTests (tests, NUM_TESTS (tests))))
return rv;
@ -251,7 +252,6 @@ main (void)
fprintf (stderr, "WARNING: unable to run locale-specific json tests.\n");
else if ((rv = runTests (tests, NUM_TESTS(tests))))
return rv;
setlocale (LC_NUMERIC, lc_numeric);
/* success */
return 0;

View File

@ -801,13 +801,8 @@ tr_variantWalk (const tr_variant * v,
{
int stackSize = 0;
int stackAlloc = 64;
char lc_numeric[128];
struct SaveNode * stack = tr_new (struct SaveNode, stackAlloc);
/* always use a '.' decimal point s.t. locale-hopping doesn't bite us */
tr_strlcpy (lc_numeric, setlocale (LC_NUMERIC, NULL), sizeof (lc_numeric));
setlocale (LC_NUMERIC, "C");
nodeConstruct (&stack[stackSize++], v, sort_dicts);
while (stackSize > 0)
@ -898,9 +893,6 @@ tr_variantWalk (const tr_variant * v,
}
}
/* restore the locale... */
setlocale (LC_NUMERIC, lc_numeric);
tr_free (stack);
}
@ -1095,8 +1087,13 @@ tr_variantMergeDicts (tr_variant * target, const tr_variant * source)
struct evbuffer *
tr_variantToBuf (const tr_variant * v, tr_variant_fmt fmt)
{
char lc_numeric[128];
struct evbuffer * buf = evbuffer_new();
/* parse with LC_NUMERIC="C" to ensure a "." decimal separator */
tr_strlcpy (lc_numeric, setlocale (LC_NUMERIC, NULL), sizeof (lc_numeric));
setlocale (LC_NUMERIC, "C");
evbuffer_expand (buf, 4096); /* alloc a little memory to start off with */
switch (fmt)
@ -1114,6 +1111,8 @@ tr_variantToBuf (const tr_variant * v, tr_variant_fmt fmt)
break;
}
/* restore the previous locale */
setlocale (LC_NUMERIC, lc_numeric);
return buf;
}
@ -1261,6 +1260,11 @@ tr_variantFromBuf (tr_variant * setme,
const char ** setme_end)
{
int err;
char lc_numeric[128];
/* parse with LC_NUMERIC="C" to ensure a "." decimal separator */
tr_strlcpy (lc_numeric, setlocale (LC_NUMERIC, NULL), sizeof (lc_numeric));
setlocale (LC_NUMERIC, "C");
switch (fmt)
{
@ -1274,5 +1278,7 @@ tr_variantFromBuf (tr_variant * setme,
break;
}
/* restore the previous locale */
setlocale (LC_NUMERIC, lc_numeric);
return err;
}