From 7d7aec241381d11317c16115f6592a0d3c6b7fce Mon Sep 17 00:00:00 2001 From: nagachika Date: Tue, 19 May 2015 17:23:27 +0000 Subject: merge revision(s) 49859,49861,49865: [Backport #10941] [Backport #10978] [Backport #11089] * dir.c (replace_real_basename): shouldn't create Ruby object before the object system is loaded. [ruby-core:68430] [Bug #10941] * dir.c (replace_real_basename): need to check the return value of GLOB_REALLOC(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@50545 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- dir.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'dir.c') diff --git a/dir.c b/dir.c index 0903ec8a14..c8c88a7339 100644 --- a/dir.c +++ b/dir.c @@ -1481,12 +1481,34 @@ replace_real_basename(char *path, long base, rb_encoding *enc, int norm_p) free(wplain); if (h == INVALID_HANDLE_VALUE) return path; FindClose(h); - tmp = rb_w32_conv_from_wchar(fd.cFileName, enc); - wlen = RSTRING_LEN(tmp); - path = GLOB_REALLOC(path, base + wlen + 1); - memcpy(path + base, RSTRING_PTR(tmp), wlen); - path[base + wlen] = 0; - rb_str_resize(tmp, 0); + if (tmp) { + char *buf; + tmp = rb_w32_conv_from_wchar(fd.cFileName, enc); + wlen = RSTRING_LEN(tmp); + buf = GLOB_REALLOC(path, base + wlen + 1); + if (buf) { + path = buf; + memcpy(path + base, RSTRING_PTR(tmp), wlen); + path[base + wlen] = 0; + } + rb_str_resize(tmp, 0); + } + else { + char *utf8filename; + wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL); + utf8filename = GLOB_REALLOC(0, wlen); + if (utf8filename) { + char *buf; + WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL); + buf = GLOB_REALLOC(path, base + wlen + 1); + if (buf) { + path = buf; + memcpy(path + base, utf8filename, wlen); + path[base + wlen] = 0; + } + GLOB_FREE(utf8filename); + } + } return path; } #elif USE_NAME_ON_FS == 1 -- cgit v1.2.3