From f73f223540f3c35d4307f9bb1fa03495c10b022d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 9 Dec 2009 12:44:23 +0000 Subject: [PATCH] (trunk libT) #2653 "transmission-remote-dotnet makes transmission-daemon segfault" -- fixed for 1.80 --- libtransmission/rpcimpl.c | 2 +- libtransmission/utils-test.c | 3 +++ libtransmission/utils.c | 41 ++++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index a85790b15..362521cd9 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -535,7 +535,7 @@ addField( const tr_torrent * tor, tr_benc * d, const char * key ) else if( tr_streq( key, keylen, "pieces" ) ) { const tr_bitfield * pieces = tr_cpPieceBitfield( &tor->completion ); char * str = tr_base64_encode( pieces->bits, pieces->byteCount, NULL ); - tr_bencDictAddStr( d, key, str ); + tr_bencDictAddStr( d, key, str!=NULL ? str : "" ); tr_free( str ); } else if( tr_streq( key, keylen, "pieceCount" ) ) diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index 0b0066f7e..95ba3abc9 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -345,6 +345,9 @@ main( void ) check( len == 5 ); tr_free( in ); tr_free( out ); + out = tr_base64_encode( NULL, 0, &len ); + check( out == NULL ); + check( len == 0 ); if( ( i = test_hex( ) ) ) return i; diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 3ca68b5f6..a20c1adfb 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -1036,29 +1036,34 @@ tr_httpParseURL( const char * url_in, #include char * -tr_base64_encode( const void * input, - int length, - int * setme_len ) +tr_base64_encode( const void * input, int length, int * setme_len ) { - char * ret; - BIO * b64; - BIO * bmem; - BUF_MEM * bptr; + int retlen = 0; + char * ret = NULL; - if( length < 1 ) - length = strlen( input ); + if( input != NULL ) + { + BIO * b64; + BIO * bmem; + BUF_MEM * bptr; + + if( length < 1 ) + length = strlen( input ); + + bmem = BIO_new( BIO_s_mem( ) ); + b64 = BIO_new( BIO_f_base64( ) ); + b64 = BIO_push( b64, bmem ); + BIO_write( b64, input, length ); + (void) BIO_flush( b64 ); + BIO_get_mem_ptr( b64, &bptr ); + ret = tr_strndup( bptr->data, bptr->length ); + retlen = bptr->length; + BIO_free_all( b64 ); + } - bmem = BIO_new( BIO_s_mem( ) ); - b64 = BIO_new( BIO_f_base64( ) ); - b64 = BIO_push( b64, bmem ); - BIO_write( b64, input, length ); - (void) BIO_flush( b64 ); - BIO_get_mem_ptr( b64, &bptr ); - ret = tr_strndup( bptr->data, bptr->length ); if( setme_len ) - *setme_len = bptr->length; + *setme_len = retlen; - BIO_free_all( b64 ); return ret; }