Skip to content

Commit 66e5dc4

Browse files
committed
Do no unescaped already unescaped bytea values
If the unescaped value contains a null byte, pg 0.18 will raise an exception.
1 parent 989dac9 commit 66e5dc4

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/oid.rb

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def type_cast(value)
3131
class Bytea < Type
3232
def type_cast(value)
3333
return if value.nil?
34+
# This is a flawed heuristic, but it avoids truncation;
35+
# we really shouldn’t be calling this with already-unescaped values
36+
return value if value =~ /\x00/
3437
PGconn.unescape_bytea value
3538
end
3639
end

activerecord/test/cases/dirty_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'models/parrot'
55
require 'models/person' # For optimistic locking
66
require 'models/aircraft'
7+
require 'models/binary'
78

89
class Pirate # Just reopening it, not defining it
910
attr_accessor :detected_changes_in_after_update # Boolean for if changes are detected
@@ -261,6 +262,14 @@ def test_float_zero_to_string_zero_not_marked_as_changed
261262
assert_empty data.changes
262263
end
263264

265+
def test_binary_with_null_byte_with_same_value_not_marked_as_changed
266+
binary = Binary.create!(data: "\\\\foo\x00\\\\bar")
267+
268+
binary.data = "\\\\foo\x00\\\\bar"
269+
270+
assert_not binary.changed?, "should be unchanged"
271+
end
272+
264273
def test_zero_to_blank_marked_as_changed
265274
pirate = Pirate.new
266275
pirate.catchphrase = "Yarrrr, me hearties"

0 commit comments

Comments
 (0)