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

ruby-changes:2664

From: ko1@a...
Date: 9 Dec 2007 14:15:00 +0900
Subject: [ruby-changes:2664] akr - Ruby:r14155 (trunk): * include/ruby/ruby.h (FilePathStringValue): defined. similar to

akr	2007-12-09 14:12:31 +0900 (Sun, 09 Dec 2007)

  New Revision: 14155

  Modified files:
    trunk/ChangeLog
    trunk/dir.c
    trunk/file.c
    trunk/include/ruby/ruby.h
    trunk/test/pathname/test_pathname.rb

  Log:
    * include/ruby/ruby.h (FilePathStringValue): defined.  similar to
      FilePathValue but no taint check.
    
    * file.c (rb_get_path_no_checksafe): implementation of
      FilePathStringValue.
      (rb_file_s_basename): use FilePathStringValue.
      (rb_file_s_dirname): ditto.
      (rb_file_s_extname): ditto.
      (rb_file_s_split): ditto.
      (rb_file_join): ditto.
    
    * dir.c (file_s_fnmatch): ditto.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/ruby.h?r1=14155&r2=14154
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/pathname/test_pathname.rb?r1=14155&r2=14154
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/file.c?r1=14155&r2=14154
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14155&r2=14154
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/dir.c?r1=14155&r2=14154

Index: include/ruby/ruby.h
===================================================================
--- include/ruby/ruby.h	(revision 14154)
+++ include/ruby/ruby.h	(revision 14155)
@@ -326,6 +326,9 @@
 VALUE rb_get_path(VALUE);
 #define FilePathValue(v) ((v) = rb_get_path(v))
 
+VALUE rb_get_path_no_checksafe(VALUE);
+#define FilePathStringValue(v) ((v) = rb_get_path_no_checksafe(v))
+
 void rb_secure(int);
 int rb_safe_level(void);
 void rb_set_safe_level(int);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 14154)
+++ ChangeLog	(revision 14155)
@@ -1,3 +1,18 @@
+Sun Dec  9 14:08:47 2007  Tanaka Akira  <akr@f...>
+
+	* include/ruby/ruby.h (FilePathStringValue): defined.  similar to
+	  FilePathValue but no taint check.
+
+	* file.c (rb_get_path_no_checksafe): implementation of
+	  FilePathStringValue.
+	  (rb_file_s_basename): use FilePathStringValue.
+	  (rb_file_s_dirname): ditto.
+	  (rb_file_s_extname): ditto.
+	  (rb_file_s_split): ditto.
+	  (rb_file_join): ditto.
+
+	* dir.c (file_s_fnmatch): ditto.
+
 Sun Dec  9 12:49:34 2007  Tanaka Akira  <akr@f...>
 
 	* re.c (append_utf8): check unicode range.
Index: dir.c
===================================================================
--- dir.c	(revision 14154)
+++ dir.c	(revision 14155)
@@ -1849,7 +1849,7 @@
 	flags = 0;
 
     StringValue(pattern);
-    StringValue(path);
+    FilePathStringValue(path);
 
     if (fnmatch(RSTRING_PTR(pattern), RSTRING_PTR(path), flags) == 0)
 	return Qtrue;
Index: test/pathname/test_pathname.rb
===================================================================
--- test/pathname/test_pathname.rb	(revision 14154)
+++ test/pathname/test_pathname.rb	(revision 14155)
@@ -475,4 +475,32 @@
   def test_kernel_pathname
     assert_equal(Pathname.new("a"), Pathname("a"))
   end
+
+  def test_file_basename
+    assert_equal("bar", File.basename(Pathname.new("foo/bar")))
+  end
+
+  def test_file_dirname
+    assert_equal("foo", File.dirname(Pathname.new("foo/bar")))
+  end
+
+  def test_file_split
+    assert_equal(["foo", "bar"], File.split(Pathname.new("foo/bar")))
+  end
+
+  def test_file_extname
+    assert_equal(".baz", File.extname(Pathname.new("bar.baz")))
+  end
+
+  def test_file_fnmatch
+    assert(File.fnmatch("*.*", Pathname.new("bar.baz")))
+  end
+
+  def test_file_join
+    assert_equal("foo/bar", File.join(Pathname.new("foo"), Pathname.new("bar")))
+    lambda {
+      $SAFE = 1
+      assert_equal("foo/bar", File.join(Pathname.new("foo"), Pathname.new("bar").taint))
+    }.call
+  end
 end
Index: file.c
===================================================================
--- file.c	(revision 14154)
+++ file.c	(revision 14155)
@@ -95,13 +95,13 @@
 VALUE rb_mFileTest;
 VALUE rb_cStat;
 
-VALUE
-rb_get_path(VALUE obj)
+static VALUE
+rb_get_path_check(VALUE obj, int check)
 {
     VALUE tmp;
     static ID to_path;
 
-    rb_check_safe_obj(obj);
+    if (check) rb_check_safe_obj(obj);
     tmp = rb_check_string_type(obj);
     if (!NIL_P(tmp)) goto exit;
 
@@ -116,12 +116,24 @@
     }
   exit:
     StringValueCStr(tmp);
-    if (obj != tmp) {
+    if (check && obj != tmp) {
 	rb_check_safe_obj(tmp);
     }
     return rb_str_new4(tmp);
 }
 
+VALUE
+rb_get_path_no_checksafe(VALUE obj)
+{
+    return rb_get_path_check(obj, 0);
+}
+
+VALUE
+rb_get_path(VALUE obj)
+{
+    return rb_get_path_check(obj, 1);
+}
+
 static long
 apply2files(void (*func)(const char *, void *), VALUE vargs, void *arg)
 {
@@ -2809,7 +2821,7 @@
     if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) {
 	StringValue(fext);
     }
-    StringValue(fname);
+    FilePathStringValue(fname);
     if (RSTRING_LEN(fname) == 0 || !*(name = RSTRING_PTR(fname)))
 	return fname;
     name = skipprefix(name);
@@ -2874,6 +2886,7 @@
     const char *name, *root, *p;
     VALUE dirname;
 
+    FilePathStringValue(fname);
     name = StringValueCStr(fname);
     root = skiproot(name);
 #ifdef DOSISH_UNC
@@ -2926,6 +2939,7 @@
     char *name, *p, *e;
     VALUE extname;
 
+    FilePathStringValue(fname);
     name = StringValueCStr(fname);
     p = strrdirsep(name);	/* get the last path component */
     if (!p)
@@ -2972,7 +2986,7 @@
 static VALUE
 rb_file_s_split(VALUE klass, VALUE path)
 {
-    StringValue(path);		/* get rid of converting twice */
+    FilePathStringValue(path);		/* get rid of converting twice */
     return rb_assoc_new(rb_file_s_dirname(Qnil, path), rb_file_s_basename(1,&path));
 }
 
@@ -3027,7 +3041,7 @@
 	    }
 	    break;
 	  default:
-	    FilePathValue(tmp);
+	    FilePathStringValue(tmp);
 	}
 	name = StringValueCStr(result);
 	if (i > 0 && !NIL_P(sep)) {

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

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