diff --git a/lib/fugit/cron.rb b/lib/fugit/cron.rb index 47fa294..13e5dce 100644 --- a/lib/fugit/cron.rb +++ b/lib/fugit/cron.rb @@ -46,7 +46,20 @@ def parse(s) def do_parse(s) parse(s) || - fail(ArgumentError.new("invalid cron string #{s.inspect}")) + fail(ArgumentError.new("invalid cron string #{trunc(s)}")) + end + + protected + + def trunc(s) + + if s.is_a?(String) + r = s.length > 28 ? s[0, 28] + "... len #{s.length}" : s + r.inspect + else + r = s.inspect + r.length > 35 ? s[0, 35] + '...' : r + end end end diff --git a/lib/fugit/nat.rb b/lib/fugit/nat.rb index e86de31..e11563e 100644 --- a/lib/fugit/nat.rb +++ b/lib/fugit/nat.rb @@ -7,6 +7,8 @@ module Fugit # module Nat + MAX_INPUT_LENGTH = 256 + class << self def parse(s, opts={}) @@ -17,6 +19,16 @@ def parse(s, opts={}) s = s.strip + if s.length > MAX_INPUT_LENGTH + + fail ArgumentError.new( + "input too long for a nat string, " + + "#{s.length} > #{MAX_INPUT_LENGTH}" + ) if opts[:do_parse] + + return nil + end + #p s; Raabro.pp(Parser.parse(s, debug: 3), colours: true) #(p s; Raabro.pp(Parser.parse(s, debug: 1), colours: true)) rescue nil @@ -29,7 +41,7 @@ def parse(s, opts={}) def do_parse(s, opts={}) - parse(s, opts) || + parse(s, opts.merge(do_parse: true)) || fail(ArgumentError.new("could not parse a nat #{s.inspect}")) end end diff --git a/lib/fugit/parse.rb b/lib/fugit/parse.rb index 73977a7..a8f0552 100644 --- a/lib/fugit/parse.rb +++ b/lib/fugit/parse.rb @@ -20,10 +20,10 @@ def parse(s, opts={}) opts[:at] = opts[:in] if opts.has_key?(:in) - (opts[:cron] != false && parse_cron(s)) || - (opts[:duration] != false && parse_duration(s)) || - (opts[:nat] != false && parse_nat(s, opts)) || - (opts[:at] != false && parse_at(s)) || + (opts[:cron] != false && parse_cron(s)) || # 542ms 616ms + (opts[:duration] != false && parse_duration(s)) || # 645ms # 534ms + (opts[:nat] != false && parse_nat(s, opts)) || # 2s # 35s + (opts[:at] != false && parse_at(s)) || # 568ms 622ms nil end diff --git a/spec/nat_spec.rb b/spec/nat_spec.rb index 0c1c08f..e3be77a 100644 --- a/spec/nat_spec.rb +++ b/spec/nat_spec.rb @@ -362,12 +362,18 @@ ].each do |input| - it "fails with ArgumentError for #{input.inspect}" do + it "fails with an ArgumentError for #{input.inspect}" do expect { Fugit::Nat.do_parse(input) - }.to raise_error(ArgumentError) + }.to raise_error(ArgumentError, /could not parse a nat/) end end + + it "fails with an ArgumentError when the input length > 256" do + + expect { Fugit::Nat.do_parse('a' * 5000) + }.to raise_error(ArgumentError, /too long .+ 5000 > 256/) + end end end