Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix error when assigning NaN to an integer column

Also covers any non-castable case by returning nil, which
is in-line with the intention of the former implementation,
but covers the odd cases which respond to to_i but raise
an error when it's called, such as NaN, Infinity and -Infinity.

Fixes #8757

Backport of #8781

Cherry Pick of c147dd7
  • Loading branch information...
commit 83c3475dd959d025d7f92cd7f9181cbc6fcc4a83 1 parent 335c988
@trisweb trisweb authored ChrisHooks committed
View
2  activerecord/lib/active_record/connection_adapters/column.rb
@@ -175,7 +175,7 @@ def value_to_integer(value)
when TrueClass, FalseClass
value ? 1 : 0
else
- value.to_i
+ value.to_i rescue nil
end
end
View
6 activerecord/test/cases/column_test.rb
@@ -48,6 +48,12 @@ def test_type_cast_non_integer_to_integer
column.type_cast(Object.new)
end
end
+
+ def test_type_cast_nan_and_infinity_to_integer
+ column = Column.new("field", nil, "integer")
+ assert_nil column.type_cast(Float::NAN)
+ assert_nil column.type_cast(1.0/0.0)
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.