Changeset 8372


Ignore:
Timestamp:
May 9, 2009, 6:01:29 PM (12 years ago)
Author:
charles
Message:

(trunk libT) #2046: fix JSON bug when parsing floating point numbers in some locales

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/JSON_parser.c

    r7632 r8372  
    5555#include <ctype.h>
    5656#include <float.h>
     57#include <locale.h>
    5758#include <stddef.h>
    5859#include <stdio.h>
     
    477478                        value.vu.str.length = jc->parse_buffer_count;
    478479                    } else {
     480                        /* the json spec requires a '.' decimal point regardless of locale */
     481                        char numeric[128];
     482                        snprintf(numeric, sizeof(numeric), "%s", setlocale(LC_NUMERIC, NULL));
     483                        setlocale(LC_NUMERIC, "POSIX" );
    479484                        sscanf(jc->parse_buffer, "%Lf", &value.vu.float_value);
     485                        value.vu.float_value = strtod(jc->parse_buffer, NULL);
     486                        setlocale(LC_NUMERIC, numeric);
    480487                    }
    481488                    break;
  • trunk/libtransmission/bencode.c

    r8248 r8372  
    480480    {
    481481        char * endptr;
    482         char * locale;
     482        char locale[128];
    483483        double d;
    484484
    485485        /* the json spec requires a '.' decimal point regardless of locale */
    486         locale = tr_strdup( setlocale ( LC_NUMERIC, NULL ) );
     486        tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
    487487        setlocale( LC_NUMERIC, "POSIX" );
    488488        d  = strtod( val->val.s.s, &endptr );
    489489        setlocale( LC_NUMERIC, locale );
    490         tr_free( locale );
    491490
    492491        if(( success = ( val->val.s.s != endptr ) && !*endptr ))
     
    10631062{
    10641063    char buf[128];
    1065     char * locale;
     1064    char locale[128];
    10661065    size_t len;
    10671066
    10681067    /* always use a '.' decimal point s.t. locale-hopping doesn't bite us */
    1069     locale = tr_strdup( setlocale ( LC_NUMERIC, NULL ) );
     1068    tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
    10701069    setlocale( LC_NUMERIC, "POSIX" );
    10711070    tr_snprintf( buf, sizeof( buf ), "%f", val->val.d );
    10721071    setlocale( LC_NUMERIC, locale );
    1073     tr_free( locale );
    10741072
    10751073    len = strlen( buf );
     
    12791277{
    12801278    struct jsonWalk * data = vdata;
    1281     char * locale;
     1279    char locale[128];
    12821280
    12831281    /* json requires a '.' decimal point regardless of locale */
    1284     locale = tr_strdup( setlocale ( LC_NUMERIC, NULL ) );
     1282    tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
    12851283    setlocale( LC_NUMERIC, "POSIX" );
    12861284    evbuffer_add_printf( data->out, "%f", val->val.d );
    12871285    setlocale( LC_NUMERIC, locale );
    1288     tr_free( locale );
    12891286
    12901287    jsonChildFunc( data );
Note: See TracChangeset for help on using the changeset viewer.