1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-09 05:14:09 +00:00

(libT) avoid an unnecessary memory alloc

This commit is contained in:
Charles Kerr 2008-08-21 18:40:40 +00:00
parent 3f25d101e0
commit 2d2d173275
3 changed files with 8 additions and 13 deletions

View file

@ -89,17 +89,16 @@ testStr( void )
uint8_t buf[128]; uint8_t buf[128];
int err; int err;
const uint8_t * end; const uint8_t * end;
uint8_t * str; const uint8_t * str;
size_t len; size_t len;
/* good string */ /* good string */
tr_snprintf( (char*)buf, sizeof( buf ), "4:boat" ); tr_snprintf( (char*)buf, sizeof( buf ), "4:boat" );
err = tr_bencParseStr( buf, buf+6, &end, &str, &len ); err = tr_bencParseStr( buf, buf+6, &end, &str, &len );
check( err == TR_OK ); check( err == TR_OK );
check( !strcmp( (char*)str, "boat" ) ); check( !strncmp( (char*)str, "boat", len ) );
check( len == 4 ); check( len == 4 );
check( end == buf + 6 ); check( end == buf + 6 );
tr_free( str );
str = NULL; str = NULL;
end = NULL; end = NULL;
len = 0; len = 0;
@ -118,7 +117,6 @@ testStr( void )
check( !*str ); check( !*str );
check( !len ); check( !len );
check( end == buf + 2 ); check( end == buf + 2 );
tr_free( str );
str = NULL; str = NULL;
end = NULL; end = NULL;
len = 0; len = 0;
@ -127,10 +125,9 @@ testStr( void )
tr_snprintf( (char*)buf, sizeof( buf ), "3:boat" ); tr_snprintf( (char*)buf, sizeof( buf ), "3:boat" );
err = tr_bencParseStr( buf, buf+6, &end, &str, &len ); err = tr_bencParseStr( buf, buf+6, &end, &str, &len );
check( err == TR_OK ); check( err == TR_OK );
check( !strcmp( (char*)str, "boa" ) ); check( !strncmp( (char*)str, "boa", len ) );
check( len == 3 ); check( len == 3 );
check( end == buf + 5 ); check( end == buf + 5 );
tr_free( str );
str = NULL; str = NULL;
end = NULL; end = NULL;
len = 0; len = 0;

View file

@ -118,7 +118,7 @@ int
tr_bencParseStr( const uint8_t * buf, tr_bencParseStr( const uint8_t * buf,
const uint8_t * bufend, const uint8_t * bufend,
const uint8_t ** setme_end, const uint8_t ** setme_end,
uint8_t ** setme_str, const uint8_t ** setme_str,
size_t * setme_strlen ) size_t * setme_strlen )
{ {
size_t len; size_t len;
@ -144,7 +144,7 @@ tr_bencParseStr( const uint8_t * buf,
return TR_ERROR; return TR_ERROR;
*setme_end = end + 1 + len; *setme_end = end + 1 + len;
*setme_str = tr_memdup( end+1, len ); *setme_str = end + 1;
*setme_strlen = len; *setme_strlen = len;
return TR_OK; return TR_OK;
} }
@ -279,7 +279,7 @@ tr_bencParseImpl( const void * buf_in,
else if( isdigit(*buf) ) /* string? */ else if( isdigit(*buf) ) /* string? */
{ {
const uint8_t * end; const uint8_t * end;
uint8_t * str; const uint8_t * str;
size_t str_len; size_t str_len;
int err; int err;
tr_benc * node; tr_benc * node;
@ -288,10 +288,8 @@ tr_bencParseImpl( const void * buf_in,
return err; return err;
node = getNode( top, parentStack, TYPE_STR ); node = getNode( top, parentStack, TYPE_STR );
if( !node ) { if( !node )
tr_free( str );
return TR_ERROR; return TR_ERROR;
}
tr_bencInitStr( node, str, str_len ); tr_bencInitStr( node, str, str_len );
buf = end; buf = end;

View file

@ -140,7 +140,7 @@ int tr_bencParseInt( const uint8_t * buf,
int tr_bencParseStr( const uint8_t * buf, int tr_bencParseStr( const uint8_t * buf,
const uint8_t * bufend, const uint8_t * bufend,
const uint8_t ** setme_end, const uint8_t ** setme_end,
uint8_t ** setme_str, const uint8_t ** setme_str,
size_t * setme_strlen ); size_t * setme_strlen );
#endif #endif