ruby-changes:40790
From: nobu <ko1@a...>
Date: Thu, 3 Dec 2015 15:33:19 +0900 (JST)
Subject: [ruby-changes:40790] nobu:r52869 (trunk): sprintf.c: fix garbage inserted with Rational
nobu 2015-12-03 15:33:13 +0900 (Thu, 03 Dec 2015) New Revision: 52869 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52869 Log: sprintf.c: fix garbage inserted with Rational * sprintf.c (rb_str_format): fix wrong shifting position in Rational conversion when not at the beginning of the result. [ruby-core:71806] [Bug #11766] Modified files: trunk/ChangeLog trunk/sprintf.c trunk/test/ruby/test_sprintf.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 52868) +++ ChangeLog (revision 52869) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Dec 3 15:33:08 2015 Nobuyoshi Nakada <nobu@r...> + + * sprintf.c (rb_str_format): fix wrong shifting position in + Rational conversion when not at the beginning of the result. + [ruby-core:71806] [Bug #11766] + Thu Dec 3 14:22:16 2015 Nobuyoshi Nakada <nobu@r...> * range.c (range_to_s): should be infected by the receiver. Index: sprintf.c =================================================================== --- sprintf.c (revision 52868) +++ sprintf.c (revision 52869) @@ -1107,16 +1107,19 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1107 done += prec; } if ((flags & FWIDTH) && width > done) { + int fill = ' '; + long shifting = 0; if (!(flags&FMINUS)) { - long i, shifting = (flags&FZERO) ? done - prefix : done; - for (i = 1; i <= shifting; i++) - buf[width - i] = buf[done - i]; + shifting = done; + if (flags&FZERO) { + shifting -= prefix; + fill = '0'; + } blen -= shifting; - FILL((flags&FZERO) ? '0' : ' ', width - done); - blen += shifting; - } else { - FILL(' ', width - done); + memmove(&buf[blen + width - done], &buf[blen], shifting); } + FILL(fill, width - done); + blen += shifting; } RB_GC_GUARD(val); break; Index: test/ruby/test_sprintf.rb =================================================================== --- test/ruby/test_sprintf.rb (revision 52868) +++ test/ruby/test_sprintf.rb (revision 52869) @@ -166,6 +166,9 @@ class TestSprintf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_sprintf.rb#L166 end end end + + bug11766 = '[ruby-core:71806] [Bug #11766]' + assert_equal("x"*10+" 1.0", sprintf("x"*10+"%8.1f", 1r)) end def test_hash -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/