diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-06-29 17:17:20 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-06-29 17:17:20 +0000 |
commit | 0caeba64c30e6746870328a0d7da6250b0a3773c (patch) | |
tree | 4ebfc8e4bbebbdc612920a14cd5f5719ce99f030 /ext | |
parent | 8a33324477d9ba0b7dd8fd28a6927e89580664e6 (diff) |
merge revision(s) r46098: [Backport #9861]
* vsnprintf.c (BSD_vfprintf): fix string width when precision is
given. as the result of `memchr` is NULL or its offset from the
start cannot exceed the size, the comparison was always false.
[ruby-core:62737] [Bug #9861]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@46611 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r-- | ext/-test-/printf/printf.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/ext/-test-/printf/printf.c b/ext/-test-/printf/printf.c index fd60b0f593..1ebe80411b 100644 --- a/ext/-test-/printf/printf.c +++ b/ext/-test-/printf/printf.c @@ -42,18 +42,23 @@ utoa(char *p, char *e, unsigned int x) static VALUE printf_test_call(int argc, VALUE *argv, VALUE self) { - VALUE opt, type, num; + VALUE opt, type, num, result; char format[sizeof(int) * 6 + 8], *p = format, cnv; int n; + const char *s; rb_scan_args(argc, argv, "2:", &type, &num, &opt); Check_Type(type, T_STRING); if (RSTRING_LEN(type) != 1) rb_raise(rb_eArgError, "wrong length(%ld)", RSTRING_LEN(type)); switch (cnv = RSTRING_PTR(type)[0]) { - case 'd': case 'x': case 'o': case 'X': break; + case 'd': case 'x': case 'o': case 'X': + n = NUM2INT(num); + break; + case 's': + s = StringValueCStr(num); + break; default: rb_raise(rb_eArgError, "wrong conversion(%c)", cnv); } - n = NUM2INT(num); *p++ = '%'; if (!NIL_P(opt)) { VALUE v; @@ -84,8 +89,13 @@ printf_test_call(int argc, VALUE *argv, VALUE self) } *p++ = cnv; *p++ = '\0'; - return rb_assoc_new(rb_enc_sprintf(rb_usascii_encoding(), format, n), - rb_usascii_str_new_cstr(format)); + if (cnv == 's') { + result = rb_enc_sprintf(rb_usascii_encoding(), format, s); + } + else { + result = rb_enc_sprintf(rb_usascii_encoding(), format, n); + } + return rb_assoc_new(result, rb_usascii_str_new_cstr(format)); } void |