diff --git a/ChangeLog b/ChangeLog index 28123d8f7f..2e60021779 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Tue Jun 30 18:38:16 2015 NAKAMURA Usaku + + * win32/file.c (rb_freopen): need to terminate by NUL. + Tue Jun 30 17:28:25 2015 Nobuyoshi Nakada * io.c (rb_io_reopen): freopen(3) with OS encoding path. diff --git a/win32/file.c b/win32/file.c index 799810a4ef..99d94fef46 100644 --- a/win32/file.c +++ b/win32/file.c @@ -725,11 +725,13 @@ int rb_freopen(VALUE fname, const char *mode, FILE *file) { WCHAR *wname, wmode[4]; + long len; int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0); if (n > numberof(wmode)) return EINVAL; MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode)); wname = rb_w32_mbstr_to_wstr(CP_UTF8, RSTRING_PTR(fname), - rb_long2int(RSTRING_LEN(fname)), NULL); + rb_long2int(RSTRING_LEN(fname)) + 1, &len); + wname[len - 1] = L'\0'; RB_GC_GUARD(fname); #if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S) e = _wfreopen(wname, wmode, file) ? 0 : errno; @@ -737,6 +739,7 @@ rb_freopen(VALUE fname, const char *mode, FILE *file) { FILE *newfp = 0; e = _wfreopen_s(&newfp, wname, wmode, file); + if (e != 0) cprintf("DEBUG: %d [%ls] [%ls]\n", e, wname, wmode); } #endif xfree(wname);