From eb4b2d609aee8c43f5b7f9e5cf6f847ee43c2ae5 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Thu, 24 Jan 2013 16:33:49 +0000 Subject: [PATCH] (libT) #5249 'error parsing json in some locales': fix the bug; the new locale unit tests in json-test pass now --- libtransmission/json-test.c | 8 ++++---- libtransmission/variant.c | 22 ++++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/libtransmission/json-test.c b/libtransmission/json-test.c index 0ed1bce22..bc20b1488 100644 --- a/libtransmission/json-test.c +++ b/libtransmission/json-test.c @@ -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; diff --git a/libtransmission/variant.c b/libtransmission/variant.c index dc92d101f..8290e9f77 100644 --- a/libtransmission/variant.c +++ b/libtransmission/variant.c @@ -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; } -- 2.40.0