summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-05-25 02:56:36 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-05-25 02:56:36 +0000
commitbae87a479016504577b7def06f38615837f12b50 (patch)
tree20eed2f9caacc78727f7ef824495b8c42edb59d2
parent7833e69c2ea4c843f6930b4a80795025f36ab155 (diff)
vsnprintf.c: fix string precision
* 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/trunk@46098 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--ext/-test-/printf/printf.c20
-rw-r--r--test/-ext-/test_printf.rb6
-rw-r--r--vsnprintf.c2
4 files changed, 29 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index e7e7af0ef64..94ef80868a2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Sun May 25 11:56:33 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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]
+
Sun May 25 11:32:42 2014 Zachary Scott <e@zzak.io>
* lib/yaml.rb: Remove Psych::EngineManager [Bug #8344]
diff --git a/ext/-test-/printf/printf.c b/ext/-test-/printf/printf.c
index fd60b0f5932..1ebe80411bd 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
diff --git a/test/-ext-/test_printf.rb b/test/-ext-/test_printf.rb
index 609af7ca7a7..235865dbdc1 100644
--- a/test/-ext-/test_printf.rb
+++ b/test/-ext-/test_printf.rb
@@ -181,4 +181,10 @@ class Test_SPrintf < Test::Unit::TestCase
zero: zr, width: width,
prec: prec))
}
+
+ def test_string_prec
+ assert_equal("a", Bug::Printf.("s", "a", prec: 3)[0])
+ assert_equal(" a", Bug::Printf.("s", "a", width: 3, prec: 3)[0])
+ assert_equal("a ", Bug::Printf.("s", "a", minus: true, width: 3, prec: 3)[0])
+ end
end
diff --git a/vsnprintf.c b/vsnprintf.c
index d8e66cdbd09..f8b5a8970b0 100644
--- a/vsnprintf.c
+++ b/vsnprintf.c
@@ -993,7 +993,7 @@ fp_begin: _double = va_arg(ap, double);
*/
const char *p = (char *)memchr(cp, 0, prec);
- if (p != NULL && (p - cp) > prec)
+ if (p != NULL && (p - cp) < prec)
size = (int)(p - cp);
else
size = prec;