From 9bbaafe03d6b17ee408d232b34df3f5668848e45 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Thu, 17 Dec 2009 10:24:58 -0500 Subject: [PATCH] Just use Regexp#source instead of hacky to_s. Check Regex pattern for NULL bytes --- lib/mongo/util/bson_ruby.rb | 6 ++++-- test/test_bson.rb | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/mongo/util/bson_ruby.rb b/lib/mongo/util/bson_ruby.rb index ba50349..98e6d7a 100644 --- a/lib/mongo/util/bson_ruby.rb +++ b/lib/mongo/util/bson_ruby.rb @@ -451,8 +451,10 @@ class BSON_RUBY buf.put(REGEX) self.class.serialize_key(buf, key) - str = val.to_s.sub(/.*?:/, '')[0..-2] # Turn "(?xxx:yyy)" into "yyy" - self.class.serialize_cstr(buf, str) + str = val.source + # We use serialize_key here since regex patterns aren't prefixed with + # length (can't contain the NULL byte). + self.class.serialize_key(buf, str) options = val.options options_str = '' diff --git a/test/test_bson.rb b/test/test_bson.rb index 2a88230..526eb92 100644 --- a/test/test_bson.rb +++ b/test/test_bson.rb @@ -329,6 +329,10 @@ class BSONTest < Test::Unit::TestCase assert_raise InvalidDocument do BSON.serialize({"\x00" => "a"}) end + + assert_raise InvalidDocument do + BSON.serialize({"a" => (Regexp.compile "ab\x00c")}) + end end end