(trunk libT) more heap pruning: avoid an unnecessary malloc() + strcmp() + free() when parsing the initial handshake string from a peer

This commit is contained in:
Jordan Lee 2011-04-05 18:16:21 +00:00
parent e02ce61d3e
commit 758da07fe9
1 changed files with 3 additions and 7 deletions

View File

@ -599,7 +599,7 @@ readHandshake( tr_handshake * handshake,
struct evbuffer * inbuf )
{
uint8_t pstrlen;
uint8_t * pstr;
uint8_t pstr[20];
uint8_t reserved[HANDSHAKE_FLAGS_LEN];
uint8_t hash[SHA_DIGEST_LENGTH];
@ -649,15 +649,11 @@ readHandshake( tr_handshake * handshake,
evbuffer_drain( inbuf, 1 );
/* pstr (BitTorrent) */
pstr = tr_new( uint8_t, pstrlen + 1 );
assert( pstrlen == 19 );
tr_peerIoReadBytes( handshake->io, inbuf, pstr, pstrlen );
pstr[pstrlen] = '\0';
if( strcmp( (char*)pstr, "BitTorrent protocol" ) )
{
tr_free( pstr );
if( memcmp( pstr, "BitTorrent protocol", 19 ) )
return tr_handshakeDone( handshake, false );
}
tr_free( pstr );
/* reserved bytes */
tr_peerIoReadBytes( handshake->io, inbuf, reserved, sizeof( reserved ) );