1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

win32/file.c: use allocv buffer and API

* win32/file.c (rb_freopen): convert path name into allocv buffer
  and get rid of conversion failure in the case non-terminated
  string.  [ruby-core:69780] [Bug #11320]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51469 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-08-02 05:15:02 +00:00
parent a2b8925a94
commit e60d35e302

View file

@ -729,13 +729,18 @@ int
rb_freopen(VALUE fname, const char *mode, FILE *file)
{
WCHAR *wname, wmode[4];
VALUE wtmp;
char *name;
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)) + 1, &len);
wname[len - 1] = L'\0';
RSTRING_GETMEM(fname, name, len);
n = rb_long2int(len);
len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
wname = ALLOCV_N(WCHAR, wtmp, len + 1);
len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
wname[len] = L'\0';
RB_GC_GUARD(fname);
#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
e = _wfreopen(wname, wmode, file) ? 0 : errno;
@ -745,7 +750,7 @@ rb_freopen(VALUE fname, const char *mode, FILE *file)
e = _wfreopen_s(&newfp, wname, wmode, file);
}
#endif
free(wname);
ALLOCV_END(wtmp);
return e;
}