about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-03-13 00:22:22 -0800
committerEric Wong <normalperson@yhbt.net>2010-03-13 00:22:22 -0800
commit437cbe78cb0f239e34f9dbf1341f5b85f96f2f25 (patch)
treef8f244f5e71fb315d971da021b881567d5f5c425
parent74e1f75b238416d9ac402f291431a5e457ae313f (diff)
downloadruby_posix_mq-437cbe78cb0f239e34f9dbf1341f5b85f96f2f25.tar.gz
We don't want folks to accidentally clobber the value for
others, so allocate a new string object for it (don't worry,
rb_str_dup() is cheap in 1.9).
-rw-r--r--ext/posix_mq/posix_mq.c2
-rw-r--r--test/test_posix_mq.rb9
2 files changed, 10 insertions, 1 deletions
diff --git a/ext/posix_mq/posix_mq.c b/ext/posix_mq/posix_mq.c
index 80943fb..d9abf1e 100644
--- a/ext/posix_mq/posix_mq.c
+++ b/ext/posix_mq/posix_mq.c
@@ -685,7 +685,7 @@ static VALUE name(VALUE self)
 {
         struct posix_mq *mq = get(self, 0);
 
-        return mq->name;
+        return rb_str_dup(mq->name);
 }
 
 static int lookup_sig(VALUE sig)
diff --git a/test/test_posix_mq.rb b/test/test_posix_mq.rb
index fa98a9c..963d623 100644
--- a/test/test_posix_mq.rb
+++ b/test/test_posix_mq.rb
@@ -27,6 +27,15 @@ class Test_POSIX_MQ < Test::Unit::TestCase
     assert @mq.closed?
   end
 
+  def test_name_clobber_proof
+    @mq = POSIX_MQ.new(@path, :rw)
+    tmp = @mq.name
+    tmp.freeze
+    assert_nothing_raised { @mq.name.gsub!(/\A/, "foo") }
+    assert_equal tmp, @mq.name
+    assert tmp.object_id != @mq.name.object_id
+  end
+
   def test_dup_clone
     @mq = POSIX_MQ.new(@path, :rw)
     dup = @mq.dup