From 230193053ec8b3fe311e9b33290b76863a062e96 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 24 Jul 2010 17:09:39 +0000 Subject: [PATCH] (trunk libT) #3449 "overflow error in tr_truncd()" -- fixed. --- libtransmission/utils-test.c | 26 +++++++++++++++++++------- libtransmission/utils.c | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index 63302e6e5..0bf428558 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -375,21 +375,31 @@ test_url( void ) return 0; } +static int +test_truncd( void ) +{ + char buf[32]; + + tr_snprintf( buf, sizeof( buf ), "%.2f%%", 99.999 ); + check( !strcmp( buf, "100.00%" ) ); + + tr_snprintf( buf, sizeof( buf ), "%.2f%%", tr_truncd( 99.999, 2 ) ); + check( !strcmp( buf, "99.99%" ) ); + + tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) ); + check( !strcmp( buf, "403650.6562" ) ); + + return 0; +} + int main( void ) { - char buf[32]; char *in, *out; int len; int i; int l; - /* tr_truncd */ - tr_snprintf( buf, sizeof( buf ), "%.2f%%", 99.999 ); - check( !strcmp( buf, "100.00%" ) ); - tr_snprintf( buf, sizeof( buf ), "%.2f%%", tr_truncd( 99.999, 2 ) ); - check( !strcmp( buf, "99.99%" ) ); - /* base64 */ out = tr_base64_encode( "YOYO!", -1, &len ); check( out ); @@ -425,6 +435,8 @@ main( void ) return i; if( ( i = test_url( ) ) ) return i; + if( ( i = test_truncd( ) ) ) + return i; /* test that tr_cryptoRandInt() stays in-bounds */ for( i = 0; i < 100000; ++i ) diff --git a/libtransmission/utils.c b/libtransmission/utils.c index b71a62033..96a37cc02 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -1386,7 +1386,7 @@ double tr_truncd( double x, int decimal_places ) { const int i = (int) pow( 10, decimal_places ); - double x2 = (int)(x*i); + const double x2 = (int64_t)(x * i); return x2 / i; }