[前][次][番号順一覧][スレッド一覧]

ruby-changes:32403

From: nobu <ko1@a...>
Date: Thu, 2 Jan 2014 03:43:19 +0900 (JST)
Subject: [ruby-changes:32403] nobu:r44482 (trunk): dbm.c: yield dup of keystr

nobu	2014-01-02 03:43:13 +0900 (Thu, 02 Jan 2014)

  New Revision: 44482

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44482

  Log:
    dbm.c: yield dup of keystr
    
    * ext/dbm/dbm.c (fdbm_fetch): yield dup of keystr, to make it shared
      and get rid of use of uninitialized variable.

  Modified files:
    trunk/ext/dbm/dbm.c
    trunk/test/dbm/test_dbm.rb
Index: ext/dbm/dbm.c
===================================================================
--- ext/dbm/dbm.c	(revision 44481)
+++ ext/dbm/dbm.c	(revision 44482)
@@ -259,8 +259,11 @@ fdbm_fetch(VALUE obj, VALUE keystr, VALU https://github.com/ruby/ruby/blob/trunk/ext/dbm/dbm.c#L259
     value = dbm_fetch(dbm, key);
     if (value.dptr == 0) {
       not_found:
-	if (ifnone == Qnil && rb_block_given_p())
-	    return rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
+	if (NIL_P(ifnone) && rb_block_given_p()) {
+	    keystr = rb_str_dup(keystr);
+	    OBJ_TAINT(keystr);
+	    return rb_yield(keystr);
+	}
 	return ifnone;
     }
     return rb_tainted_str_new(value.dptr, value.dsize);
Index: test/dbm/test_dbm.rb
===================================================================
--- test/dbm/test_dbm.rb	(revision 44481)
+++ test/dbm/test_dbm.rb	(revision 44482)
@@ -58,6 +58,14 @@ if defined? DBM https://github.com/ruby/ruby/blob/trunk/test/dbm/test_dbm.rb#L58
         assert_nil(@dbm_rdonly.delete("bar"))
       end
     end
+
+    def test_fetch_not_found
+      notfound = nil
+      result = Object.new
+      assert_same(result, @dbm_rdonly.fetch("bar") {|k| notfound = k; result})
+      assert_equal("bar", notfound)
+      assert_predicate(notfound, :tainted?)
+    end
   end
 
   class TestDBM < Test::Unit::TestCase

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]