From 624c8ff1cc55bf1af6f0f5aff339dafd8e2e8254 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Wed, 19 Sep 2012 05:11:19 +0000 Subject: [PATCH] (trunk, libt) #4932 'crash on magnet links' -- fixed. --- libtransmission/completion.c | 6 +++++- libtransmission/peer-msgs.c | 5 ++++- libtransmission/rpcimpl.c | 16 ++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/libtransmission/completion.c b/libtransmission/completion.c index c0538c439..77a9015ec 100644 --- a/libtransmission/completion.c +++ b/libtransmission/completion.c @@ -271,8 +271,12 @@ void * tr_cpCreatePieceBitfield( const tr_completion * cp, size_t * byte_count ) { void * ret; + tr_piece_index_t n; tr_bitfield pieces; - const tr_piece_index_t n = cp->tor->info.pieceCount; + + assert( tr_torrentHasMetadata( cp->tor ) ); + + n = cp->tor->info.pieceCount; tr_bitfieldConstruct( &pieces, n ); if( tr_cpHasAll( cp ) ) diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index 2895e460e..fb91164c5 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -1984,10 +1984,13 @@ gotError( tr_peerIo * io UNUSED, short what, void * vmsgs ) static void sendBitfield( tr_peermsgs * msgs ) { + void * bytes; size_t byte_count = 0; struct evbuffer * out = msgs->outMessages; - void * bytes = tr_cpCreatePieceBitfield( &msgs->torrent->completion, &byte_count ); + assert( tr_torrentHasMetadata( msgs->torrent ) ); + + bytes = tr_cpCreatePieceBitfield( &msgs->torrent->completion, &byte_count ); evbuffer_add_uint32( out, sizeof( uint8_t ) + byte_count ); evbuffer_add_uint8 ( out, BT_BITFIELD ); evbuffer_add ( out, bytes, byte_count ); diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index 2404276de..34ec3b72d 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -652,12 +652,16 @@ addField( const tr_torrent * const tor, else if( tr_streq( key, keylen, "peersSendingToUs" ) ) tr_bencDictAddInt( d, key, st->peersSendingToUs ); else if( tr_streq( key, keylen, "pieces" ) ) { - size_t byte_count = 0; - void * bytes = tr_cpCreatePieceBitfield( &tor->completion, &byte_count ); - char * str = tr_base64_encode( bytes, byte_count, NULL ); - tr_bencDictAddStr( d, key, str!=NULL ? str : "" ); - tr_free( str ); - tr_free( bytes ); + if (tr_torrentHasMetadata( tor ) ) { + size_t byte_count = 0; + void * bytes = tr_cpCreatePieceBitfield( &tor->completion, &byte_count ); + char * str = tr_base64_encode( bytes, byte_count, NULL ); + tr_bencDictAddStr( d, key, str!=NULL ? str : "" ); + tr_free( str ); + tr_free( bytes ); + } else { + tr_bencDictAddStr( d, key, "" ); + } } else if( tr_streq( key, keylen, "pieceCount" ) ) tr_bencDictAddInt( d, key, inf->pieceCount );