From 7f08a87969a7a7f5a7d79bf26845c500b04b556e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 9 May 2009 18:01:29 +0000 Subject: [PATCH] (trunk libT) #2046: fix JSON bug when parsing floating point numbers in some locales --- libtransmission/JSON_parser.c | 7 +++++++ libtransmission/bencode.c | 15 ++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/libtransmission/JSON_parser.c b/libtransmission/JSON_parser.c index 0f409129e..284f3f693 100644 --- a/libtransmission/JSON_parser.c +++ b/libtransmission/JSON_parser.c @@ -54,6 +54,7 @@ SOFTWARE. #include #include #include +#include #include #include #include @@ -476,7 +477,13 @@ static int parse_parse_buffer(JSON_parser jc) value.vu.str.value = jc->parse_buffer; value.vu.str.length = jc->parse_buffer_count; } else { + /* the json spec requires a '.' decimal point regardless of locale */ + char numeric[128]; + snprintf(numeric, sizeof(numeric), "%s", setlocale(LC_NUMERIC, NULL)); + setlocale(LC_NUMERIC, "POSIX" ); sscanf(jc->parse_buffer, "%Lf", &value.vu.float_value); + value.vu.float_value = strtod(jc->parse_buffer, NULL); + setlocale(LC_NUMERIC, numeric); } break; case JSON_T_INTEGER: diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index 2cdd6e47b..1ee17e389 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -479,15 +479,14 @@ tr_bencGetReal( const tr_benc * val, double * setme ) if( !success && tr_bencIsString(val) ) { char * endptr; - char * locale; + char locale[128]; double d; /* the json spec requires a '.' decimal point regardless of locale */ - locale = tr_strdup( setlocale ( LC_NUMERIC, NULL ) ); + tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) ); setlocale( LC_NUMERIC, "POSIX" ); d = strtod( val->val.s.s, &endptr ); setlocale( LC_NUMERIC, locale ); - tr_free( locale ); if(( success = ( val->val.s.s != endptr ) && !*endptr )) *setme = d; @@ -1062,15 +1061,14 @@ static void saveRealFunc( const tr_benc * val, void * evbuf ) { char buf[128]; - char * locale; + char locale[128]; size_t len; /* always use a '.' decimal point s.t. locale-hopping doesn't bite us */ - locale = tr_strdup( setlocale ( LC_NUMERIC, NULL ) ); + tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) ); setlocale( LC_NUMERIC, "POSIX" ); tr_snprintf( buf, sizeof( buf ), "%f", val->val.d ); setlocale( LC_NUMERIC, locale ); - tr_free( locale ); len = strlen( buf ); evbuffer_add_printf( evbuf, "%lu:", (unsigned long)len ); @@ -1278,14 +1276,13 @@ static void jsonRealFunc( const tr_benc * val, void * vdata ) { struct jsonWalk * data = vdata; - char * locale; + char locale[128]; /* json requires a '.' decimal point regardless of locale */ - locale = tr_strdup( setlocale ( LC_NUMERIC, NULL ) ); + tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) ); setlocale( LC_NUMERIC, "POSIX" ); evbuffer_add_printf( data->out, "%f", val->val.d ); setlocale( LC_NUMERIC, locale ); - tr_free( locale ); jsonChildFunc( data ); }