Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix segmentation fault from calling from_string with a non-string

* Calling BSON::ObjectId.from_string with a BSON::ObjectId segfaulted
* C function attempted to use BSON::ObjectId as a String
* Adds separate check/exception for non-String arguments
  • Loading branch information...
commit 533f41163a7866d6faec808247b0d08233e65c85 1 parent 71512f2
@jferris jferris authored brandonblack committed
Showing with 13 additions and 1 deletion.
  1. +6 −0 ext/cbson/cbson.c
  2. +7 −1 test/bson/object_id_test.rb
View
6 ext/cbson/cbson.c
@@ -954,7 +954,13 @@ static VALUE objectid_from_string(VALUE self, VALUE str)
int i;
if (!legal_objectid_str(str)) {
+ if (TYPE(str) == T_STRING) {
rb_raise(InvalidObjectId, "illegal ObjectId format: %s", RSTRING_PTR(str));
+ } else {
+ VALUE inspect;
+ inspect = rb_funcall(str, rb_intern("to_s"), 0);
+ rb_raise(InvalidObjectId, "not a String: %s", inspect);
+ }
}
oid = rb_ary_new2(12);
View
8 test/bson/object_id_test.rb
@@ -70,11 +70,17 @@ def test_from_string
end
def test_illegal_from_string
- assert_raise InvalidObjectId do
+ assert_raise InvalidObjectId do
ObjectId.from_string("")
end
end
+ def test_from_string_with_object_id
+ assert_raise InvalidObjectId do
+ ObjectId.from_string(@o)
+ end
+ end
+
def test_legal
assert !ObjectId.legal?(nil)
assert !ObjectId.legal?("fred")
Please sign in to comment.
Something went wrong with that request. Please try again.