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

* Fixed a bug in reading bencoded data. #2112

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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