Changeset 5183


Ignore:
Timestamp:
Mar 2, 2008, 7:42:45 PM (14 years ago)
Author:
charles
Message:

don't crash in makemeta when trying to build a torrent from files you don't have read access to. (wereHamster)

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode.c

    r5127 r5183  
    9696    begin = buf + 1;
    9797    end = memchr( begin, 'e', (bufend-buf)-1 );
    98     if( end == NULL )
     98    if( end == NULL ) {
     99        fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    99100        return TR_ERROR;
     101    }
    100102
    101103    errno = 0;
    102104    val = strtoll( begin, &endptr, 10 );
    103     if( errno || ( endptr != end ) ) /* incomplete parse */
     105    if( errno || ( endptr != end ) ) { /* incomplete parse */
     106        fprintf( stderr, "Unable to parse int [%*.*s]\n", (int)(end-begin), (int)(end-begin), (const char*)begin );
     107        if( errno )
     108            fprintf( stderr, "errno is %d (%s)\n", errno, strerror(errno) );
     109        fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    104110        err = TR_ERROR;
    105     else if( val && *(const char*)begin=='0' ) /* no leading zeroes! */
     111    }
     112    else if( val && *(const char*)begin=='0' ) { /* no leading zeroes! */
     113        fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    106114        err = TR_ERROR;
     115    }
    107116    else {
    108117        *setme_end = end + 1;
     
    138147
    139148    end = memchr( buf, ':', bufend-buf );
    140     if( end == NULL )
     149    if( end == NULL ) {
     150        fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    141151        return TR_ERROR;
     152    }
    142153
    143154    errno = 0;
    144155    len = strtoul( (const char*)buf, &endptr, 10 );
    145     if( errno || endptr!=end )
     156    if( errno || endptr!=end ) {
     157        fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    146158        return TR_ERROR;
    147 
    148     if( (const uint8_t*)end + 1 + len > bufend )
     159    }
     160
     161    if( (const uint8_t*)end + 1 + len > bufend ) {
     162        fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    149163        return TR_ERROR;
     164    }
    150165
    151166    *setme_end = end + 1 + len;
     
    318333tr_bencLoad( const void  * buf_in,
    319334             int           buflen,
    320              tr_benc  * setme_benc,
     335             tr_benc     * setme_benc,
    321336             char       ** setme_end )
    322337{
     
    324339    const uint8_t * end;
    325340    const int ret = tr_bencParse( buf, buf+buflen, setme_benc, &end );
     341fprintf( stderr, "tried to parse len %d\n", buflen );
    326342    if( !ret && setme_end )
    327343        *setme_end = (char*) end;
     344fprintf( stderr, "tr_bencLoad returning %d\n", ret );
    328345    return ret;
    329346}
  • trunk/libtransmission/makemeta.c

    r5127 r5183  
    205205    totalRemain = b->totalSize;
    206206    fp = fopen( b->files[fileIndex].filename, "rb" );
     207    if( !fp ) {
     208        tr_err( "Unable to open \"%s\": %s", b->files[fileIndex].filename, tr_strerror( errno ) );
     209        tr_free( ret );
     210        b->failed = 1;
     211        return NULL;
     212    }
    207213    while ( totalRemain )
    208214    {
     
    228234                if( ++fileIndex < b->fileCount ) {
    229235                    fp = fopen( b->files[fileIndex].filename, "rb" );
     236                    if( !fp ) {
     237                        tr_err( "Unable to open \"%s\": %s", b->files[fileIndex].filename, tr_strerror( errno ) );
     238                        tr_free( ret );
     239                        b->failed = 1;
     240                        return NULL;
     241                    }
    230242                }
    231243            }
     
    346358    tr_bencInitInt( val, builder->pieceSize );
    347359
    348     pch = getHashInfo( builder );
    349     val = tr_bencDictAdd( dict, "pieces" );
    350     tr_bencInitStr( val, pch, SHA_DIGEST_LENGTH * builder->pieceCount, 0 );
     360    if( ( pch = getHashInfo( builder ) ) ) {
     361        val = tr_bencDictAdd( dict, "pieces" );
     362        tr_bencInitStr( val, pch, SHA_DIGEST_LENGTH * builder->pieceCount, 0 );
     363    }
    351364
    352365    val = tr_bencDictAdd( dict, "private" );
     
    386399
    387400    /* save the file */
    388     if ( !builder->abortFlag ) {
     401    if ( !builder->failed && !builder->abortFlag ) {
    389402        size_t nmemb;
    390403        char * pch = tr_bencSave( &top, &n );
  • trunk/libtransmission/metainfo.c

    r5127 r5183  
    5555
    5656    success = parseURL( url, host, &port, &path );
    57 
    58     if( success ) {
     57    if( !success )
     58        tr_err( "Can't parse URL \"%s\"", url );
     59    else {
    5960        *setme_host = tr_strdup( host );
    6061        *setme_port = port;
     
    177178    char buf[4096];
    178179
     180fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    179181    /* info_hash: urlencoded 20-byte SHA1 hash of the value of the info key
    180182     * from the Metainfo file. Note that the value will be a bencoded
     
    186188        tr_sha1( inf->hash, str, len, NULL );
    187189        tr_free( str );
     190fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    188191    }
    189192    else
    190193    {
    191194        tr_err( "info dictionary not found!" );
    192         return TR_EINVALID;
    193     }
    194 
     195fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
     196        return TR_EINVALID;
     197    }
     198
     199fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    195200    tr_sha1_to_hex( inf->hashString, inf->hash );
    196201    savedname( inf->torrent, sizeof( inf->torrent ), inf->hashString, tag );
     202fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    197203
    198204    /* comment */
     
    201207    if( val && val->type == TYPE_STR )
    202208        strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
     209fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    203210    tr_free( inf->comment );
    204211    inf->comment = tr_strdup( buf );
    205212   
     213fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    206214    /* creator */
    207215    memset( buf, '\0', sizeof( buf ) );
     
    209217    if( val && val->type == TYPE_STR )
    210218        strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
     219fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    211220    tr_free( inf->creator );
    212221    inf->creator = tr_strdup( buf );
     
    215224    inf->dateCreated = 0;
    216225    val = tr_bencDictFind( meta, "creation date" );
     226fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    217227    if( NULL != val && TYPE_INT == val->type )
    218228    {
     229fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    219230        inf->dateCreated = val->val.i;
    220231    }
     
    223234    val  = tr_bencDictFind( beInfo, "private" );
    224235    val2 = tr_bencDictFind( meta,  "private" );
     236fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    225237    if( ( NULL != val  && ( TYPE_INT != val->type  || 0 != val->val.i ) ) ||
    226238        ( NULL != val2 && ( TYPE_INT != val2->type || 0 != val2->val.i ) ) )
    227239    {
     240fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    228241        inf->isPrivate = 1;
    229242    }
     
    231244    /* Piece length */
    232245    val = tr_bencDictFind( beInfo, "piece length" );
     246fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    233247    if( NULL == val || TYPE_INT != val->type )
    234248    {
     249fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    235250        tr_err( "%s \"piece length\" entry", ( val ? "Invalid" : "Missing" ) );
    236251        goto fail;
    237252    }
     253fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    238254    inf->pieceSize = val->val.i;
    239255
    240256    /* Hashes */
    241257    val = tr_bencDictFind( beInfo, "pieces" );
     258fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    242259    if( NULL == val || TYPE_STR != val->type )
    243260    {
     261fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    244262        tr_err( "%s \"pieces\" entry", ( val ? "Invalid" : "Missing" ) );
    245263        goto fail;
    246264    }
     265fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    247266    if( val->val.s.i % SHA_DIGEST_LENGTH )
    248267    {
     268fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    249269        tr_err( "Invalid \"piece\" string (size is %d)", val->val.s.i );
    250270        goto fail;
    251271    }
     272fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    252273    inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH;
    253274
     
    261282    /* get file or top directory name */
    262283    val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL );
     284fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    263285    if( parseFiles( inf, tr_bencDictFindFirst( beInfo,
    264286                                               "name.utf-8", "name", NULL ),
     
    266288                    tr_bencDictFind( beInfo, "length" ) ) )
    267289    {
     290fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    268291        goto fail;
    269292    }
     293fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    270294
    271295    if( !inf->fileCount )
    272296    {
     297fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    273298        tr_err( "Torrent has no files." );
    274299        goto fail;
    275300    }
     301fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    276302
    277303    if( !inf->totalSize )
    278304    {
     305fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    279306        tr_err( "Torrent is zero bytes long." );
    280307        goto fail;
    281308    }
     309fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    282310
    283311    /* TODO add more tests so we don't crash on weird files */
    284312
     313fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    285314    if( (uint64_t) inf->pieceCount !=
    286315        ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize )
    287316    {
     317fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    288318        tr_err( "Size of hashes and files don't match" );
    289319        goto fail;
    290320    }
    291321
     322fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    292323    /* get announce or announce-list */
    293324    if( getannounce( inf, meta ) )
    294325    {
     326fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    295327        goto fail;
    296328    }
    297329
     330fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    298331    return TR_OK;
    299332
     333fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
    300334  fail:
    301335    tr_metainfoFree( inf );
     
    500534
    501535        if( tr_httpParseUrl( pch, -1, &address, &port, &announce ) )
    502         {
    503             tr_err( "Invalid announce URL (%s)", val->val.s.s );
    504536            return TR_EINVALID;
    505         }
     537
    506538        sublist                   = calloc( 1, sizeof( sublist[0] ) );
    507539        sublist[0].address        = address;
  • trunk/libtransmission/torrent.c

    r5155 r5183  
    419419    memset( setmeInfo, 0, sizeof( tr_info ) );
    420420
    421     if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) )
     421    if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) ) {
     422fprintf( stderr, "%s:%d can't get metainfo\n", __FILE__, __LINE__ );
    422423        return TR_EINVALID;
     424    }
    423425
    424426    err = tr_metainfoParse( setmeInfo, metainfo, handle->tag );
     427fprintf( stderr, "%s:%d err %d\n", __FILE__, __LINE__, err );
    425428    doFree = !err && ( setmeInfo == &tmp );
    426429
    427430    if( !err && hashExists( handle, setmeInfo->hash ) ) {
    428431        err = TR_EDUPLICATE;
     432fprintf( stderr, "%s:%d err %d\n", __FILE__, __LINE__, err );
    429433        doFree = 1;
    430434    }
     
    433437        tr_metainfoFree( setmeInfo );
    434438
     439fprintf( stderr, "%s:%d err %d\n", __FILE__, __LINE__, err );
    435440    return err;
    436441}
     
    446451
    447452    err = tr_torrentParse( handle, ctor, &tmpInfo );
     453fprintf( stderr, "%s:%d, err %d\n", __FILE__, __LINE__, err );
    448454    if( !err ) {
    449455        tor = tr_new0( tr_torrent, 1 );
    450456        tor->info = tmpInfo;
    451457        torrentRealInit( handle, tor, ctor );
     458fprintf( stderr, "%s:%d, tor %p\n", __FILE__, __LINE__, tor );
    452459    } else if( setmeError ) {
     460fprintf( stderr, "err is %d\n", err );
    453461        *setmeError = err;
    454462    }
    455463
     464fprintf( stderr, "returning torrent %p\n", tor );
    456465    return tor;
    457466}
Note: See TracChangeset for help on using the changeset viewer.