Skip to content

Commit

Permalink
Raise an error if length is longer than 9 digits
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 15, 2011
1 parent 6e13238 commit 79b7a07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/otnetstring.rb
@@ -1,14 +1,22 @@
require 'stringio'

module OTNetstring
class Error < StandardError; end

def self.parse(io)
io = StringIO.new(io) if io.respond_to? :to_str
length, byte = "", "0"
while byte =~ /\d/
length << byte
length, byte = "", nil

while byte.nil? || byte =~ /\d/
length << byte if byte
byte = io.readchar
end
length = length.to_i

if length.size > 9
raise Error, "#{length} is longer than 9 digits"
end
length = Integer(length)

case byte
when '#' then Integer io.read(length)
when ',' then io.read(length)
Expand Down
6 changes: 6 additions & 0 deletions spec/otnetstring_spec.rb
Expand Up @@ -59,6 +59,12 @@
it "parses a boolean" do
OTNetstring.parse('4!true!').should == true
end

it "raises an error if length is longer than 9 digits" do
lambda {
OTNetstring.parse('9' * 10 + ',')
}.should raise_error(OTNetstring::Error, '9999999999 is longer than 9 digits')
end
end

context "encoding" do
Expand Down

0 comments on commit 79b7a07

Please sign in to comment.