From 2d2d173275b61ad67c0d446afd0f24d6d08a42d8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 21 Aug 2008 18:40:40 +0000 Subject: [PATCH] (libT) avoid an unnecessary memory alloc --- libtransmission/bencode-test.c | 9 +++------ libtransmission/bencode.c | 10 ++++------ libtransmission/bencode.h | 2 +- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/libtransmission/bencode-test.c b/libtransmission/bencode-test.c index d4bd52345..bc330c4ca 100644 --- a/libtransmission/bencode-test.c +++ b/libtransmission/bencode-test.c @@ -89,17 +89,16 @@ testStr( void ) uint8_t buf[128]; int err; const uint8_t * end; - uint8_t * str; + const uint8_t * str; size_t len; /* good string */ tr_snprintf( (char*)buf, sizeof( buf ), "4:boat" ); err = tr_bencParseStr( buf, buf+6, &end, &str, &len ); check( err == TR_OK ); - check( !strcmp( (char*)str, "boat" ) ); + check( !strncmp( (char*)str, "boat", len ) ); check( len == 4 ); check( end == buf + 6 ); - tr_free( str ); str = NULL; end = NULL; len = 0; @@ -118,7 +117,6 @@ testStr( void ) check( !*str ); check( !len ); check( end == buf + 2 ); - tr_free( str ); str = NULL; end = NULL; len = 0; @@ -127,10 +125,9 @@ testStr( void ) tr_snprintf( (char*)buf, sizeof( buf ), "3:boat" ); err = tr_bencParseStr( buf, buf+6, &end, &str, &len ); check( err == TR_OK ); - check( !strcmp( (char*)str, "boa" ) ); + check( !strncmp( (char*)str, "boa", len ) ); check( len == 3 ); check( end == buf + 5 ); - tr_free( str ); str = NULL; end = NULL; len = 0; diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index 4a33b00dd..ad6848c14 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -118,7 +118,7 @@ int tr_bencParseStr( const uint8_t * buf, const uint8_t * bufend, const uint8_t ** setme_end, - uint8_t ** setme_str, + const uint8_t ** setme_str, size_t * setme_strlen ) { size_t len; @@ -144,7 +144,7 @@ tr_bencParseStr( const uint8_t * buf, return TR_ERROR; *setme_end = end + 1 + len; - *setme_str = tr_memdup( end+1, len ); + *setme_str = end + 1; *setme_strlen = len; return TR_OK; } @@ -279,7 +279,7 @@ tr_bencParseImpl( const void * buf_in, else if( isdigit(*buf) ) /* string? */ { const uint8_t * end; - uint8_t * str; + const uint8_t * str; size_t str_len; int err; tr_benc * node; @@ -288,10 +288,8 @@ tr_bencParseImpl( const void * buf_in, return err; node = getNode( top, parentStack, TYPE_STR ); - if( !node ) { - tr_free( str ); + if( !node ) return TR_ERROR; - } tr_bencInitStr( node, str, str_len ); buf = end; diff --git a/libtransmission/bencode.h b/libtransmission/bencode.h index 331d651ae..8630fc154 100644 --- a/libtransmission/bencode.h +++ b/libtransmission/bencode.h @@ -140,7 +140,7 @@ int tr_bencParseInt( const uint8_t * buf, int tr_bencParseStr( const uint8_t * buf, const uint8_t * bufend, const uint8_t ** setme_end, - uint8_t ** setme_str, + const uint8_t ** setme_str, size_t * setme_strlen ); #endif