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

ruby-changes:50769

From: nobu <ko1@a...>
Date: Wed, 28 Mar 2018 16:01:52 +0900 (JST)
Subject: [ruby-changes:50769] nobu:r62952 (trunk): win32/file.c: relative path with drive letter

nobu	2018-03-28 16:01:48 +0900 (Wed, 28 Mar 2018)

  New Revision: 62952

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

  Log:
    win32/file.c: relative path with drive letter
    
    * win32/file.c (IS_ABSOLUTE_PATH_P): home directory should not be
      a relative path regardless a drive letter.   PathIsRelativeW
      returns FALSE on such path.  [ruby-core:86356] [Bug #14638]

  Modified files:
    trunk/test/ruby/test_file_exhaustive.rb
    trunk/win32/file.c
Index: win32/file.c
===================================================================
--- win32/file.c	(revision 62951)
+++ win32/file.c	(revision 62952)
@@ -22,6 +22,15 @@ static struct code_page_table { https://github.com/ruby/ruby/blob/trunk/win32/file.c#L22
 
 #define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
 #define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
+static int
+IS_ABSOLUTE_PATH_P(const WCHAR *path, size_t len)
+{
+    if (len < 2) return FALSE;
+    if (ISALPHA(path[0]))
+        return len > 2 && path[1] == L':' && IS_DIR_SEPARATOR_P(path[2]);
+    else
+        return IS_DIR_UNC_P(path);
+}
 
 /* MultiByteToWideChar() doesn't work with code page 51932 */
 #define INVALID_CODE_PAGE 51932
@@ -315,7 +324,7 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/win32/file.c#L324
 	}
 	whome_len = wcslen(whome);
 
-	if (PathIsRelativeW(whome) && !(whome_len >= 2 && IS_DIR_UNC_P(whome))) {
+	if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
 	    free(wpath);
 	    xfree(whome);
 	    rb_raise(rb_eArgError, "non-absolute home");
@@ -397,7 +406,7 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/win32/file.c#L406
 	    }
 	    whome_len = wcslen(whome);
 
-	    if (PathIsRelativeW(whome) && !(whome_len >= 2 && IS_DIR_UNC_P(whome))) {
+	    if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
 		free(wpath);
 		free(wdir);
 		xfree(whome);
@@ -523,7 +532,7 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/win32/file.c#L532
     buffer_pos[0] = L'\0';
 
     /* tainted if path is relative */
-    if (!tainted && PathIsRelativeW(buffer) && !(buffer_len >= 2 && IS_DIR_UNC_P(buffer)))
+    if (!tainted && !IS_ABSOLUTE_PATH_P(buffer, buffer_len))
 	tainted = 1;
 
     /* FIXME: Make this more robust */
Index: test/ruby/test_file_exhaustive.rb
===================================================================
--- test/ruby/test_file_exhaustive.rb	(revision 62951)
+++ test/ruby/test_file_exhaustive.rb	(revision 62952)
@@ -891,6 +891,8 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L891
 
     assert_raise(ArgumentError) { File.expand_path(".", UnknownUserHome) }
     assert_nothing_raised(ArgumentError) { File.expand_path("#{DRIVE}/", UnknownUserHome) }
+    ENV["HOME"] = "#{DRIVE}UserHome"
+    assert_raise(ArgumentError) { File.expand_path("~") }
   ensure
     ENV["HOME"] = home
   end

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

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