Changeset 12932


Ignore:
Timestamp:
Sep 28, 2011, 4:07:35 PM (11 years ago)
Author:
jordan
Message:

#4506 'crash from memory corruption somewhere called from tr_handshakeDone()' -- possible fix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bitfield.c

    r12927 r12932  
    171171    assert( b->bit_count > 0 );
    172172
    173     if( b->alloc_count )
     173    if( b->alloc_count ) {
     174        assert( b->alloc_count <= n );
    174175        memcpy( bits, b->bits, b->alloc_count );
    175     else if( tr_bitfieldHasAll( b ) )
     176    } else if( tr_bitfieldHasAll( b ) ) {
    176177        set_all_true( bits, b->bit_count );
     178    }
    177179
    178180    *byte_count = n;
     
    181183
    182184static void
    183 tr_bitfieldEnsureBitsAlloced( tr_bitfield * b, size_t nth )
     185tr_bitfieldEnsureBitsAlloced( tr_bitfield * b, size_t n )
    184186{
    185187    size_t bytes_needed;
     
    187189
    188190    if( has_all )
    189         bytes_needed = get_bytes_needed( MAX( nth, b->true_count ) + 1 );
     191        bytes_needed = get_bytes_needed( MAX( n, b->true_count ) );
    190192    else
    191         bytes_needed = get_bytes_needed( nth + 1 );
     193        bytes_needed = get_bytes_needed( n );
    192194
    193195    if( b->alloc_count < bytes_needed )
     
    200202            set_all_true( b->bits, b->true_count );
    201203    }
     204}
     205
     206static void
     207tr_bitfieldEnsureNthBitAlloced( tr_bitfield * b, size_t nth )
     208{
     209    /* count is zero-based, so we need to allocate nth+1 bits before setting the nth */
     210    tr_bitfieldEnsureBitsAlloced( b, nth + 1 );
    202211}
    203212
     
    333342    if( !tr_bitfieldHas( b, nth ) )
    334343    {
    335         tr_bitfieldEnsureBitsAlloced( b, nth );
     344        tr_bitfieldEnsureNthBitAlloced( b, nth );
    336345        b->bits[nth >> 3u] |= ( 0x80 >> ( nth & 7u ) );
    337346        tr_bitfieldIncTrueCount( b, 1 );
     
    359368    em = 0xff << ( 7 - ( end & 7 ) );
    360369
    361     tr_bitfieldEnsureBitsAlloced( b, end );
     370    tr_bitfieldEnsureNthBitAlloced( b, end );
    362371    if( sb == eb )
    363372    {
     
    382391    if( !tr_bitfieldHas( b, nth ) )
    383392    {
    384         tr_bitfieldEnsureBitsAlloced( b, nth );
     393        tr_bitfieldEnsureNthBitAlloced( b, nth );
    385394        b->bits[nth >> 3u] &= ( 0xff7f >> ( nth & 7u ) );
    386395        tr_bitfieldIncTrueCount( b, -1 );
     
    409418    em = ~( 0xff << ( 7 - ( end & 7 ) ) );
    410419
    411     tr_bitfieldEnsureBitsAlloced( b, end );
     420    tr_bitfieldEnsureNthBitAlloced( b, end );
    412421    if( sb == eb )
    413422    {
Note: See TracChangeset for help on using the changeset viewer.