From 5128d0a4342d85899d21703c8945958f5970fb59 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 19 Jun 2007 03:01:57 +0000 Subject: [PATCH] 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. --- libtransmission/bencode.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index bddbbdbbd..370566c8b 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -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;