Changeset 1141

Show
Ignore:
Timestamp:
03/06/10 11:35:37 (2 years ago)
Author:
rakshasa
Message:

* Fixed a bug in reading bencoded data. #2112

Location:
trunk/libtorrent
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/libtorrent/src/protocol/extensions.cc

    r1140 r1141  
    4646#include "protocol/peer_connection_base.h" 
    4747#include "torrent/connection_manager.h" 
    48 #include "torrent/object.h" 
    4948#include "torrent/object_stream.h" 
    5049#include "torrent/peer/connection_list.h" 
    5150#include "torrent/peer/peer_info.h" 
    52 #include "torrent/object_static_map.h" 
    5351#include "manager.h" 
    5452 
     
    5654 
    5755namespace torrent { 
    58  
    59 enum ext_handshake_keys { 
    60   key_e, 
    61   key_m_utPex, 
    62   key_p, 
    63   key_reqq, 
    64   key_v, 
    65   key_handshake_LAST 
    66 }; 
    67  
    68 enum ext_pex_keys { 
    69   key_pex_added, 
    70   key_pex_LAST 
    71 }; 
    72  
    73 typedef static_map_type<ext_handshake_keys, key_handshake_LAST> ExtHandshakeMessage; 
    74 typedef static_map_type<ext_pex_keys, key_pex_LAST> ExtPEXMessage; 
    7556 
    7657template <> 
  • trunk/libtorrent/src/protocol/extensions.h

    r1138 r1141  
    4141#include <vector> 
    4242 
    43 #include <torrent/exceptions.h> 
    44 #include <torrent/object.h> 
     43#include "torrent/exceptions.h" 
     44#include "torrent/object.h" 
     45#include "torrent/object_static_map.h" 
    4546 
    4647#include "download/download_info.h" 
     
    156157  char*               m_readPos; 
    157158}; 
     159 
     160// 
     161//  
     162// 
     163 
     164enum ext_handshake_keys { 
     165  key_e, 
     166  key_m_utPex, 
     167  key_p, 
     168  key_reqq, 
     169  key_v, 
     170  key_handshake_LAST 
     171}; 
     172 
     173enum ext_pex_keys { 
     174  key_pex_added, 
     175  key_pex_LAST 
     176}; 
     177 
     178typedef static_map_type<ext_handshake_keys, key_handshake_LAST> ExtHandshakeMessage; 
     179typedef static_map_type<ext_pex_keys, key_pex_LAST> ExtPEXMessage; 
     180 
     181// 
     182// 
     183// 
    158184 
    159185inline 
  • trunk/libtorrent/src/torrent/object_raw_bencode.h

    r1138 r1141  
    3838#define LIBTORRENT_OBJECT_RAW_BENCODE_H 
    3939 
     40#include <algorithm> 
    4041#include <string> 
    4142#include <cstring> 
  • trunk/libtorrent/src/torrent/object_stream.cc

    r1140 r1141  
    3939#include <iterator> 
    4040#include <iostream> 
     41#include <cmath> 
     42#include <limits> 
    4143#include <rak/algorithm.h> 
    4244#include <rak/functional.h> 
     
    212214    first = object_read_bencode_c_value(first + 1, last, object->as_value()); 
    213215 
    214     if (first == NULL || first == last || *first++ != 'e') 
     216    if (first == last || *first++ != 'e') 
    215217      break; 
    216218 
     
    224226    *object = Object::create_list(); 
    225227 
    226     while (first != NULL && first != last) { 
     228    while (first != last) { 
    227229      if (*first == 'e') 
    228230        return first + 1; 
     
    249251    Object::string_type prev; 
    250252 
    251     while (first != NULL && first != last) { 
     253    while (first != last) { 
    252254      if (*first == 'e') 
    253255        return first + 1; 
     
    348350  first = object_read_bencode_skip_c(first, last); 
    349351 
    350   if (first == NULL) 
    351     return first; 
    352  
    353352  raw_bencode obj = raw_bencode(tmp, std::distance(tmp, first)); 
    354353 
    355   if (obj.is_empty()) 
    356     return NULL; 
     354//   if (obj.is_empty()) 
     355//     throw torrent::bencode_error("Invalid bencode data."); 
    357356 
    358357  switch (type) { 
     
    644643//       sprintf(buffer, "Verified wrong, %u, '%u', '%s'.", std::distance(first, last), (unsigned int)*first, escaped.c_str()); 
    645644 
    646 //       throw torrent::bencode_error("Invalid bencode data."); 
     645//       throw torrent::internal_error("Invalid bencode data."); 
    647646//     } 
    648647//   } 
     
    657656  char current_key[static_map_mapping_type::max_key_size + 2] = ""; 
    658657 
    659   while (first != last && first != NULL) { 
     658  while (first != last) { 
    660659    // End a dictionary/list or the whole stream. 
    661660    if (*first == 'e') { 
     
    666665 
    667666      stack_itr--; 
     667      continue; 
    668668    } 
    669669 
     
    717717      // The bencode object isn't a list. This should either skip it 
    718718      // or produce an error. 
    719       if (*first++ != 'd') 
    720         throw torrent::bencode_error("Invalid bencode data."); 
     719      if (*first++ != 'd') { 
     720        first = object_read_bencode_skip_c(first - 1, last); 
     721        break; 
     722      } 
    721723 
    722724      stack_itr++; 
     
    734736      // The bencode object isn't a list. This should either skip it 
    735737      // or produce an error. 
    736       if (*first++ != 'l') 
    737         throw torrent::bencode_error("Invalid bencode data."); 
     738      if (*first++ != 'l') { 
     739        first = object_read_bencode_skip_c(first - 1, last); 
     740        break; 
     741      } 
    738742 
    739743      first_key = key_search.first; 
     
    746750 
    747751        if (first_key->key[key_search.second + 2] == '*') { 
    748           if ((first = object_read_bencode_raw_c(first, last, 
    749                                                  &entry_values[first_key->index].object, 
    750                                                  key_search.first->key[key_search.second + 1])) == NULL) 
    751             break; 
     752          first = object_read_bencode_raw_c(first, last, 
     753                                            &entry_values[first_key->index].object, 
     754                                            key_search.first->key[key_search.second + 1]); 
    752755        } else { 
    753           if ((first = object_read_bencode_c(first, last, &entry_values[first_key->index].object)) == NULL) 
    754             break; 
     756          first = object_read_bencode_c(first, last, &entry_values[first_key->index].object); 
    755757        } 
    756758 
  • trunk/libtorrent/test/Makefile.am

    r1137 r1141  
    11TESTS = LibTorrentTest 
    22check_PROGRAMS = $(TESTS) 
     3LibTorrentTest_LDADD = \ 
     4        ../src/libtorrent.la 
     5 
    36LibTorrentTest_SOURCES = \ 
    47        rak/allocators_test.cc \ 
     
    1417        main.cc 
    1518 
    16 LibTorrentTest_LDADD = \ 
    17         ../src/libtorrent.la 
    18  
    1919LibTorrentTest_CXXFLAGS = $(CPPUNIT_CFLAGS) 
    2020LibTorrentTest_LDFLAGS = $(CPPUNIT_LIBS)  -ldl 
  • trunk/libtorrent/test/torrent/object_static_map_test.cc

    r1140 r1141  
    44#include <torrent/object_stream.h> 
    55#include "torrent/object_static_map.h" 
     6#include "protocol/extensions.h" 
    67 
    78#import "object_test_utils.h" 
     
    150151 
    151152   CPPUNIT_ASSERT(static_map_read_bencode(test_skip_map, test_read_skip_bencode)); 
     153} 
     154 
     155template <> 
     156const torrent::ExtHandshakeMessage::key_list_type torrent::ExtHandshakeMessage::keys = { 
     157  { key_e,            "e" }, 
     158  { key_m_utPex,      "m::ut_pex" }, 
     159  { key_p,            "p" }, 
     160  { key_reqq,         "reqq" }, 
     161  { key_v,            "v" }, 
     162}; 
     163 
     164void 
     165ObjectStaticMapTest::test_read_extensions() { 
     166  torrent::ExtHandshakeMessage test_ext; 
     167 
     168  CPPUNIT_ASSERT(static_map_read_bencode(test_ext, "d1:ai1ee")); 
     169 
     170  CPPUNIT_ASSERT(static_map_read_bencode(test_ext, "d1:mi1ee")); 
     171  CPPUNIT_ASSERT(static_map_read_bencode(test_ext, "d1:mdee")); 
     172  CPPUNIT_ASSERT(static_map_read_bencode(test_ext, "d6:ut_pexi0ee")); 
     173  CPPUNIT_ASSERT(static_map_read_bencode(test_ext, "d1:md6:ut_pexi0eee")); 
    152174} 
    153175 
     
    176198enum keys_raw_types { key_raw_types_empty, key_raw_types_list, key_raw_types_map, key_raw_types_str, key_raw_types_LAST}; 
    177199enum keys_multiple { key_multiple_a, key_multiple_b, key_multiple_c, key_multiple_LAST }; 
     200enum keys_dict { key_dict_a_b, key_dict_LAST }; 
    178201 
    179202typedef torrent::static_map_type<keys_empty, key_empty_LAST> test_empty_type; 
     
    182205typedef torrent::static_map_type<keys_raw_types, key_raw_types_LAST> test_raw_types_type; 
    183206typedef torrent::static_map_type<keys_multiple, key_multiple_LAST> test_multiple_type; 
     207typedef torrent::static_map_type<keys_dict, key_dict_LAST> test_dict_type; 
    184208 
    185209template <> const test_empty_type::key_list_type 
     
    196220template <> const test_multiple_type::key_list_type 
    197221test_multiple_type::keys = { { key_multiple_a, "a" }, { key_multiple_b, "b*" }, { key_multiple_c, "c" } }; 
     222template <> const test_dict_type::key_list_type 
     223test_dict_type::keys = { { key_dict_a_b, "a::b" } }; 
    198224 
    199225void 
     
    290316 
    291317void 
     318ObjectStaticMapTest::test_read_dict() { 
     319  test_dict_type map_normal; 
     320 
     321  CPPUNIT_ASSERT(static_map_read_bencode(map_normal, "d1:ai1ee")); 
     322  CPPUNIT_ASSERT(map_normal[key_dict_a_b].is_empty()); 
     323 
     324  CPPUNIT_ASSERT(static_map_read_bencode(map_normal, "d1:adee")); 
     325  CPPUNIT_ASSERT(map_normal[key_dict_a_b].is_empty()); 
     326 
     327  CPPUNIT_ASSERT(static_map_read_bencode(map_normal, "d1:ad1:bi1eee")); 
     328  CPPUNIT_ASSERT(map_normal[key_dict_a_b].as_value() == 1); 
     329} 
     330 
     331void 
    292332ObjectStaticMapTest::test_write_empty() { 
    293333  test_empty_type map_normal; 
  • trunk/libtorrent/test/torrent/object_static_map_test.h

    r1138 r1141  
    88  CPPUNIT_TEST(test_write); 
    99  CPPUNIT_TEST(test_read); 
     10  CPPUNIT_TEST(test_read_extensions); 
    1011 
    1112  CPPUNIT_TEST(test_read_empty); 
     
    1415  CPPUNIT_TEST(test_read_raw_types); 
    1516  CPPUNIT_TEST(test_read_multiple); 
     17  CPPUNIT_TEST(test_read_dict); 
    1618 
    1719  CPPUNIT_TEST(test_write_empty); 
     
    2931  void test_read(); 
    3032 
     33  void test_read_extensions(); 
     34 
    3135  // Proper unit tests: 
    3236  void test_read_empty(); 
     
    3539  void test_read_raw_types(); 
    3640  void test_read_multiple(); 
     41  void test_read_dict(); 
    3742 
    3843  void test_write_empty(); 
  • trunk/libtorrent/test/torrent/object_stream_test.cc

    r1140 r1141  
    125125  CPPUNIT_ASSERT(object_stream_read_skip("d1:ali1eee")); 
    126126  CPPUNIT_ASSERT(object_stream_read_skip("d1:ad1:bi1eee")); 
     127 
     128  CPPUNIT_ASSERT(object_stream_read_skip("d1:md6:ut_pexi0eee")); 
    127129} 
    128130