diff options
author | Dirk Engling <erdgeist@erdgeist.org> | 2024-03-07 03:29:46 +0100 |
---|---|---|
committer | Dirk Engling <erdgeist@erdgeist.org> | 2024-03-07 03:29:46 +0100 |
commit | 9c98e1e775c48684442fe97ca93bfa71b295d81e (patch) | |
tree | e17be9c2ef30da5b71042fec07e49ef26658fac5 | |
parent | cc24e1ab2d571402fe352a24898d5a103e28b844 (diff) |
Remember hash and not torrent pointer for use after releasing the lock on torrent vector
-rw-r--r-- | ot_stats.c | 28 |
1 files changed, 16 insertions, 12 deletions
@@ -293,7 +293,7 @@ static int torrent_statter( ot_torrent *torrent, uintptr_t data ) { | |||
293 | /* Converter function from memory to human readable hex strings */ | 293 | /* Converter function from memory to human readable hex strings */ |
294 | static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;} | 294 | static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;} |
295 | 295 | ||
296 | typedef struct { size_t val; ot_torrent * torrent; } ot_record; | 296 | typedef struct { size_t val; ot_hash hash; } ot_record; |
297 | 297 | ||
298 | /* Fetches stats from tracker */ | 298 | /* Fetches stats from tracker */ |
299 | size_t stats_top_txt( char * reply, int amount ) { | 299 | size_t stats_top_txt( char * reply, int amount ) { |
@@ -311,18 +311,22 @@ size_t stats_top_txt( char * reply, int amount ) { | |||
311 | for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { | 311 | for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { |
312 | ot_vector *torrents_list = mutex_bucket_lock( bucket ); | 312 | ot_vector *torrents_list = mutex_bucket_lock( bucket ); |
313 | for( j=0; j<torrents_list->size; ++j ) { | 313 | for( j=0; j<torrents_list->size; ++j ) { |
314 | ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; | 314 | ot_torrent *torrent = (ot_torrent*)(torrents_list->data) + j; |
315 | int idx = amount - 1; while( (idx >= 0) && ( peer_list->peer_count > top100c[idx].val ) ) --idx; | 315 | idx = amount - 1; |
316 | while( (idx >= 0) && ( torrent->peer_list->peer_count > top100c[idx].val ) ) | ||
317 | --idx; | ||
316 | if ( idx++ != amount - 1 ) { | 318 | if ( idx++ != amount - 1 ) { |
317 | memmove( top100c + idx + 1, top100c + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); | 319 | memmove( top100c + idx + 1, top100c + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); |
318 | top100c[idx].val = peer_list->peer_count; | 320 | memcpy( &top100c[idx].hash, &torrent->hash, sizeof(ot_hash)); |
319 | top100c[idx].torrent = (ot_torrent*)(torrents_list->data) + j; | 321 | top100c[idx].val = torrent->peer_list->peer_count; |
320 | } | 322 | } |
321 | idx = amount - 1; while( (idx >= 0) && ( peer_list->seed_count > top100s[idx].val ) ) --idx; | 323 | idx = amount - 1; |
324 | while( (idx >= 0) && ( torrent->peer_list->seed_count > top100s[idx].val ) ) | ||
325 | --idx; | ||
322 | if ( idx++ != amount - 1 ) { | 326 | if ( idx++ != amount - 1 ) { |
323 | memmove( top100s + idx + 1, top100s + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); | 327 | memmove( top100s + idx + 1, top100s + idx, ( amount - 1 - idx ) * sizeof( ot_record ) ); |
324 | top100s[idx].val = peer_list->seed_count; | 328 | memcpy( &top100s[idx].hash, &torrent->hash, sizeof(ot_hash)); |
325 | top100s[idx].torrent = (ot_torrent*)(torrents_list->data) + j; | 329 | top100s[idx].val = torrent->peer_list->seed_count; |
326 | } | 330 | } |
327 | } | 331 | } |
328 | mutex_bucket_unlock( bucket, 0 ); | 332 | mutex_bucket_unlock( bucket, 0 ); |
@@ -332,12 +336,12 @@ size_t stats_top_txt( char * reply, int amount ) { | |||
332 | 336 | ||
333 | r += sprintf( r, "Top %d torrents by peers:\n", amount ); | 337 | r += sprintf( r, "Top %d torrents by peers:\n", amount ); |
334 | for( idx=0; idx<amount; ++idx ) | 338 | for( idx=0; idx<amount; ++idx ) |
335 | if( top100c[idx].torrent ) | 339 | if( top100c[idx].val ) |
336 | r += sprintf( r, "\t%zd\t%s\n", top100c[idx].val, to_hex( hex_out, top100c[idx].torrent->hash) ); | 340 | r += sprintf( r, "\t%zd\t%s\n", top100c[idx].val, to_hex( hex_out, top100c[idx].hash) ); |
337 | r += sprintf( r, "Top %d torrents by seeds:\n", amount ); | 341 | r += sprintf( r, "Top %d torrents by seeds:\n", amount ); |
338 | for( idx=0; idx<amount; ++idx ) | 342 | for( idx=0; idx<amount; ++idx ) |
339 | if( top100s[idx].torrent ) | 343 | if( top100s[idx].val ) |
340 | r += sprintf( r, "\t%zd\t%s\n", top100s[idx].val, to_hex( hex_out, top100s[idx].torrent->hash) ); | 344 | r += sprintf( r, "\t%zd\t%s\n", top100s[idx].val, to_hex( hex_out, top100s[idx].hash) ); |
341 | 345 | ||
342 | return r - reply; | 346 | return r - reply; |
343 | } | 347 | } |