fix terribly nasty bencode corruption bug from r2024 regarding the sorting of dictionary keys in tr_bencDictAdd(). this corrupts all the pointers that are already floating out there to existing values in the dictionary... if BitTornado really needs these to be sorted, then we should sort a temporary copy in tr_bencSave(), not here in our internal structures.

This commit is contained in:
Charles Kerr 2007-06-19 03:01:57 +00:00
parent 64ce2edbfb
commit 5128d0a434
1 changed files with 3 additions and 15 deletions

View File

@ -365,26 +365,14 @@ benc_val_t * tr_bencListAdd( benc_val_t * list )
benc_val_t * tr_bencDictAdd( benc_val_t * dict, const char * key )
{
benc_val_t * keyval, * itemval;
int i;
assert( TYPE_DICT == dict->type );
assert( dict->val.l.count + 2 <= dict->val.l.alloc );
/* Keep dictionaries sorted by keys alphabetically.
BitTornado-based clients (and maybe others) need this. */
for( i = 0; i < dict->val.l.count; i += 2 )
{
assert( TYPE_STR == dict->val.l.vals[i].type );
if( strcmp( key, dict->val.l.vals[i].val.s.s ) < 0 )
break;
}
memmove( &dict->val.l.vals[i+2], &dict->val.l.vals[i],
( dict->val.l.count - i ) * sizeof(benc_val_t) );
keyval = &dict->val.l.vals[i];
itemval = &dict->val.l.vals[i+1];
dict->val.l.count += 2;
keyval = dict->val.l.vals + dict->val.l.count++;
tr_bencInitStr( keyval, key, -1, 1 );
itemval = dict->val.l.vals + dict->val.l.count++;
tr_bencInit( itemval, TYPE_INT );
return itemval;