1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44482 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-01-01 18:43:13 +00:00
parent b66d7182b7
commit e4518c5bde
2 changed files with 13 additions and 2 deletions

View file

@ -259,8 +259,11 @@ fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
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);

View file

@ -58,6 +58,14 @@ if defined? DBM
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