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

This commit is contained in:
Charles Kerr 2008-01-31 15:20:42 +00:00
parent b5e119f1ea
commit 375949551b
3 changed files with 34 additions and 55 deletions

View File

@ -151,7 +151,6 @@ testParse( void )
snprintf( (char*)buf, sizeof( buf ), "i64e" );
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
check( !err );
check( tr_bencIsInt( &val ) );
check( tr_bencGetInt( &val ) == 64 );
check( end == buf + 4 );
tr_bencFree( &val );
@ -159,7 +158,6 @@ testParse( void )
snprintf( (char*)buf, sizeof( buf ), "li64ei32ei16ee" );
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
check( !err );
check( tr_bencIsList( &val ) );
check( end == buf + strlen( (char*)buf ) );
check( val.val.l.count == 3 );
check( tr_bencGetInt( &val.val.l.vals[0] ) == 64 );
@ -204,11 +202,8 @@ testParse( void )
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
check( !err );
check( end == buf + strlen( (const char*)buf ) );
check( tr_bencIsList( &val ) );
check(( child = tr_bencListGetNthChild( &val, 0 )));
check( tr_bencIsList( child ) );
check(( child2 = tr_bencListGetNthChild( child, 0 )));
check( tr_bencIsDict( child2 ) );
saved = tr_bencSave( &val, &len );
check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) );
tr_free( saved );
@ -216,7 +211,7 @@ testParse( void )
end = NULL;
snprintf( (char*)buf, sizeof( buf ), "d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e" );
err = tr_bencLoad( buf, sizeof( buf ), &val, (char**)&end );
err = tr_bencParse( buf, buf+sizeof( buf ), &val, &end );
check( !err );
check( end == buf + strlen( (const char*)buf ) );
tr_bencFree( &val );
@ -245,7 +240,6 @@ testStackSmash( void )
in[depth*2] = '\0';
err = tr_bencParse( in, in+(depth*2), &val, &end );
check( !err );
check( tr_bencIsList( &val ) );
check( end == in+(depth*2) );
saved = tr_bencSave( &val, &len );
check( !strcmp( saved, (char*)in ) );

View File

@ -40,25 +40,19 @@
***
**/
int
static int
tr_bencIsInt( const benc_val_t * val )
{
return val!=NULL && val->type==TYPE_INT;
}
int
tr_bencIsString( const benc_val_t * val )
{
return val!=NULL && val->type==TYPE_STR;
}
int
static int
tr_bencIsList( const benc_val_t * val )
{
return val!=NULL && val->type==TYPE_LIST;
}
int
static int
tr_bencIsDict( const benc_val_t * val )
{
return val!=NULL && val->type==TYPE_DICT;
@ -580,33 +574,27 @@ nodeNewLeaf( const benc_val_t * val )
static struct SaveNode*
nodeNew( const benc_val_t * val )
{
switch( val->type )
{
case TYPE_INT:
case TYPE_STR:
return nodeNewLeaf( val );
break;
case TYPE_LIST:
return nodeNewList( val );
break;
case TYPE_DICT:
return nodeNewDict( val );
break;
}
struct SaveNode * node;
assert( 0 && "invalid type!" );
return NULL;
if( val->type == TYPE_LIST )
node = nodeNewList( val );
else if( val->type == TYPE_DICT )
node = nodeNewDict( val );
else
node = nodeNewLeaf( val );
return node;
}
typedef void (*BencNodeWalkFunc)( const benc_val_t * val, void * user_data );
typedef void (*BencWalkFunc)( const benc_val_t * val, void * user_data );
struct WalkFuncs
{
BencNodeWalkFunc intFunc;
BencNodeWalkFunc stringFunc;
BencNodeWalkFunc dictBeginFunc;
BencNodeWalkFunc listBeginFunc;
BencNodeWalkFunc containerEndFunc;
BencWalkFunc intFunc;
BencWalkFunc stringFunc;
BencWalkFunc dictBeginFunc;
BencWalkFunc listBeginFunc;
BencWalkFunc containerEndFunc;
};
/**
@ -672,7 +660,9 @@ bencWalk( const benc_val_t * top,
break;
default:
assert( 0 && "invalid type!" );
/* did caller give us an uninitialized val? */
tr_err( "Invalid benc type %d", val->type );
break;
}
}
@ -684,31 +674,31 @@ bencWalk( const benc_val_t * top,
****/
static void
saveIntFunc( const benc_val_t * val, void * buf )
saveIntFunc( const benc_val_t * val, void * evbuf )
{
evbuffer_add_printf( buf, "i%"PRId64"e", tr_bencGetInt(val) );
evbuffer_add_printf( evbuf, "i%"PRId64"e", tr_bencGetInt(val) );
}
static void
saveStringFunc( const benc_val_t * val, void * user_data )
saveStringFunc( const benc_val_t * val, void * vevbuf )
{
struct evbuffer * out = user_data;
evbuffer_add_printf( out, "%i:", val->val.s.i );
evbuffer_add( out, val->val.s.s, val->val.s.i );
struct evbuffer * evbuf = vevbuf;
evbuffer_add_printf( evbuf, "%i:", val->val.s.i );
evbuffer_add( evbuf, val->val.s.s, val->val.s.i );
}
static void
saveDictBeginFunc( const benc_val_t * val UNUSED, void * buf )
saveDictBeginFunc( const benc_val_t * val UNUSED, void * evbuf )
{
evbuffer_add_printf( buf, "d" );
evbuffer_add_printf( evbuf, "d" );
}
static void
saveListBeginFunc( const benc_val_t * val UNUSED, void * buf )
saveListBeginFunc( const benc_val_t * val UNUSED, void * evbuf )
{
evbuffer_add_printf( buf, "l" );
evbuffer_add_printf( evbuf, "l" );
}
static void
saveContainerEndFunc( const benc_val_t * val UNUSED, void * buf )
saveContainerEndFunc( const benc_val_t * val UNUSED, void * evbuf )
{
evbuffer_add_printf( buf, "e" );
evbuffer_add_printf( evbuf, "e" );
}
char*
tr_bencSave( const benc_val_t * top, int * len )

View File

@ -118,11 +118,6 @@ int tr_bencParseStr( const uint8_t * buf,
***
**/
int tr_bencIsInt( const benc_val_t * val );
int tr_bencIsList( const benc_val_t * val );
int tr_bencIsDict( const benc_val_t * val );
int tr_bencIsString( const benc_val_t * val );
benc_val_t* tr_bencListGetNthChild( benc_val_t * val, int i );