mirror of
https://github.com/transmission/transmission
synced 2025-03-06 19:48:15 +00:00
(libT) avoid an unnecessary memory alloc
This commit is contained in:
parent
3f25d101e0
commit
2d2d173275
3 changed files with 8 additions and 13 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue