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

Fix assign ip address with invalid values raise exception #11574

Merged
merged 1 commit into from
Aug 14, 2013

Conversation

pftg
Copy link
Contributor

@pftg pftg commented Jul 23, 2013

Closes #11552

In order that set attribute should not be bang method, rescue invalid ip address exceptions.

There is two options to resolve #11552 issue:

  • remain invalid value as attribute value and rescue all exceptions on type casting, like I have proposed in this PR
  • return default IPAddr.new value like BigDecimal does: IPAddr.new 'invalid' #=> #<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>

What do you think about second solution?

@pftg
Copy link
Contributor Author

pftg commented Jul 23, 2013

/cc @kennyj, @senny, @rafaelfranca

@egilburg
Copy link
Contributor

In case of other fields that cannot parse, setting invalid value sets it to nil.

record = SomeRecord.new
record.updated_at = 'asdf'
record.updated_at # => nil
record.updated_at_before_type_cast # => 'asdf'

If saved, nil will be set (subject to NOT NULL constraints, if present), not the raw value.

Should the same behavior be preserved for other parseable fields like IP address?

@pftg
Copy link
Contributor Author

pftg commented Jul 24, 2013

cool idea, thanks, I thought about it. Have skipped it to be consistent with others casts for Postgresql. But, I think need to give it a chance. Will review more casts solutions.

@pftg
Copy link
Contributor Author

pftg commented Jul 26, 2013

Changed fix with using as @egilburg mention nil strategy for invalid attributes assigns.
Added Changelog.

So, it's ready for reviewing. Thanks in advance.

P.S. Build failed because of:

$ gem install bundler
ERROR:  Could not find a valid gem 'bundler' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Connection timed out - connect(2) (https://rubygems.org/latest_specs.4.8.gz)
ERROR:  Possible alternatives: bundler

@@ -100,7 +100,7 @@ def string_to_cidr(string)
if string.nil?
nil
elsif String === string
IPAddr.new(string)
IPAddr.new(string) rescue nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific Exception we can rescue to assign nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may raise: RuntimeError or ArgumentError

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what circumstances does it raise a RuntimeError?

Sample irb session:

Loading development environment (Rails 4.0.0)
irb(main):001:0> IPAddr.new("akjsd")
IPAddr::InvalidAddressError: invalid address

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You checked 2.0.0 ruby version. But I found in 1.9.3 version only 2 exceptions. Not sure that #set invoked from #initialize

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can get RuntimeError only from methods which are not invoked on initialization step.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for ruby 1.9.3:

ArgumentError: invalid address

for ruby 2.0.0:

IPAddr::InvalidAddressError: invalid address

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't completely understand what you mean with:

Yes, we can get RuntimeError only from methods which are not invoked on initialization step.

As IPAddr::InvalidAddressError is a subclass of ArgumentError catching it would be perfect but I'm still not sure when you get a RuntimeError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPAddr::InvalidAddressError is sub-class of ArgumentError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your point, fixing it. Thanks.

@senny
Copy link
Member

senny commented Aug 14, 2013

this no longer applies cleanly, can you rebase?

@pftg
Copy link
Contributor Author

pftg commented Aug 14, 2013

Thanks @senny for your comments. Updated with rescue only specific exception ArgumentError, tested for ruby 1.9.3 and 2.0.0. Awaiting for travis build.

* Assign inet/cidr attribute with `nil` value for invalid address.

`IPAddr` raise exception on parsing invalid address. Rescue exception
on type cast and return `nil`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this paragraph should be replaced with an Example. It explains the situation better from a user perspective.

In order that set attribute should not be bang method
@pftg
Copy link
Contributor Author

pftg commented Aug 14, 2013

@senny, updated with example of the issue in Changelog.

senny added a commit that referenced this pull request Aug 14, 2013
…et_assign

Fix assign ip address with invalid values raise exception
@senny senny merged commit a4d4af4 into rails:master Aug 14, 2013
@senny
Copy link
Member

senny commented Aug 14, 2013

Thank you 💛

@pftg
Copy link
Contributor Author

pftg commented Aug 14, 2013

Thanks @senny!

@pftg pftg deleted the 11552_rescue_on_invalid_inet_assign branch August 14, 2013 18:35
@leckylao
Copy link

leckylao commented Sep 9, 2013

👍

senny added a commit that referenced this pull request Oct 22, 2013
…et_assign

Fix assign ip address with invalid values raise exception
Conflicts:
	activerecord/CHANGELOG.md
@Intrepidd
Copy link
Contributor

Hey, since you merged this one for 4.0.4rc1, how about merging #11867 for consistency ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Postgres inet type not usable in Rails 4?
5 participants