[前][次][番号順一覧][スレッド一覧]

ruby-changes:34017

From: nobu <ko1@a...>
Date: Sun, 25 May 2014 11:56:43 +0900 (JST)
Subject: [ruby-changes:34017] nobu:r46098 (trunk): vsnprintf.c: fix string precision

nobu	2014-05-25 11:56:36 +0900 (Sun, 25 May 2014)

  New Revision: 46098

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=46098

  Log:
    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]

  Modified files:
    trunk/ChangeLog
    trunk/ext/-test-/printf/printf.c
    trunk/test/-ext-/test_printf.rb
    trunk/vsnprintf.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46097)
+++ ChangeLog	(revision 46098)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun May 25 11:56:33 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@z...>
 
 	* lib/yaml.rb: Remove Psych::EngineManager [Bug #8344]
Index: vsnprintf.c
===================================================================
--- vsnprintf.c	(revision 46097)
+++ vsnprintf.c	(revision 46098)
@@ -993,7 +993,7 @@ fp_begin:		_double = va_arg(ap, double); https://github.com/ruby/ruby/blob/trunk/vsnprintf.c#L993
 				 */
 				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;
Index: ext/-test-/printf/printf.c
===================================================================
--- ext/-test-/printf/printf.c	(revision 46097)
+++ ext/-test-/printf/printf.c	(revision 46098)
@@ -42,18 +42,23 @@ utoa(char *p, char *e, unsigned int x) https://github.com/ruby/ruby/blob/trunk/ext/-test-/printf/printf.c#L42
 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, https://github.com/ruby/ruby/blob/trunk/ext/-test-/printf/printf.c#L89
     }
     *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
Index: test/-ext-/test_printf.rb
===================================================================
--- test/-ext-/test_printf.rb	(revision 46097)
+++ test/-ext-/test_printf.rb	(revision 46098)
@@ -181,4 +181,10 @@ class Test_SPrintf < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_printf.rb#L181
                                             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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]