aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-03 06:33:13 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-03 06:33:13 +0000
commit1336844a7f3b30d21cc38eae32ad14e16a951e7d (patch)
tree4cc7a6af54afd5f8a38a1bdead524f160dd69e8b
parent6ae16dd1907227575c5ffccb16e9e765b3036705 (diff)
downloadruby-1336844a7f3b30d21cc38eae32ad14e16a951e7d.tar.gz
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--sprintf.c17
-rw-r--r--test/ruby/test_sprintf.rb3
3 files changed, 19 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 742fd15173..1c72c0d775 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Thu Dec 3 15:33:08 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* range.c (range_to_s): should be infected by the receiver.
diff --git a/sprintf.c b/sprintf.c
index 99c97a01c0..84d5fd112b 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -1107,16 +1107,19 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
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;
diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb
index 8af94a89fe..f400e2be6e 100644
--- a/test/ruby/test_sprintf.rb
+++ b/test/ruby/test_sprintf.rb
@@ -166,6 +166,9 @@ class TestSprintf < Test::Unit::TestCase
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