Skip to content

Commit

Permalink
optimize block size without overflows fix #5755
Browse files Browse the repository at this point in the history
  • Loading branch information
cfpp2p committed Jul 21, 2014
1 parent 3d7b408 commit e418740
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions libtransmission/metainfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* This exemption does not extend to derived works not owned by
* the Transmission project.
*
* $Id: metainfo.c 14234 2014-02-08 15:25:18Z cfpp2p $
* $Id: metainfo.c 14305 2014-07-21 12:27:38Z cfpp2p $
*/

#include <assert.h>
Expand Down Expand Up @@ -564,7 +564,7 @@ tr_metainfoParseImpl( const tr_session * session,

/* piece length */
if( !isMagnet ) {
if( !tr_bencDictFindInt( infoDict, "piece length", &i ) || ( i < 1 ) || ( i >= 4294967296 ) )
if( !tr_bencDictFindInt( infoDict, "piece length", &i ) || ( i < 1 ) || ( i >= 536870912 ) )
return "piece length";
inf->pieceSize = i;
}
Expand Down
30 changes: 24 additions & 6 deletions libtransmission/torrent.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,12 +705,30 @@ static void torrentStart( tr_torrent * tor, bool bypass_queue );
uint32_t
tr_getBlockSize( uint32_t pieceSize )
{
uint32_t b = pieceSize;

while( b > MAX_BLOCK_SIZE )
b /= 2u;

if( !b || ( pieceSize % b ) ) /* not cleanly divisible */
uint32_t b = MAX_BLOCK_SIZE;

// this was established in the previos revision logic
// do we really want to allow ANY piece sizes less than MAX_BLOCK_SIZE?
if( pieceSize <= MAX_BLOCK_SIZE ) return pieceSize;

/**
* ticket 4005
* use a MIN_BLOCK_SIZE of 8193 - same as previous revision
* lesser block sizes create an undo number of requests
* note - allowing a block size of 1 would guarantee any piece size
* but smaller requests result in higher overhead
* due to tracking a greater number of requests
* there should be some constraints on this
* we cant have everything
*/

// try for the LARGEST block size we can find
// not just any one thats greater than 8192 as per previous revision
// this way we are more efficient and less overhead
while( (b > 8193) && (pieceSize % b) )
b--;

if( pieceSize % b ) /* not cleanly divisible */
return 0;
return b;
}
Expand Down

0 comments on commit e418740

Please sign in to comment.