Changeset 4883


Ignore:
Timestamp:
Jan 31, 2008, 3:20:44 PM (14 years ago)
Author:
charles
Message:

(1.0x) more graceful handling of bad inputs to tr_bencFree(), tr_bencSave(), and tr_bencPrint().

Location:
branches/1.0x/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/1.0x/libtransmission/bencode-test.c

    r4877 r4883  
    152152    err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
    153153    check( !err );
    154     check( tr_bencIsInt( &val ) );
    155154    check( tr_bencGetInt( &val ) == 64 );
    156155    check( end == buf + 4 );
     
    160159    err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
    161160    check( !err );
    162     check( tr_bencIsList( &val ) );
    163161    check( end == buf + strlen( (char*)buf ) );
    164162    check( val.val.l.count == 3 );
     
    205203    check( !err );
    206204    check( end == buf + strlen( (const char*)buf ) );
    207     check( tr_bencIsList( &val ) );
    208205    check(( child = tr_bencListGetNthChild( &val, 0 )));
    209     check( tr_bencIsList( child ) );
    210206    check(( child2 = tr_bencListGetNthChild( child, 0 )));
    211     check( tr_bencIsDict( child2 ) );
    212207    saved = tr_bencSave( &val, &len );
    213208    check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) );
     
    217212    end = NULL;
    218213    snprintf( (char*)buf, sizeof( buf ), "d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e" );
    219     err = tr_bencLoad( buf, sizeof( buf ), &val, (char**)&end );
     214    err = tr_bencParse( buf, buf+sizeof( buf ), &val, &end );
    220215    check( !err );
    221216    check( end == buf + strlen( (const char*)buf ) );
     
    246241    err = tr_bencParse( in, in+(depth*2), &val, &end );
    247242    check( !err );
    248     check( tr_bencIsList( &val ) );
    249243    check( end == in+(depth*2) );
    250244    saved = tr_bencSave( &val, &len );
  • branches/1.0x/libtransmission/bencode.c

    r4880 r4883  
    4141**/
    4242
    43 int
     43static int
    4444tr_bencIsInt( const benc_val_t * val )
    4545{
     
    4747}
    4848
    49 int
    50 tr_bencIsString( const benc_val_t * val )
    51 {
    52     return val!=NULL && val->type==TYPE_STR;
    53 }
    54 
    55 int
     49static int
    5650tr_bencIsList( const benc_val_t * val )
    5751{
     
    5953}
    6054
    61 int
     55static int
    6256tr_bencIsDict( const benc_val_t * val )
    6357{
     
    581575nodeNew( const benc_val_t * val )
    582576{
    583     switch( val->type )
    584     {
    585         case TYPE_INT:
    586         case TYPE_STR:
    587             return nodeNewLeaf( val );
    588             break;
    589         case TYPE_LIST:
    590             return nodeNewList( val );
    591             break;
    592         case TYPE_DICT:
    593             return nodeNewDict( val );
    594             break;
    595     }
    596 
    597     assert( 0 && "invalid type!" );
    598     return NULL;
    599 }
    600 
    601 typedef void (*BencNodeWalkFunc)( const benc_val_t * val, void * user_data );
     577    struct SaveNode * node;
     578
     579    if( val->type == TYPE_LIST )
     580        node = nodeNewList( val );
     581    else if( val->type == TYPE_DICT )
     582        node = nodeNewDict( val );
     583    else
     584        node = nodeNewLeaf( val );
     585
     586    return node;
     587}
     588
     589typedef void (*BencWalkFunc)( const benc_val_t * val, void * user_data );
    602590
    603591struct WalkFuncs
    604592{
    605     BencNodeWalkFunc intFunc;
    606     BencNodeWalkFunc stringFunc;
    607     BencNodeWalkFunc dictBeginFunc;
    608     BencNodeWalkFunc listBeginFunc;
    609     BencNodeWalkFunc containerEndFunc;
     593    BencWalkFunc intFunc;
     594    BencWalkFunc stringFunc;
     595    BencWalkFunc dictBeginFunc;
     596    BencWalkFunc listBeginFunc;
     597    BencWalkFunc containerEndFunc;
    610598};
    611599
     
    673661
    674662            default:
    675                 assert( 0 && "invalid type!" );
     663                /* did caller give us an uninitialized val? */
     664                tr_err( "Invalid benc type %d", val->type );
     665                break;
    676666        }
    677667    }
     
    685675
    686676static void
    687 saveIntFunc( const benc_val_t * val, void * buf )
    688 {
    689     evbuffer_add_printf( buf, "i%"PRId64"e", tr_bencGetInt(val) );
    690 }
    691 static void
    692 saveStringFunc( const benc_val_t * val, void * user_data )
    693 {
    694     struct evbuffer * out = user_data;
    695     evbuffer_add_printf( out, "%i:", val->val.s.i );
    696     evbuffer_add( out, val->val.s.s, val->val.s.i );
    697 }
    698 static void
    699 saveDictBeginFunc( const benc_val_t * val UNUSED, void * buf )
    700 {
    701     evbuffer_add_printf( buf, "d" );
    702 }
    703 static void
    704 saveListBeginFunc( const benc_val_t * val UNUSED, void * buf )
    705 {
    706     evbuffer_add_printf( buf, "l" );
    707 }
    708 static void
    709 saveContainerEndFunc( const benc_val_t * val UNUSED, void * buf )
    710 {
    711     evbuffer_add_printf( buf, "e" );
     677saveIntFunc( const benc_val_t * val, void * evbuf )
     678{
     679    evbuffer_add_printf( evbuf, "i%"PRId64"e", tr_bencGetInt(val) );
     680}
     681static void
     682saveStringFunc( const benc_val_t * val, void * vevbuf )
     683{
     684    struct evbuffer * evbuf = vevbuf;
     685    evbuffer_add_printf( evbuf, "%i:", val->val.s.i );
     686    evbuffer_add( evbuf, val->val.s.s, val->val.s.i );
     687}
     688static void
     689saveDictBeginFunc( const benc_val_t * val UNUSED, void * evbuf )
     690{
     691    evbuffer_add_printf( evbuf, "d" );
     692}
     693static void
     694saveListBeginFunc( const benc_val_t * val UNUSED, void * evbuf )
     695{
     696    evbuffer_add_printf( evbuf, "l" );
     697}
     698static void
     699saveContainerEndFunc( const benc_val_t * val UNUSED, void * evbuf )
     700{
     701    evbuffer_add_printf( evbuf, "e" );
    712702}
    713703char*
  • branches/1.0x/libtransmission/bencode.h

    r4875 r4883  
    119119**/
    120120
    121 int tr_bencIsInt( const benc_val_t * val );
    122 int tr_bencIsList( const benc_val_t * val );
    123 int tr_bencIsDict( const benc_val_t * val );
    124 int tr_bencIsString( const benc_val_t * val );
    125 
    126121benc_val_t* tr_bencListGetNthChild( benc_val_t * val, int i );
    127122
Note: See TracChangeset for help on using the changeset viewer.