diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-31 00:41:56 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-31 00:41:56 +0000 |
commit | ba5aa6066869e80af71fddb0b42d31222f328c97 (patch) | |
tree | fec1a3a25d77156c9498a93272fcb7502c8a4cf8 /sprintf.c | |
parent | 4b9a21cdd6881325836ec54d1daca47c9ce4fa02 (diff) |
sprintf.c: avoid garbage in common (no exception) case
Format strings which are dynamically-generated will benefit
from this. This won't cover exceptions, but exceptions for
sprintf should be too uncommon to care about (unlike IO)
* sprintf.c (rb_str_format): use rb_str_tmp_frozen_{acquire,release}
* test/ruby/test_sprintf.rb (test_no_hidden_garbage): new test
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r-- | sprintf.c | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -475,6 +475,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) int tainted = 0; VALUE nextvalue; VALUE tmp; + VALUE orig; VALUE str; volatile VALUE hash = Qundef; @@ -498,7 +499,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) if (OBJ_TAINTED(fmt)) tainted = 1; StringValue(fmt); enc = rb_enc_get(fmt); - fmt = rb_str_new4(fmt); + orig = fmt; + fmt = rb_str_tmp_frozen_acquire(fmt); p = RSTRING_PTR(fmt); end = p + RSTRING_LEN(fmt); blen = 0; @@ -1196,7 +1198,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) } sprint_exit: - RB_GC_GUARD(fmt); + rb_str_tmp_frozen_release(orig, fmt); /* XXX - We cannot validate the number of arguments if (digit)$ style used. */ if (posarg >= 0 && nextarg < argc) { |