BEncode::DecodeError: invalid string length

Bug #221218 reported by Arsecroft
4
Affects Status Importance Assigned to Milestone
Ruby bencode implementation
Fix Committed
Undecided
Unassigned

Bug Description

EDIT: I think it may have something to do with the way the file's encoded?

I added "puts str.length" before the begin statement, and the number is wildly different from what is expected.

length = 27880
str.length = 2000

When I load up the file (File.open(filename).read) it seems there are escaped chars and I guess ruby unescapes them?

Also, when I use ruby to rewrite the file, it gets much much smaller. 2kb down from 28kb.

--

Every torrent file I've tried to read gives me the same error, "BEncode::DecodeError: invalid string length."

I've tried the torrents in several torrent clients, but they all work.

After editing bdecode.rb and adding two lines to BEncode::parse

       begin
         scanner.pos += length
       rescue RangeError
           puts "scanner.pos = " + scanner.pos.to_s
           puts "length = " + length.to_s
         raise BEncode::DecodeError, "invalid string length"
       end

I get the following output:

irb(main):001:0> require 'bdecode.rb'
=> true
irb(main):002:0> BEncode::load_file("ubuntu-7.10-desktop-amd64.iso.torrent")
scanner.pos = 223
length = 27880
BEncode::DecodeError: invalid string length
       from ./bdecode.rb:62:in `parse'
       from ./bdecode.rb:43:in `parse'
       from ./bdecode.rb:15:in `load'
       from ./bdecode.rb:22:in `load_file'
       from (irb):2
irb(main):003:0> File.open("ubuntu-7.10-desktop-amd64.iso.torrent").stat.size
=> 28105

here's the download link to the torrent file (It's also attatched):
http://releases.ubuntu.com/7.10/ubuntu-7.10-desktop-amd64.iso.torrent

ruby -v:
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

gem -v:
1.1.0

BEncode gem version: 0.5.0

Revision history for this message
Arsecroft (arsecroft) wrote :
Arsecroft (arsecroft)
description: updated
Revision history for this message
Daniel Schierbeck (dasch) wrote :

I think the string may be UTF-8... I'll have to add support for that, then.

Revision history for this message
Arsecroft (arsecroft) wrote : Re: [Bug 221218] Re: BEncode::DecodeError: invalid string length

Apparently bittorrent are required to be utf-8.
http://wiki.theory.org/BitTorrentSpecification#Metainfo_File_Structure

I'm not sure if there's a way to make ruby read the unencoded chars,
because then our problem would be solved.

This really isn't even a bug in the library, this is a bug in the
specific file(s), but it would make it a heck of a lot nicer if it
would work despite the faulty torrent files.

Revision history for this message
Daxx (b-launchpad-net-d10-karoo-co-uk) wrote :

Try ...
  File.open(filename, 'rb').read

(i.e. open the file in binary mode when using Windows OS)

Daxx

Revision history for this message
Daniel Schierbeck (dasch) wrote :

Fixed by Mike Hodgson in master.

Changed in ruby-bencode:
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.