[macruby-changes] [4928] MacRuby/trunk/sprintf.c

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 22 01:09:27 PST 2010


Revision: 4928
          http://trac.macosforge.org/projects/ruby/changeset/4928
Author:   watson1978 at gmail.com
Date:     2010-11-22 01:09:20 -0800 (Mon, 22 Nov 2010)
Log Message:
-----------
sprintf() will not throw exception when passed the format of single % character before a '\n' or '\0'.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_equal("%", sprintf("%", ""))
assert_equal("foo%", sprintf("foo%", ""))
assert_equal("%\n", sprintf("%\n", ""))
assert_equal("%\n.3f", sprintf("%\n.3f", 1.2))
assert_equal("%\x00.3f", sprintf("%\0.3f", 1.2))
assert_raise(ArgumentError){ sprintf("% ", "") }

puts :ok
}}}

Modified Paths:
--------------
    MacRuby/trunk/sprintf.c

Modified: MacRuby/trunk/sprintf.c
===================================================================
--- MacRuby/trunk/sprintf.c	2010-11-22 07:30:50 UTC (rev 4927)
+++ MacRuby/trunk/sprintf.c	2010-11-22 09:09:20 UTC (rev 4928)
@@ -716,8 +716,23 @@
 		    break;
 
 		default:
-		    rb_raise(rb_eArgError, "malformed format string - %%%c",
-			    format_str[i]);
+		    if (format_str[i - 1] == '%' &&
+			(format_str[i] == '\0' || format_str[i] == '\n')) {
+			if (format_str[i] == '\n') {
+			    arg = rb_str_new("%\n", 2);
+			}
+			else if (format_len > i) {
+			    arg = rb_str_new("%\0", 2);
+			}
+			else {
+			    arg = rb_str_new("%", 1);
+			}
+			complete = true;
+		    }
+		    else {
+			rb_raise(rb_eArgError, "malformed format string - %%%c",
+				 format_str[i]);
+		    }
 	    }
 	    if (!complete) {
 		continue;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101122/ad67097b/attachment-0001.html>


More information about the macruby-changes mailing list