From a237db5cbc3e3093330c20b26a97320262ee7b16 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 5 Apr 2014 23:52:52 +0000 Subject: [PATCH] dln.c: non-ascii path on Windows * dln.c (dln_load): use wchar version to load a library in non-ascii path on Windows. based on the patch by Bugra Barin in [ruby-core:61845]. [Bug #9699] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45523 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ dln.c | 15 ++++++++++----- ext/-test-/win32/dln/empty/empty.c | 4 ++++ ext/-test-/win32/dln/empty/extconf.rb | 3 +++ test/-ext-/win32/test_dln.rb | 22 ++++++++++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 ext/-test-/win32/dln/empty/empty.c create mode 100644 ext/-test-/win32/dln/empty/extconf.rb diff --git a/ChangeLog b/ChangeLog index 9828928ce7..134ea99124 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sun Apr 6 08:52:50 2014 Bugra Barin + + * dln.c (dln_load): use wchar version to load a library in + non-ascii path on Windows. based on the patch by Bugra Barin + in [ruby-core:61845]. [Bug #9699] + Sat Apr 5 19:36:33 2014 Tadayoshi Funaba * ext/date/date_core.c (d_lite_cmp): should compare with #<. diff --git a/dln.c b/dln.c index 1f93950eaa..b3522f111c 100644 --- a/dln.c +++ b/dln.c @@ -1256,20 +1256,25 @@ dln_load(const char *file) #if defined _WIN32 && !defined __CYGWIN__ HINSTANCE handle; - char winfile[MAXPATHLEN]; + WCHAR *winfile; char message[1024]; void (*init_fct)(); char *buf; - if (strlen(file) >= MAXPATHLEN) dln_loaderror("filename too long"); - /* Load the file as an object one */ init_funcname(&buf, file); - strlcpy(winfile, file, sizeof(winfile)); + /* Convert the file path to wide char */ + winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL); + if (!winfile) { + dln_memerror(); + } /* Load file */ - if ((handle = LoadLibrary(winfile)) == NULL) { + handle = LoadLibraryW(winfile); + free(winfile); + + if (!handle) { error = dln_strerror(); goto failed; } diff --git a/ext/-test-/win32/dln/empty/empty.c b/ext/-test-/win32/dln/empty/empty.c new file mode 100644 index 0000000000..c4f94f1644 --- /dev/null +++ b/ext/-test-/win32/dln/empty/empty.c @@ -0,0 +1,4 @@ +void +Init_empty(void) +{ +} diff --git a/ext/-test-/win32/dln/empty/extconf.rb b/ext/-test-/win32/dln/empty/extconf.rb new file mode 100644 index 0000000000..a4efed90c9 --- /dev/null +++ b/ext/-test-/win32/dln/empty/extconf.rb @@ -0,0 +1,3 @@ +if $mingw or $mswin + create_makefile("-test-/win32/dln/empty") +end diff --git a/test/-ext-/win32/test_dln.rb b/test/-ext-/win32/test_dln.rb index 7c5fe61f59..40c1f533b0 100644 --- a/test/-ext-/win32/test_dln.rb +++ b/test/-ext-/win32/test_dln.rb @@ -1,4 +1,6 @@ require 'test/unit' +require 'tmpdir' +require 'rbconfig' require_relative '../../ruby/envutil' module Bug @@ -8,6 +10,26 @@ module Bug bug = '[Bug #6303]' assert_in_out_err(['-r-test-/win32/dln', '-eexit'], '', [], [], bug, timeout: 10) end + + def test_nonascii_load + bug9699 = '[ruby-core:61845] [Bug #9699]' + so = "-test-/win32/dln/empty." + RbConfig::CONFIG["DLEXT"] + so = $:.find {|d| d = File.join(d, so); break d if File.exist?(d)} + assert_not_nil(so) + Dir.mkdir(dir = File.join(testdir = Dir.mktmpdir("test"), "\u{30c6 30b9 30c8}")) + File.copy_stream(so, File.join(dir, File.basename(so))) + assert_separately(['-', bug9699, testdir, File.basename(so)], <<-'end;') + bug, dir, so = *ARGV + assert_nothing_raised(LoadError, bug) do + require File.join(dir, "\u{30c6 30b9 30c8}", so) + end + end; + ensure + File.unlink(File.join(dir, File.basename(so))) rescue nil + Dir.rmdir(dir) rescue nil + Dir.rmdir(testdir) rescue nil + end + end end end if /mswin|mingw/ =~ RUBY_PLATFORM