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

ruby-changes:40130

From: nobu <ko1@a...>
Date: Thu, 22 Oct 2015 13:14:04 +0900 (JST)
Subject: [ruby-changes:40130] nobu:r52211 (trunk): io.c: check ARGV element type

nobu	2015-10-22 13:13:52 +0900 (Thu, 22 Oct 2015)

  New Revision: 52211

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

  Log:
    io.c: check ARGV element type
    
    * io.c (argf_next_argv): check ARGV element type, and try
      conversion if necessary.  [ruby-core:71140] [Bug #11610]

  Modified files:
    trunk/ChangeLog
    trunk/io.c
    trunk/test/ruby/test_argf.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52210)
+++ ChangeLog	(revision 52211)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Oct 22 13:13:49 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* io.c (argf_next_argv): check ARGV element type, and try
+	  conversion if necessary.  [ruby-core:71140] [Bug #11610]
+
 Thu Oct 22 11:11:16 2015  Shugo Maeda  <shugo@r...>
 
 	* test/net/ftp/test_ftp.rb: add tests for getbinaryfile and
Index: io.c
===================================================================
--- io.c	(revision 52210)
+++ io.c	(revision 52211)
@@ -8004,9 +8004,11 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8004
     if (ARGF.next_p == 1) {
       retry:
 	if (RARRAY_LEN(ARGF.argv) > 0) {
-	    ARGF.filename = rb_str_encode_ospath(rb_ary_shift(ARGF.argv));
-	    fn = StringValueCStr(ARGF.filename);
-	    if (strlen(fn) == 1 && fn[0] == '-') {
+	    VALUE filename = rb_ary_shift(ARGF.argv);
+	    StringValueCStr(filename);
+	    ARGF.filename = rb_str_encode_ospath(filename);
+	    fn = StringValueCStr(filename);
+	    if (RSTRING_LEN(filename) == 1 && fn[0] == '-') {
 		ARGF.current_file = rb_stdin;
 		if (ARGF.inplace) {
 		    rb_warn("Can't do inplace edit for stdio; skipping");
@@ -8015,7 +8017,7 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8017
 	    }
 	    else {
 		VALUE write_io = Qnil;
-		int fr = rb_sysopen(ARGF.filename, O_RDONLY, 0);
+		int fr = rb_sysopen(filename, O_RDONLY, 0);
 
 		if (ARGF.inplace) {
 		    struct stat st;
@@ -8029,7 +8031,7 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8031
 			rb_io_close(rb_stdout);
 		    }
 		    fstat(fr, &st);
-		    str = ARGF.filename;
+		    str = filename;
 		    if (*ARGF.inplace) {
 			str = rb_str_dup(str);
 			rb_str_cat2(str, ARGF.inplace);
@@ -8039,14 +8041,14 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8041
 			(void)unlink(RSTRING_PTR(str));
 			if (rename(fn, RSTRING_PTR(str)) < 0) {
 			    rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
-				    ARGF.filename, str, strerror(errno));
+				    filename, str, strerror(errno));
 			    goto retry;
 			}
 			fr = rb_sysopen(str, O_RDONLY, 0);
 #else
 			if (rename(fn, RSTRING_PTR(str)) < 0) {
 			    rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
-				    ARGF.filename, str, strerror(errno));
+				    filename, str, strerror(errno));
 			    close(fr);
 			    goto retry;
 			}
@@ -8058,13 +8060,13 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8060
 #else
 			if (unlink(fn) < 0) {
 			    rb_warn("Can't remove %"PRIsVALUE": %s, skipping file",
-				    ARGF.filename, strerror(errno));
+				    filename, strerror(errno));
 			    close(fr);
 			    goto retry;
 			}
 #endif
 		    }
-		    fw = rb_sysopen(ARGF.filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+		    fw = rb_sysopen(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
 #ifndef NO_SAFE_RENAME
 		    fstat(fw, &st2);
 #ifdef HAVE_FCHMOD
@@ -8080,9 +8082,9 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8082
 			err = chown(fn, st.st_uid, st.st_gid);
 #endif
 			if (err && getuid() == 0 && st2.st_uid == 0) {
-			    const char *wkfn = RSTRING_PTR(ARGF.filename);
+			    const char *wkfn = RSTRING_PTR(filename);
 			    rb_warn("Can't set owner/group of %"PRIsVALUE" to same as %"PRIsVALUE": %s, skipping file",
-				    ARGF.filename, str, strerror(errno));
+				    filename, str, strerror(errno));
 			    (void)close(fr);
 			    (void)close(fw);
 			    (void)unlink(wkfn);
Index: test/ruby/test_argf.rb
===================================================================
--- test/ruby/test_argf.rb	(revision 52210)
+++ test/ruby/test_argf.rb	(revision 52211)
@@ -899,4 +899,12 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L899
       assert_equal "done with eof\n", f.gets
     end
   end
+
+  def test_wrong_type
+    assert_separately([], <<-'end;')
+      bug11610 = '[ruby-core:71140] [Bug #11610]'
+      ARGV[0] = nil
+      assert_raise(TypeError, bug11610) {gets}
+    end;
+  end
 end

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

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