summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index c69da43..85e8156 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -75,10 +75,10 @@ static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_
75 if( !new_data ) return NULL; 75 if( !new_data ) return NULL;
76 76
77 // Adjust pointer if it moved by realloc 77 // Adjust pointer if it moved by realloc
78 match = match - (ot_byte*)vector->data + new_data; 78 match = new_data + (match - (ot_byte*)vector->data);
79 79
80 vector->data = new_data; 80 vector->data = new_data;
81 vector->space = new_space;; 81 vector->space = new_space;
82 } 82 }
83 MEMMOVE( match + member_size, match, ((ot_byte*)vector->data) + member_size * vector->size - match ); 83 MEMMOVE( match + member_size, match, ((ot_byte*)vector->data) + member_size * vector->size - match );
84 vector->size++; 84 vector->size++;
@@ -94,11 +94,11 @@ static int vector_remove_peer( ot_vector *vector, ot_peer *peer ) {
94 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch ); 94 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch );
95 95
96 if( !exactmatch ) return 0; 96 if( !exactmatch ) return 0;
97 exactmatch = OT_FLAG( match ) & PEER_FLAG_SEEDING ? 2 : 1; 97 exactmatch = ( OT_FLAG( match ) & PEER_FLAG_SEEDING ) ? 2 : 1;
98 MEMMOVE( match, match + 1, end - match - 1 ); 98 MEMMOVE( match, match + 1, sizeof(ot_peer) * ( end - match - 1 ) );
99 if( ( --vector->size * OT_VECTOR_SHRINK_THRESH < vector->space ) && ( vector->space > OT_VECTOR_MIN_MEMBERS ) ) { 99 if( ( --vector->size * OT_VECTOR_SHRINK_THRESH < vector->space ) && ( vector->space > OT_VECTOR_MIN_MEMBERS ) ) {
100 vector->space /= OT_VECTOR_SHRINK_RATIO; 100 vector->space /= OT_VECTOR_SHRINK_RATIO;
101 realloc( vector->data, vector->space * sizeof( ot_peer ) ); 101 vector->data = realloc( vector->data, vector->space * sizeof( ot_peer ) );
102 } 102 }
103 return exactmatch; 103 return exactmatch;
104} 104}
@@ -120,11 +120,15 @@ static int vector_remove_torrent( ot_vector *vector, ot_hash *hash ) {
120 match = BINARY_FIND( hash, vector->data, vector->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); 120 match = BINARY_FIND( hash, vector->data, vector->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch );
121 121
122 if( !exactmatch ) return 0; 122 if( !exactmatch ) return 0;
123 free_peerlist( match->peer_list ); 123
124 MEMMOVE( match, match + 1, end - match - 1 ); 124 // If this is being called after a unsuccessful malloc() for peer_list
125 // in add_peer_to_torrent, match->peer_list actually might be NULL
126 if( match->peer_list) free_peerlist( match->peer_list );
127
128 MEMMOVE( match, match + 1, sizeof(ot_torrent) * ( end - match - 1 ) );
125 if( ( --vector->size * OT_VECTOR_SHRINK_THRESH < vector->space ) && ( vector->space > OT_VECTOR_MIN_MEMBERS ) ) { 129 if( ( --vector->size * OT_VECTOR_SHRINK_THRESH < vector->space ) && ( vector->space > OT_VECTOR_MIN_MEMBERS ) ) {
126 vector->space /= OT_VECTOR_SHRINK_RATIO; 130 vector->space /= OT_VECTOR_SHRINK_RATIO;
127 realloc( vector->data, vector->space * sizeof( ot_torrent ) ); 131 vector->data = realloc( vector->data, vector->space * sizeof( ot_torrent ) );
128 } 132 }
129 return 1; 133 return 1;
130} 134}
@@ -175,12 +179,13 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
175 179
176 if( !exactmatch ) { 180 if( !exactmatch ) {
177 // Create a new torrent entry, then 181 // Create a new torrent entry, then
182 MEMMOVE( &torrent->hash, hash, sizeof( ot_hash ) );
183
178 torrent->peer_list = malloc( sizeof (ot_peerlist) ); 184 torrent->peer_list = malloc( sizeof (ot_peerlist) );
179 if( !torrent->peer_list ) { 185 if( !torrent->peer_list ) {
180 vector_remove_torrent( torrents_list, hash ); 186 vector_remove_torrent( torrents_list, hash );
181 return NULL; 187 return NULL;
182 } 188 }
183 MEMMOVE( &torrent->hash, hash, sizeof( ot_hash ) );
184 189
185 byte_zero( torrent->peer_list, sizeof( ot_peerlist )); 190 byte_zero( torrent->peer_list, sizeof( ot_peerlist ));
186 torrent->peer_list->base = NOW; 191 torrent->peer_list->base = NOW;