Skip to content

Commit

Permalink
start fix for issue #249
Browse files Browse the repository at this point in the history
  - should standardize how we coerced Integer/Float values in pb
  - need to coerce value in Varint to make sure it is in bounds
  - bounds should be inclusive
  • Loading branch information
abrandoned committed Mar 1, 2015
1 parent 2ea42e8 commit cf54ce9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/protobuf/field/bool_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def acceptable?(val)
[true, false].include?(val)
end

def coerce!(val)
val
end

def decode(value)
value == 1
end
Expand Down
2 changes: 1 addition & 1 deletion lib/protobuf/field/bytes_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def define_setter
begin
val = "#{val}" if val.is_a?(Symbol)

if val.nil?
if val.nil? || (val.respond_to?(:empty?) && val.empty?)
@values.delete(field.name)
elsif field.acceptable?(val)
@values[field.name] = val.dup
Expand Down
10 changes: 8 additions & 2 deletions lib/protobuf/field/varint_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class VarintField < BaseField
##
# Constants
#

INT32_MAX = 2**31 - 1
INT32_MIN = -2**31
INT64_MAX = 2**63 - 1
Expand Down Expand Up @@ -37,11 +36,18 @@ def self.encode(value)
#

def acceptable?(val)
(val > self.class.min || val < self.class.max)
return false unless val.respond_to?(:to_i)

coerced_value = coerce!(val)
coerced_value >= self.class.min && coerced_value <= self.class.max
rescue
false
end

def coerce!(val)
Integer(val)
end

def decode(value)
value
end
Expand Down

0 comments on commit cf54ce9

Please sign in to comment.