Do no unescaped already unescaped bytea values

If the unescaped value contains a null byte, pg 0.18 will raise an
exception.
This commit is contained in:
Rafael Mendonça França 2015-01-06 12:50:58 -03:00
parent 02edbcdf22
commit ed2b4eb1cb
2 changed files with 12 additions and 0 deletions

View File

@ -27,6 +27,9 @@ module ActiveRecord
class Bytea < Type
def type_cast(value)
return if value.nil?
# This is a flawed heuristic, but it avoids truncation;
# we really shouldnt be calling this with already-unescaped values
return value if value =~ /\x00/
PGconn.unescape_bytea value
end
end

View File

@ -4,6 +4,7 @@ require 'models/pirate' # For timestamps
require 'models/parrot'
require 'models/person' # For optimistic locking
require 'models/aircraft'
require 'models/binary'
class Pirate # Just reopening it, not defining it
attr_accessor :detected_changes_in_after_update # Boolean for if changes are detected
@ -261,6 +262,14 @@ class DirtyTest < ActiveRecord::TestCase
assert_empty data.changes
end
def test_binary_with_null_byte_with_same_value_not_marked_as_changed
binary = Binary.create!(data: "\\\\foo\x00\\\\bar")
binary.data = "\\\\foo\x00\\\\bar"
assert_not binary.changed?, "should be unchanged"
end
def test_zero_to_blank_marked_as_changed
pirate = Pirate.new
pirate.catchphrase = "Yarrrr, me hearties"