lt sync 3232 and patch segfault on start

This commit is contained in:
Andrew Resch 2009-02-07 00:59:56 +00:00
parent 89b5ca1cfe
commit 44d2fd1f1f
4 changed files with 95 additions and 75 deletions

View File

@ -60,7 +60,7 @@ namespace libtorrent
~bitfield() { dealloc(); }
void assign(char const* bytes, int bits)
{ resize(bits); std::memcpy(m_bytes, bytes, (bits + 7) / 8); }
{ resize(bits); std::memcpy(m_bytes, bytes, (bits + 7) / 8); clear_trailing_bits(); }
bool operator[](int index) const
{ return get_bit(index); }
@ -198,6 +198,7 @@ namespace libtorrent
if (old_size_bytes && b) m_bytes[old_size_bytes - 1] |= (0xff >> b);
if (old_size_bytes < new_size_bytes)
std::memset(m_bytes + old_size_bytes, 0xff, new_size_bytes - old_size_bytes);
clear_trailing_bits();
}
else
{
@ -209,6 +210,7 @@ namespace libtorrent
void set_all()
{
std::memset(m_bytes, 0xff, (m_size + 7) / 8);
clear_trailing_bits();
}
void clear_all()
@ -240,12 +242,17 @@ namespace libtorrent
m_own = true;
}
m_size = bits;
// clear the tail bits in the last byte
if (m_size && (bits & 7)) m_bytes[(m_size + 7) / 8 - 1] &= 0xff << (7 - (bits & 7));
clear_trailing_bits();
}
private:
void clear_trailing_bits()
{
// clear the tail bits in the last byte
if (m_size & 7) m_bytes[(m_size + 7) / 8 - 1] &= 0xff << (8 - (m_size & 7));
}
void dealloc() { if (m_own) std::free(m_bytes); m_bytes = 0; }
unsigned char* m_bytes;
int m_size; // in bits

View File

@ -287,7 +287,7 @@ public:
m_RC4_handler->encrypt(buffer, size);
#ifdef TORRENT_DEBUG
m_encrypted_bytes += size;
TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size() + size);
TORRENT_ASSERT(m_encrypted_bytes == send_buffer_size() + size);
#endif
}
#endif

View File

@ -443,6 +443,8 @@ namespace libtorrent
int pad_size = rand() % 512;
TORRENT_ASSERT(send_buffer_size() == m_encrypted_bytes);
// synchash,skeyhash,vc,crypto_provide,len(pad),pad,len(ia)
buffer::interval send_buf =
allocate_send_buffer(20 + 20 + 8 + 4 + 2 + pad_size + 2);
@ -506,7 +508,7 @@ namespace libtorrent
const int packet_size = 20 + 20 + 8 + 4 + 2 + pad_size + 2;
TORRENT_ASSERT(send_buffer_size() - packet_size == m_encrypted_bytes);
m_encrypted_bytes += packet_size;
TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size());
TORRENT_ASSERT(m_encrypted_bytes == send_buffer_size());
#endif
TORRENT_ASSERT(send_buf.begin == send_buf.end);
@ -525,6 +527,8 @@ namespace libtorrent
int pad_size =rand() % 512;
TORRENT_ASSERT(send_buffer_size() == m_encrypted_bytes);
const int buf_size = 8 + 4 + 2 + pad_size;
buffer::interval send_buf = allocate_send_buffer(buf_size);
if (send_buf.begin == 0) return; // out of memory
@ -636,7 +640,6 @@ namespace libtorrent
m_RC4_handler->encrypt(const_cast<char*>(buf), size);
#ifdef TORRENT_DEBUG
m_encrypted_bytes += size;
TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size() + size);
#endif
}
@ -680,6 +683,7 @@ namespace libtorrent
void bt_peer_connection::setup_send()
{
encrypt_pending_buffer();
TORRENT_ASSERT(!m_encrypted || !m_rc4_encrypted || m_encrypted_bytes == send_buffer_size());
peer_connection::setup_send();
}
@ -1525,7 +1529,11 @@ namespace libtorrent
if (t->is_seed())
{
memset(i.begin, 0xff, packet_size - 5);
memset(i.begin, 0xff, packet_size - 6);
// Clear trailing bits
unsigned char *p = ((unsigned char *)i.begin) + packet_size - 6;
*p = (0xff << ((8 - (num_pieces & 7)) & 7)) & 0xff;
}
else
{
@ -2670,10 +2678,17 @@ namespace libtorrent
#ifdef TORRENT_DEBUG
if (m_encrypted_bytes > 0)
{
if (m_rc4_encrypted)
{
m_encrypted_bytes -= bytes_transferred;
TORRENT_ASSERT(m_encrypted_bytes >= 0);
TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size());
}
else
{
m_encrypted_bytes -= (std::min)(int(bytes_transferred), m_encrypted_bytes);
}
TORRENT_ASSERT(m_encrypted_bytes >= 0);
}
#endif

View File

@ -2550,14 +2550,13 @@ namespace libtorrent
}
int auto_managed_ = rd.dict_find_int_value("auto_managed", -1);
if (auto_managed_ != -1) auto_managed(auto_managed_);
if (auto_managed_ != -1) m_auto_managed = auto_managed_;
int sequential_ = rd.dict_find_int_value("sequential_download", -1);
if (sequential_ != -1) set_sequential_download(sequential_);
int paused_ = rd.dict_find_int_value("paused", -1);
if (paused_ == 1) pause();
else if (paused_ == 0) resume();
if (paused_ != -1) m_paused = paused_;
lazy_entry const* trackers = rd.dict_find_list("trackers");
if (trackers)
@ -4740,4 +4739,3 @@ namespace libtorrent
#endif
}