1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-19 04:41:11 +00:00

(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 i;
int n; int n;
int rv; 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, const testFunc tests[] = { test_elements,
test_utf8, test_utf8,
@ -237,7 +239,6 @@ main (void)
test_unescape }; test_unescape };
/* run the tests in a locale with a decimal point of '.' */ /* 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"); setlocale (LC_NUMERIC, "C");
if ((rv = runTests (tests, NUM_TESTS (tests)))) if ((rv = runTests (tests, NUM_TESTS (tests))))
return rv; return rv;
@ -251,7 +252,6 @@ main (void)
fprintf (stderr, "WARNING: unable to run locale-specific json tests.\n"); fprintf (stderr, "WARNING: unable to run locale-specific json tests.\n");
else if ((rv = runTests (tests, NUM_TESTS(tests)))) else if ((rv = runTests (tests, NUM_TESTS(tests))))
return rv; return rv;
setlocale (LC_NUMERIC, lc_numeric);
/* success */ /* success */
return 0; return 0;

View file

@ -801,13 +801,8 @@ tr_variantWalk (const tr_variant * v,
{ {
int stackSize = 0; int stackSize = 0;
int stackAlloc = 64; int stackAlloc = 64;
char lc_numeric[128];
struct SaveNode * stack = tr_new (struct SaveNode, stackAlloc); 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); nodeConstruct (&stack[stackSize++], v, sort_dicts);
while (stackSize > 0) while (stackSize > 0)
@ -898,9 +893,6 @@ tr_variantWalk (const tr_variant * v,
} }
} }
/* restore the locale... */
setlocale (LC_NUMERIC, lc_numeric);
tr_free (stack); tr_free (stack);
} }
@ -1095,8 +1087,13 @@ tr_variantMergeDicts (tr_variant * target, const tr_variant * source)
struct evbuffer * struct evbuffer *
tr_variantToBuf (const tr_variant * v, tr_variant_fmt fmt) tr_variantToBuf (const tr_variant * v, tr_variant_fmt fmt)
{ {
char lc_numeric[128];
struct evbuffer * buf = evbuffer_new(); 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 */ evbuffer_expand (buf, 4096); /* alloc a little memory to start off with */
switch (fmt) switch (fmt)
@ -1114,6 +1111,8 @@ tr_variantToBuf (const tr_variant * v, tr_variant_fmt fmt)
break; break;
} }
/* restore the previous locale */
setlocale (LC_NUMERIC, lc_numeric);
return buf; return buf;
} }
@ -1261,6 +1260,11 @@ tr_variantFromBuf (tr_variant * setme,
const char ** setme_end) const char ** setme_end)
{ {
int err; 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) switch (fmt)
{ {
@ -1274,5 +1278,7 @@ tr_variantFromBuf (tr_variant * setme,
break; break;
} }
/* restore the previous locale */
setlocale (LC_NUMERIC, lc_numeric);
return err; return err;
} }