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

ruby-changes:36279

From: nobu <ko1@a...>
Date: Mon, 10 Nov 2014 17:24:56 +0900 (JST)
Subject: [ruby-changes:36279] nobu:r48360 (trunk): win32.c: login name encoding

nobu	2014-11-10 17:24:36 +0900 (Mon, 10 Nov 2014)

  New Revision: 48360

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

  Log:
    win32.c: login name encoding
    
    * ext/etc/etc.c (etc_getlogin): set login name encoding properly.
      [ruby-core:66163] [Bug #10493]

  Modified files:
    trunk/ChangeLog
    trunk/ext/etc/etc.c
    trunk/test/etc/test_etc.rb
    trunk/win32/win32.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48359)
+++ ChangeLog	(revision 48360)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Nov 10 17:24:34 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/etc/etc.c (etc_getlogin): set login name encoding properly.
+	  [ruby-core:66163] [Bug #10493]
+
 Mon Nov 10 16:20:42 2014  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* lib/rubygems/*.rb: Update to RubyGems master(3e36528).
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 48359)
+++ win32/win32.c	(revision 48360)
@@ -546,11 +546,12 @@ init_env(void) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L546
 	if (!GetEnvironmentVariableW(L"USERNAME", env, numberof(env)) &&
 	    !GetUserNameW(env, (len = numberof(env), &len))) {
 	    NTLoginName = "<Unknown>";
-	    return;
 	}
-	set_env_val(L"USER");
+	else {
+	    set_env_val(L"USER");
+	    NTLoginName = rb_w32_wstr_to_mbstr(CP_UTF8, env, -1, NULL);
+	}
     }
-    NTLoginName = strdup(rb_w32_getenv("USER"));
 
     if (!GetEnvironmentVariableW(TMPDIR, env, numberof(env)) &&
 	!GetEnvironmentVariableW(L"TMP", env, numberof(env)) &&
Index: ext/etc/etc.c
===================================================================
--- ext/etc/etc.c	(revision 48359)
+++ ext/etc/etc.c	(revision 48360)
@@ -81,8 +81,15 @@ etc_getlogin(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L81
     login = getenv("USER");
 #endif
 
-    if (login)
-	return rb_tainted_str_new2(login);
+    if (login) {
+#ifdef _WIN32
+	rb_encoding *extenc = rb_utf8_encoding();
+#else
+	rb_encoding *extenc = rb_locale_encoding();
+#endif
+	return rb_external_str_new_with_enc(login, strlen(login), extenc);
+    }
+
     return Qnil;
 }
 
Index: test/etc/test_etc.rb
===================================================================
--- test/etc/test_etc.rb	(revision 48359)
+++ test/etc/test_etc.rb	(revision 48360)
@@ -4,7 +4,9 @@ require "etc" https://github.com/ruby/ruby/blob/trunk/test/etc/test_etc.rb#L4
 class TestEtc < Test::Unit::TestCase
   def test_getlogin
     s = Etc.getlogin
-    assert(s.is_a?(String) || s == nil, "getlogin must return a String or nil")
+    return if s == nil
+    assert(s.is_a?(String), "getlogin must return a String or nil")
+    assert_predicate(s, :valid_encoding?, "login name should be a valid string")
   end
 
   def test_passwd

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

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