Skip to content

Commit 86d03fe

Browse files
committed
added type validation to distance method so that seg.faults are prevented
1 parent 87da243 commit 86d03fe

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ source :rubygems
33
gem 'ffi'
44

55
group :test do
6-
gem 'rspec', '1.3.1'
6+
gem 'rspec', '2.7.0'
77
gem 'jeweler'
88
end

Gemfile.lock

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
GEM
22
remote: http://rubygems.org/
33
specs:
4+
diff-lcs (1.1.3)
45
ffi (1.0.9)
56
git (1.2.5)
67
jeweler (1.6.4)
78
bundler (~> 1.0)
89
git (>= 1.2.5)
910
rake
1011
rake (0.9.2)
11-
rspec (1.3.1)
12+
rspec (2.7.0)
13+
rspec-core (~> 2.7.0)
14+
rspec-expectations (~> 2.7.0)
15+
rspec-mocks (~> 2.7.0)
16+
rspec-core (2.7.1)
17+
rspec-expectations (2.7.0)
18+
diff-lcs (~> 1.1.2)
19+
rspec-mocks (2.7.0)
1220

1321
PLATFORMS
1422
ruby
1523

1624
DEPENDENCIES
1725
ffi
1826
jeweler
19-
rspec (= 1.3.1)
27+
rspec (= 2.7.0)

lib/levenshtein.rb

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
require 'ffi'
22

33
module Levenshtein
4-
extend FFI::Library
5-
6-
# Try loading in order.
7-
library = File.dirname(__FILE__) + "/../ext/levenshtein/levenshtein"
8-
candidates = ['.bundle', '.so', '.dylib', ''].map { |ext| library + ext }
9-
ffi_lib(candidates)
10-
11-
attach_function :distance, :levenshtein, [:string, :string], :int
4+
class << self
5+
extend FFI::Library
6+
7+
# Try loading in order.
8+
library = File.dirname(__FILE__) + "/../ext/levenshtein/levenshtein"
9+
candidates = ['.bundle', '.so', '.dylib', ''].map { |ext| library + ext }
10+
ffi_lib(candidates)
11+
12+
def distance(str1, str2)
13+
validate(str1)
14+
validate(str2)
15+
ffi_distance(str1, str2)
16+
end
17+
18+
private
19+
def validate(arg)
20+
unless arg.kind_of?(String)
21+
raise TypeError, "wrong argument type #{arg.class} (expected String)"
22+
end
23+
end
24+
25+
attach_function :ffi_distance, :levenshtein, [:string, :string], :int
26+
end
1227
end

spec/levenshtein_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@
1919
Levenshtein.distance(w2, w1).should == d
2020
end
2121
end
22+
23+
it "should raise an error if either argument is nil" do
24+
expect { Levenshtein.distance("", nil) }.to raise_error TypeError
25+
expect { Levenshtein.distance(nil, "") }.to raise_error TypeError
26+
end
27+
28+
it "should raise an error if either argument is something else than a string" do
29+
expect { Levenshtein.distance("woah", /woah/) }.to raise_error TypeError
30+
expect { Levenshtein.distance(5.3, "5.3") }.to raise_error TypeError
31+
expect { Levenshtein.distance(Object.new, "Hello") }.to raise_error TypeError
32+
end
2233
end

spec/spec_helper.rb

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
require 'spec'
2-
31
require File.dirname(__FILE__) + "/../lib/levenshtein"

0 commit comments

Comments
 (0)