Skip to content

Commit

Permalink
Merge pull request protocolbuffers#332 from skippy/nil-out-of-bounds
Browse files Browse the repository at this point in the history
ruby: return nil for out of bounds
  • Loading branch information
cfallin committed May 3, 2015
2 parents 4b2a632 + d55733c commit dcf1213
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 26 deletions.
5 changes: 3 additions & 2 deletions ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ To build this Ruby extension, you will need:
To Build the JRuby extension, you will need:

* Maven
* The latest version of the protobuf java library
* The latest version of the protobuf java library (see ../java/README.md)
* Install JRuby via rbenv or RVM

First switch to the desired platform with rbenv or RVM.
Expand All @@ -75,7 +75,8 @@ Then install the required Ruby gems:

Then build the Gem:

$ rake gem
$ rake
$ rake clobber_package gem
$ gem install `ls pkg/google-protobuf-*.gem`

To run the specs:
Expand Down
3 changes: 3 additions & 0 deletions ruby/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ require "rake/testtask"
spec = Gem::Specification.load("google-protobuf.gemspec")

if RUBY_PLATFORM == "java"
if `which mvn` == ''
raise ArgumentError, "maven needs to be installed"
end
task :clean do
system("mvn clean")
end
Expand Down
20 changes: 14 additions & 6 deletions ruby/ext/google/protobuf_c/repeated_field.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ RepeatedField* ruby_to_RepeatedField(VALUE _self) {
return self;
}

static int index_position(VALUE _index, RepeatedField* repeated_field) {
int index = NUM2INT(_index);
if (index < 0 && repeated_field->size > 0) {
index = repeated_field->size + index;
}
return index;
}


/*
* call-seq:
* RepeatedField.each(&block)
Expand Down Expand Up @@ -74,18 +83,17 @@ VALUE RepeatedField_each(VALUE _self) {
* call-seq:
* RepeatedField.[](index) => value
*
* Accesses the element at the given index. Throws an exception on out-of-bounds
* errors.
* Accesses the element at the given index. Returns nil on out-of-bounds
*/
VALUE RepeatedField_index(VALUE _self, VALUE _index) {
RepeatedField* self = ruby_to_RepeatedField(_self);
int element_size = native_slot_size(self->field_type);
upb_fieldtype_t field_type = self->field_type;
VALUE field_type_class = self->field_type_class;

int index = NUM2INT(_index);
int index = index_position(_index, self);
if (index < 0 || index >= self->size) {
rb_raise(rb_eRangeError, "Index out of range");
return Qnil;
}

void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
Expand All @@ -105,9 +113,9 @@ VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
VALUE field_type_class = self->field_type_class;
int element_size = native_slot_size(field_type);

int index = NUM2INT(_index);
int index = index_position(_index, self);
if (index < 0 || index >= (INT_MAX - 1)) {
rb_raise(rb_eRangeError, "Index out of range");
return Qnil;
}
if (index >= self->size) {
RepeatedField_reserve(self, index + 1);
Expand Down
2 changes: 1 addition & 1 deletion ruby/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.0.0-pre</version>
<version>3.0.0-alpha-3-pre</version>
</dependency>
</dependencies>
</project>
14 changes: 7 additions & 7 deletions ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,15 @@ public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) {
public IRubyObject dup(ThreadContext context) {
RubyMessage dup = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK);
IRubyObject value;
for (Descriptors.FieldDescriptor fieldDescriptor : builder.getAllFields().keySet()) {
for (Descriptors.FieldDescriptor fieldDescriptor : this.descriptor.getFields()) {
if (fieldDescriptor.isRepeated()) {
dup.repeatedFields.put(fieldDescriptor, getRepeatedField(context, fieldDescriptor));
} else if (builder.hasField(fieldDescriptor)) {
dup.fields.put(fieldDescriptor, wrapField(context, fieldDescriptor, builder.getField(fieldDescriptor)));
dup.addRepeatedField(fieldDescriptor, this.getRepeatedField(context, fieldDescriptor));
} else if (fields.containsKey(fieldDescriptor)) {
dup.fields.put(fieldDescriptor, fields.get(fieldDescriptor));
} else if (this.builder.hasField(fieldDescriptor)) {
dup.fields.put(fieldDescriptor, wrapField(context, fieldDescriptor, this.builder.getField(fieldDescriptor)));
}
}
for (Descriptors.FieldDescriptor fieldDescriptor : fields.keySet()) {
dup.fields.put(fieldDescriptor, fields.get(fieldDescriptor));
}
for (Descriptors.FieldDescriptor fieldDescriptor : maps.keySet()) {
dup.maps.put(fieldDescriptor, maps.get(fieldDescriptor));
}
Expand Down Expand Up @@ -411,6 +410,7 @@ private RubyRepeatedField getRepeatedField(ThreadContext context, Descriptors.Fi
for (int i = 0; i < count; i++) {
ret.push(context, wrapField(context, fieldDescriptor, this.builder.getRepeatedField(fieldDescriptor, i)));
}
addRepeatedField(fieldDescriptor, ret);
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,25 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
*/
@JRubyMethod(name = "[]=")
public IRubyObject indexSet(ThreadContext context, IRubyObject index, IRubyObject value) {
int arrIndex = normalizeArrayIndex(index);
Utils.checkType(context, fieldType, value, (RubyModule) typeClass);
this.storage.set(RubyNumeric.num2int(index), value);
this.storage.set(arrIndex, value);
return context.runtime.getNil();
}

/*
* call-seq:
* RepeatedField.[](index) => value
*
* Accesses the element at the given index. Throws an exception on out-of-bounds
* errors.
* Accesses the element at the given index. Returns nil on out-of-bounds
*/
@JRubyMethod(name = "[]")
public IRubyObject index(ThreadContext context, IRubyObject index) {
return this.storage.eltInternal(RubyNumeric.num2int(index));
int arrIndex = normalizeArrayIndex(index);
if (arrIndex < 0 || arrIndex >= this.storage.size()) {
return context.runtime.getNil();
}
return this.storage.eltInternal(arrIndex);
}

/*
Expand All @@ -134,8 +138,7 @@ public IRubyObject index(ThreadContext context, IRubyObject index) {
@JRubyMethod(rest = true)
public IRubyObject insert(ThreadContext context, IRubyObject[] args) {
for (int i = 0; i < args.length; i++) {
Utils.checkType(context, fieldType, args[i], (RubyModule) typeClass);
this.storage.add(args[i]);
push(context, args[i]);
}
return context.runtime.getNil();
}
Expand Down Expand Up @@ -385,6 +388,15 @@ private void checkArrayElementType(ThreadContext context, RubyArray arr) {
}
}

private int normalizeArrayIndex(IRubyObject index) {
int arrIndex = RubyNumeric.num2int(index);
int arrSize = this.storage.size();
if (arrIndex < 0 && arrSize > 0) {
arrIndex = arrSize + arrIndex;
}
return arrIndex;
}

private RubyArray storage;
private Descriptors.FieldDescriptor.Type fieldType;
private IRubyObject typeClass;
Expand Down
43 changes: 39 additions & 4 deletions ruby/tests/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ def test_rptfield_int32
assert l4 == [0, 0, 0, 0, 0, 42, 100, 101, 102]
end

def test_parent_rptfield
#make sure we set the RepeatedField and can add to it
m = TestMessage.new
assert m.repeated_string == []
m.repeated_string << 'ok'
m.repeated_string.push('ok2')
assert m.repeated_string == ['ok', 'ok2']
m.repeated_string += ['ok3']
assert m.repeated_string == ['ok', 'ok2', 'ok3']
end

def test_rptfield_msg
l = Google::Protobuf::RepeatedField.new(:message, TestMessage)
l.push TestMessage.new
Expand Down Expand Up @@ -383,10 +394,31 @@ def test_rptfield_array_ducktyping
length_methods.each do |lm|
assert l.send(lm) == 0
end
# out of bounds returns a nil
assert l[0] == nil
assert l[1] == nil
assert l[-1] == nil
l.push 4
length_methods.each do |lm|
assert l.send(lm) == 1
assert l.send(lm) == 1
end
assert l[0] == 4
assert l[1] == nil
assert l[-1] == 4
assert l[-2] == nil

l.push 2
length_methods.each do |lm|
assert l.send(lm) == 2
end
assert l[0] == 4
assert l[1] == 2
assert l[2] == nil
assert l[-1] == 2
assert l[-2] == 4
assert l[-3] == nil

#adding out of scope will backfill with empty objects
end

def test_map_basic
Expand Down Expand Up @@ -724,9 +756,12 @@ def test_dup
m = TestMessage.new
m.optional_string = "hello"
m.optional_int32 = 42
m.repeated_msg.push TestMessage2.new(:foo => 100)
m.repeated_msg.push TestMessage2.new(:foo => 200)

tm1 = TestMessage2.new(:foo => 100)
tm2 = TestMessage2.new(:foo => 200)
m.repeated_msg.push tm1
assert m.repeated_msg[-1] == tm1
m.repeated_msg.push tm2
assert m.repeated_msg[-1] == tm2
m2 = m.dup
assert m == m2
m.optional_int32 += 1
Expand Down

0 comments on commit dcf1213

Please sign in to comment.