From 254a1f15cbc658108940aa4cd9f81c8dc79d78f2 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 30 Jun 2010 16:40:19 +0000 Subject: [PATCH] (trunk libT) down the rabbit hole: various minor type correctness changes unearthed by -Wconversion --- libtransmission/bencode.c | 8 +++---- libtransmission/completion.c | 4 ++-- libtransmission/crypto.c | 4 ++-- libtransmission/handshake.c | 38 ++++++++++++++++------------------ libtransmission/peer-io.c | 5 ++--- libtransmission/peer-io.h | 10 ++++----- libtransmission/torrent.c | 17 +++++++-------- libtransmission/torrent.h | 6 +++--- libtransmission/tr-lpd.c | 2 +- libtransmission/transmission.h | 12 ++++++----- libtransmission/utils.c | 17 ++++++++------- libtransmission/utils.h | 6 +++--- libtransmission/verify.c | 10 ++++----- 13 files changed, 67 insertions(+), 72 deletions(-) diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index 1e8e9d9bf..255e6f229 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -65,8 +65,7 @@ isSomething( const tr_benc * val ) } static void -tr_bencInit( tr_benc * val, - int type ) +tr_bencInit( tr_benc * val, char type ) { memset( val, 0, sizeof( *val ) ); val->type = type; @@ -468,10 +467,9 @@ tr_bencGetInt( const tr_benc * val, } tr_bool -tr_bencGetStr( const tr_benc * val, - const char ** setme ) +tr_bencGetStr( const tr_benc * val, const char ** setme ) { - const int success = tr_bencIsString( val ); + const tr_bool success = tr_bencIsString( val ); if( success ) *setme = getStr( val ); diff --git a/libtransmission/completion.c b/libtransmission/completion.c index 70125eb5b..acf858a0c 100644 --- a/libtransmission/completion.c +++ b/libtransmission/completion.c @@ -186,7 +186,7 @@ tr_cpSetHaveAll( tr_completion * cp ) tr_bool tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * blockBitfield ) { - int success = FALSE; + tr_bool success = FALSE; assert( cp ); assert( blockBitfield ); @@ -198,7 +198,7 @@ tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * blockBitfield ) tr_block_index_t b = 0; tr_piece_index_t p = 0; uint32_t pieceBlock = 0; - uint32_t completeBlocksInPiece = 0; + uint16_t completeBlocksInPiece = 0; tr_block_index_t completeBlocksInTorrent = 0; uint32_t blocksInCurrentPiece = tr_torPieceCountBlocks( cp->tor, p ); diff --git a/libtransmission/crypto.c b/libtransmission/crypto.c index 5164c06ed..dce13face 100644 --- a/libtransmission/crypto.c +++ b/libtransmission/crypto.c @@ -368,12 +368,12 @@ tr_cryptoRandBuf( void * buf, size_t len ) char* tr_ssha1( const void * plaintext ) { + enum { saltval_len = 8, + salter_len = 64 }; static const char * salter = "0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "./"; - const size_t salter_len = 64; - const size_t saltval_len = 8; size_t i; char salt[saltval_len]; diff --git a/libtransmission/handshake.c b/libtransmission/handshake.c index 330d24a0a..e18873687 100644 --- a/libtransmission/handshake.c +++ b/libtransmission/handshake.c @@ -99,6 +99,8 @@ enum #define HANDSHAKE_SET_EXTPREF( reserved, val ) ( ( reserved )[5] |= 0x03 &\ ( val ) ) +typedef uint8_t handshake_state_t; + struct tr_handshake { tr_bool haveReadAnythingFromPeer; @@ -108,7 +110,7 @@ struct tr_handshake tr_crypto * crypto; tr_session * session; uint8_t mySecret[KEY_LEN]; - uint8_t state; + handshake_state_t state; tr_encryption_mode encryptionMode; uint16_t pad_c_len; uint16_t pad_d_len; @@ -155,7 +157,7 @@ enum } while( 0 ) static const char* -getStateName( short state ) +getStateName( const handshake_state_t state ) { const char * str = "f00!"; @@ -198,16 +200,14 @@ getStateName( short state ) } static void -setState( tr_handshake * handshake, - short state ) +setState( tr_handshake * handshake, handshake_state_t state ) { dbgmsg( handshake, "setting to state [%s]", getStateName( state ) ); handshake->state = state; } static void -setReadState( tr_handshake * handshake, - int state ) +setReadState( tr_handshake * handshake, handshake_state_t state ) { setState( handshake, state ); } @@ -588,8 +588,7 @@ readPadD( tr_handshake * handshake, tr_peerIoReadBytes( handshake->io, inbuf, tmp, needlen ); tr_free( tmp ); - tr_peerIoSetEncryption( handshake->io, - handshake->crypto_select ); + tr_peerIoSetEncryption( handshake->io, handshake->crypto_select ); setState( handshake, AWAITING_HANDSHAKE ); return READ_NOW; @@ -725,7 +724,7 @@ static int readPeerId( tr_handshake * handshake, struct evbuffer * inbuf ) { - int peerIsGood; + tr_bool peerIsGood; char client[128]; tr_torrent * tor; const uint8_t * tor_peer_id; @@ -746,7 +745,7 @@ readPeerId( tr_handshake * handshake, tor = tr_torrentFindFromHash( handshake->session, tr_peerIoGetTorrentHash( handshake->io ) ); tor_peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( ); peerIsGood = memcmp( peer_id, tor_peer_id, PEER_ID_LEN ) != 0; - dbgmsg( handshake, "isPeerGood == %d", peerIsGood ); + dbgmsg( handshake, "isPeerGood == %d", (int)peerIsGood ); return tr_handshakeDone( handshake, peerIsGood ); } @@ -965,7 +964,7 @@ readIA( tr_handshake * handshake, * PadD is reserved for future extensions to the handshake... * standard practice at this time is for it to be zero-length */ { - const int len = 0; + const uint16_t len = 0; tr_peerIoWriteUint16( handshake->io, outbuf, len ); } @@ -1096,19 +1095,18 @@ canRead( struct tr_peerIo * io, void * arg, size_t * piece ) return ret; } -static int -fireDoneFunc( tr_handshake * handshake, - tr_bool isConnected ) +static tr_bool +fireDoneFunc( tr_handshake * handshake, tr_bool isConnected ) { const uint8_t * peer_id = isConnected && handshake->havePeerID ? tr_peerIoGetPeersId( handshake->io ) : NULL; - const int success = ( *handshake->doneCB )( handshake, - handshake->io, - handshake->haveReadAnythingFromPeer, - isConnected, - peer_id, - handshake->doneUserData ); + const tr_bool success = ( *handshake->doneCB )( handshake, + handshake->io, + handshake->haveReadAnythingFromPeer, + isConnected, + peer_id, + handshake->doneUserData ); return success; } diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 29fa306f3..4b312763d 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -634,7 +634,7 @@ tr_peerIoClear( tr_peerIo * io ) int tr_peerIoReconnect( tr_peerIo * io ) { - int pendingEvents; + short int pendingEvents; tr_session * session; assert( tr_isPeerIo( io ) ); @@ -745,8 +745,7 @@ tr_peerIoGetWriteBufferSpace( const tr_peerIo * io, uint64_t now ) **/ void -tr_peerIoSetEncryption( tr_peerIo * io, - int encryptionMode ) +tr_peerIoSetEncryption( tr_peerIo * io, uint32_t encryptionMode ) { assert( tr_isPeerIo( io ) ); assert( encryptionMode == PEER_ENCRYPTION_NONE diff --git a/libtransmission/peer-io.h b/libtransmission/peer-io.h index 24fd4cafe..241ddb47c 100644 --- a/libtransmission/peer-io.h +++ b/libtransmission/peer-io.h @@ -77,11 +77,11 @@ typedef struct tr_peerIo tr_priority_t priority; - int pendingEvents; + short int pendingEvents; int magicNumber; - uint8_t encryptionMode; + uint32_t encryptionMode; tr_bool isSeed; tr_port port; @@ -276,10 +276,10 @@ typedef enum } EncryptionMode; -void tr_peerIoSetEncryption( tr_peerIo * io, - int encryptionMode ); +void tr_peerIoSetEncryption( tr_peerIo * io, uint32_t encryptionMode ); -static inline tr_bool tr_peerIoIsEncrypted( const tr_peerIo * io ) +static inline tr_bool +tr_peerIoIsEncrypted( const tr_peerIo * io ) { return ( io != NULL ) && ( io->encryptionMode == PEER_ENCRYPTION_RC4 ); } diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 5bc37084b..b253424df 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -370,7 +370,7 @@ onTrackerResponse( tr_torrent * tor, const tr_tracker_event * event, void * unus case TR_TRACKER_PEERS: { size_t i, n; - const int seedProbability = event->seedProbability; + const int8_t seedProbability = event->seedProbability; const tr_bool allAreSeeds = seedProbability == 100; tr_pex * pex = tr_peerMgrArrayToPex( event->compact, event->compactLen, &n ); @@ -416,9 +416,8 @@ onTrackerResponse( tr_torrent * tor, const tr_tracker_event * event, void * unus **** ***/ -static int -getBytePiece( const tr_info * info, - uint64_t byteOffset ) +static tr_piece_index_t +getBytePiece( const tr_info * info, uint64_t byteOffset ) { assert( info ); assert( info->pieceSize != 0 ); @@ -456,7 +455,7 @@ calculatePiecePriority( const tr_torrent * tor, int fileHint ) { tr_file_index_t i; - int priority = TR_PRI_LOW; + tr_priority_t priority = TR_PRI_LOW; /* find the first file that has data in this piece */ if( fileHint >= 0 ) { @@ -567,7 +566,7 @@ torrentInitFromInfo( tr_torrent * tor ) tor->blockSize = tr_getBlockSize( info->pieceSize ); if( info->pieceSize ) - tor->lastPieceSize = info->totalSize % info->pieceSize; + tor->lastPieceSize = (uint32_t)(info->totalSize % info->pieceSize); if( !tor->lastPieceSize ) tor->lastPieceSize = info->pieceSize; @@ -983,9 +982,7 @@ tr_torrentStat( tr_torrent * tor ) s->sizeWhenDone = tr_cpSizeWhenDone ( &tor->completion ); s->recheckProgress = s->activity == TR_STATUS_CHECK - ? 1.0 - - ( tr_torrentCountUncheckedPieces( tor ) / - (double) tor->info.pieceCount ) + ? 1.0 - ( tr_torrentCountUncheckedPieces( tor ) / (float) tor->info.pieceCount ) : 0.0; s->activityDate = tor->activityDate; @@ -1844,7 +1841,7 @@ static void setFileDND( tr_torrent * tor, tr_file_index_t fileIndex, int doDownload ) { tr_file * file; - const int dnd = !doDownload; + const uint8_t dnd = !doDownload; tr_piece_index_t firstPiece, firstPieceDND; tr_piece_index_t lastPiece, lastPieceDND; tr_file_index_t i; diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 48d77cf51..0c221dfaf 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -185,8 +185,8 @@ struct tr_torrent uint32_t lastBlockSize; uint32_t lastPieceSize; - uint32_t blockCountInPiece; - uint32_t blockCountInLastPiece; + uint16_t blockCountInPiece; + uint16_t blockCountInLastPiece; struct tr_completion completion; @@ -273,7 +273,7 @@ tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block ) } /* how many blocks are in this piece? */ -static inline uint32_t +static inline uint16_t tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece ) { return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece diff --git a/libtransmission/tr-lpd.c b/libtransmission/tr-lpd.c index 1daecc762..c35bdb6db 100644 --- a/libtransmission/tr-lpd.c +++ b/libtransmission/tr-lpd.c @@ -511,7 +511,7 @@ static int tr_lpdConsiderAnnounce( tr_pex* peer, const char* const msg ) return 0; /* determine announced peer port, refuse if value too large */ - if( sscanf( value, "%u", &peerPort ) != 1 || peerPort > (in_port_t)-1 ) + if( sscanf( value, "%d", &peerPort ) != 1 || peerPort > (in_port_t)-1 ) return 0; peer->port = htons( peerPort ); diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 3f0bdfae7..52a832b29 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -771,20 +771,22 @@ void tr_sessionSetTorrentDoneScript( tr_session *, const char * scriptFilename ) ** Message Logging */ -enum +typedef enum { TR_MSG_ERR = 1, TR_MSG_INF = 2, TR_MSG_DBG = 3 -}; -void tr_setMessageLevel( int ); +} +tr_msg_level; -int tr_getMessageLevel( void ); +void tr_setMessageLevel( tr_msg_level ); + +tr_msg_level tr_getMessageLevel( void ); typedef struct tr_msg_list { /* TR_MSG_ERR, TR_MSG_INF, or TR_MSG_DBG */ - uint8_t level; + tr_msg_level level; /* The line number in the source file where this message originated */ int line; diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 792c65b1a..a43dd4a22 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -56,7 +56,7 @@ time_t transmission_now = 0; -int messageLevel = TR_MSG_INF; +tr_msg_level messageLevel = TR_MSG_INF; static tr_lock * messageLock = NULL; static tr_bool messageQueuing = FALSE; static tr_msg_list * messageQueue = NULL; @@ -114,12 +114,12 @@ tr_getLog( void ) } void -tr_setMessageLevel( int level ) +tr_setMessageLevel( tr_msg_level level ) { - messageLevel = MAX( 0, level ); + messageLevel = level; } -int +tr_msg_level tr_getMessageLevel( void ) { return messageLevel; @@ -255,7 +255,8 @@ tr_deepLog( const char * file, void tr_msg( const char * file, int line, - int level, const char * name, + tr_msg_level level, + const char * name, const char * fmt, ... ) { const int err = errno; /* message logging shouldn't affect errno */ @@ -463,7 +464,7 @@ tr_loadFile( const char * path, struct stat sb; int fd; ssize_t n; - const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); + const char * const err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); /* try to stat the file */ errno = 0; @@ -500,7 +501,7 @@ tr_loadFile( const char * path, errno = err; return NULL; } - n = read( fd, buf, sb.st_size ); + n = read( fd, buf, (size_t)sb.st_size ); if( n == -1 ) { const int err = errno; @@ -1391,7 +1392,7 @@ tr_moveFile( const char * oldpath, const char * newpath, tr_bool * renamed ) char * buf; struct stat st; off_t bytesLeft; - const off_t buflen = 1024 * 128; /* 128 KiB buffer */ + const size_t buflen = 1024 * 128; /* 128 KiB buffer */ /* make sure the old file exists */ if( stat( oldpath, &st ) ) { diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 5b7476bb6..3d24d5bb0 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -118,15 +118,15 @@ void tr_msgInit( void ); #define TR_MAX_MSG_LOG 10000 -extern int messageLevel; +extern tr_msg_level messageLevel; -static inline tr_bool tr_msgLoggingIsActive( int level ) +static inline tr_bool tr_msgLoggingIsActive( tr_msg_level level ) { return messageLevel >= level; } void tr_msg( const char * file, int line, - int level, + tr_msg_level level, const char * torrent, const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 ); diff --git a/libtransmission/verify.c b/libtransmission/verify.c index 16f4b2124..1e206873e 100644 --- a/libtransmission/verify.c +++ b/libtransmission/verify.c @@ -60,7 +60,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag ) tr_piece_index_t pieceIndex = 0; const time_t begin = tr_time( ); time_t end; - const int64_t buflen = 1024 * 128; /* 128 KiB buffer */ + const size_t buflen = 1024 * 128; /* 128 KiB buffer */ uint8_t * buffer = tr_valloc( buflen ); tr_torrentUncheck( tor ); @@ -69,9 +69,9 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag ) while( !*stopFlag && ( pieceIndex < tor->info.pieceCount ) ) { - int64_t leftInPiece; - int64_t leftInFile; - int64_t bytesThisPass; + uint32_t leftInPiece; + uint32_t bytesThisPass; + uint64_t leftInFile; const tr_file * file = &tor->info.files[fileIndex]; /* if we're starting a new piece... */ @@ -101,7 +101,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag ) /* read a bit */ if( fd >= 0 ) { const ssize_t numRead = tr_pread( fd, buffer, bytesThisPass, filePos ); - if( numRead == bytesThisPass ) + if( numRead == (ssize_t)bytesThisPass ) SHA1_Update( &sha, buffer, numRead ); if( numRead > 0 ) { pieceBytesRead += numRead;