Skip to content
Browse files

Raise an error if length is longer than 9 digits

  • Loading branch information...
1 parent 6e13238 commit 79b7a07ae4273b7b71ef7cf915f63839be41eb3e @josh josh committed May 15, 2011
Showing with 18 additions and 4 deletions.
  1. +12 −4 lib/otnetstring.rb
  2. +6 −0 spec/otnetstring_spec.rb
View
16 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)
View
6 spec/otnetstring_spec.rb
@@ -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

0 comments on commit 79b7a07

Please sign in to comment.
Something went wrong with that request. Please try again.