cjheath / geoip

The Ruby gem for querying Maxmind.com's GeoIP database, which returns the geographic location of a server given its IP address

Home Page:http://geoip.rubyforge.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Geoip#country raises undefined method when sent a string that is not an IP address

mocoso opened this issue · comments

If you pass in a string that is blank or just contains numbers and dots (but is not an IP address) then GeoIP raises a NoMethodError: undefined method `&' for "1.2":String exception

For example

> GeoIP.new('the-data-file.dat').country('')
NoMethodError: undefined method `&' for "1.2":String
    from /[redacted]/geoip-1.1.1/lib/geoip.rb:518:in `seek_record'

or

> GeoIP.new('the-data-file.dat').country('1.2.3')
NoMethodError: undefined method `&' for "1.2":String
    from /[redacted]/geoip-1.1.1/lib/geoip.rb:518:in `seek_record'

Better behaviour would be to return nil OR to raise an exception complaining that the argument is invalid

Also, whitespace could be handled better (.strip?):

1.8.7 :019 > geo_ip.country("90.79.162.140").country_name => "France" 1.8.7 :020 > geo_ip.country("90.79.162.140\n").country_name => "France" 1.8.7 :021 > geo_ip.country("90.79.162.140 \n").country_name => "N/A"

What version of Ruby are you getting this with? Both Ruby 1.9.3-p280 and 1.8.7 give me a SocketError exception:
SocketError: newline at the end of hostname
from ./lib/geoip.rb:504:in `getaddress'
...

and I don't get the "undefined method `&' for "1.2":String exception" error either (geoip version change) but rather just "nil" - it's possible that the IPv6 patch fixed this?