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

ruby-changes:34501

From: usa <ko1@a...>
Date: Fri, 27 Jun 2014 17:46:27 +0900 (JST)
Subject: [ruby-changes:34501] usa:r46582 (ruby_2_0_0): merge revision(s) 46098: [Backport #9861]

usa	2014-06-27 17:46:12 +0900 (Fri, 27 Jun 2014)

  New Revision: 46582

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

  Log:
    merge revision(s) 46098: [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]

  Modified directories:
    branches/ruby_2_0_0/
  Modified files:
    branches/ruby_2_0_0/ChangeLog
    branches/ruby_2_0_0/ext/-test-/printf/printf.c
    branches/ruby_2_0_0/test/-ext-/test_printf.rb
    branches/ruby_2_0_0/version.h
    branches/ruby_2_0_0/vsnprintf.c
Index: ruby_2_0_0/ChangeLog
===================================================================
--- ruby_2_0_0/ChangeLog	(revision 46581)
+++ ruby_2_0_0/ChangeLog	(revision 46582)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1
+Fri Jun 27 17:37:12 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]
+
 Fri Jun 27 17:27:26 2014  Eric Wong  <e@8...>
 
 	* process.c (proc_getgroups, proc_setgroups): use ALLOCV_N
Index: ruby_2_0_0/vsnprintf.c
===================================================================
--- ruby_2_0_0/vsnprintf.c	(revision 46581)
+++ ruby_2_0_0/vsnprintf.c	(revision 46582)
@@ -999,7 +999,7 @@ fp_begin:		_double = va_arg(ap, double); https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/vsnprintf.c#L999
 				 */
 				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: ruby_2_0_0/ext/-test-/printf/printf.c
===================================================================
--- ruby_2_0_0/ext/-test-/printf/printf.c	(revision 46581)
+++ ruby_2_0_0/ext/-test-/printf/printf.c	(revision 46582)
@@ -21,6 +21,83 @@ printf_test_v(VALUE self, VALUE obj) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ext/-test-/printf/printf.c#L21
     return rb_enc_sprintf(rb_usascii_encoding(), "{%+"PRIsVALUE"}", obj);
 }
 
+static VALUE
+printf_test_q(VALUE self, VALUE obj)
+{
+    return rb_enc_sprintf(rb_usascii_encoding(), "[% "PRIsVALUE"]", obj);
+}
+
+static char *
+utoa(char *p, char *e, unsigned int x)
+{
+    char *e0 = e;
+    if (e <= p) return p;
+    do {
+	*--e = x % 10 + '0';
+    } while ((x /= 10) != 0 && e > p);
+    memmove(p, e, e0 - e);
+    return p + (e0 - e);
+}
+
+static VALUE
+printf_test_call(int argc, VALUE *argv, VALUE self)
+{
+    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':
+	n = NUM2INT(num);
+	break;
+      case 's':
+	s = StringValueCStr(num);
+	break;
+      default: rb_raise(rb_eArgError, "wrong conversion(%c)", cnv);
+    }
+    *p++ = '%';
+    if (!NIL_P(opt)) {
+	VALUE v;
+	Check_Type(opt, T_HASH);
+	if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("space"))))) {
+	    *p++ = ' ';
+	}
+	if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("hash"))))) {
+	    *p++ = '#';
+	}
+	if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("plus"))))) {
+	    *p++ = '+';
+	}
+	if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("minus"))))) {
+	    *p++ = '-';
+	}
+	if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("zero"))))) {
+	    *p++ = '0';
+	}
+	if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("width"))))) {
+	    p = utoa(p, format + sizeof(format), NUM2UINT(v));
+	}
+	if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("prec"))))) {
+	    *p++ = '.';
+	    if (FIXNUM_P(v))
+		p = utoa(p, format + sizeof(format), NUM2UINT(v));
+	}
+    }
+    *p++ = cnv;
+    *p++ = '\0';
+    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
 Init_printf(void)
 {
@@ -28,4 +105,5 @@ Init_printf(void) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ext/-test-/printf/printf.c#L105
     rb_define_singleton_method(m, "i", printf_test_i, 1);
     rb_define_singleton_method(m, "s", printf_test_s, 1);
     rb_define_singleton_method(m, "v", printf_test_v, 1);
+    rb_define_singleton_method(m, "call", printf_test_call, -1);
 }
Index: ruby_2_0_0/version.h
===================================================================
--- ruby_2_0_0/version.h	(revision 46581)
+++ ruby_2_0_0/version.h	(revision 46582)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1
 #define RUBY_VERSION "2.0.0"
 #define RUBY_RELEASE_DATE "2014-06-27"
-#define RUBY_PATCHLEVEL 506
+#define RUBY_PATCHLEVEL 507
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 6
Index: ruby_2_0_0/test/-ext-/test_printf.rb
===================================================================
--- ruby_2_0_0/test/-ext-/test_printf.rb	(revision 46581)
+++ ruby_2_0_0/test/-ext-/test_printf.rb	(revision 46582)
@@ -46,4 +46,10 @@ class Test_SPrintf < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/-ext-/test_printf.rb#L46
                    inspect: Bug::Printf.v(obj).untrusted?,
                  })
   end
+
+  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

Property changes on: ruby_2_0_0
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r46098


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

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