Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack overflow doing IPAddress#as_json #89

Open
asomers opened this issue May 19, 2017 · 0 comments · May be fixed by #90
Open

Stack overflow doing IPAddress#as_json #89

asomers opened this issue May 19, 2017 · 0 comments · May be fixed by #90

Comments

@asomers
Copy link
Contributor

asomers commented May 19, 2017

The IPAddress gem abuses the Enumerable mixin. One example is the each method. That method will always yield at least one value, which causes problems for other Ruby code that assumes an Enumerable cannot contain itself. For example, the following snippet will trigger a stack overflow:

require 'ipaddress'
require 'active_support/json'
IPAddress.parse("1.2.3.4/32").as_json

The best solution would be to draw a distinction between an address and a network, like Python3's ipaddress module. If that were the case, then IPNetwork#each would yield a bunch of IPAddress objects, which would not implement #each.

A smaller change that wouldn't break compatibility with existing IPAddress users to be to define an as_json method which would simply call to_string.

asomers added a commit to asomers/ipaddress that referenced this issue Aug 7, 2017
Add IPAddress::IPv4#as_json and IPAddress::IPv6#as_json.  If that method
is not defined, then ActiveSupport will create it automatically, but it
overflows the stack due to the way that ipaddress uses #each.

Fixes ipaddress-gem#89
@asomers asomers linked a pull request Aug 7, 2017 that will close this issue
smortex pushed a commit to asomers/ipaddress that referenced this issue Aug 30, 2017
Add IPAddress::IPv4#as_json and IPAddress::IPv6#as_json.  If that method
is not defined, then ActiveSupport will create it automatically, but it
overflows the stack due to the way that ipaddress uses #each.

Fixes ipaddress-gem#89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant