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:
parent
a2b8925a94
commit
e60d35e302
1 changed files with 9 additions and 4 deletions
13
win32/file.c
13
win32/file.c
|
@ -729,13 +729,18 @@ int
|
||||||
rb_freopen(VALUE fname, const char *mode, FILE *file)
|
rb_freopen(VALUE fname, const char *mode, FILE *file)
|
||||||
{
|
{
|
||||||
WCHAR *wname, wmode[4];
|
WCHAR *wname, wmode[4];
|
||||||
|
VALUE wtmp;
|
||||||
|
char *name;
|
||||||
long len;
|
long len;
|
||||||
int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
|
int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
|
||||||
if (n > numberof(wmode)) return EINVAL;
|
if (n > numberof(wmode)) return EINVAL;
|
||||||
MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
|
MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
|
||||||
wname = rb_w32_mbstr_to_wstr(CP_UTF8, RSTRING_PTR(fname),
|
RSTRING_GETMEM(fname, name, len);
|
||||||
rb_long2int(RSTRING_LEN(fname)) + 1, &len);
|
n = rb_long2int(len);
|
||||||
wname[len - 1] = L'\0';
|
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);
|
RB_GC_GUARD(fname);
|
||||||
#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
|
#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
|
||||||
e = _wfreopen(wname, wmode, file) ? 0 : errno;
|
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);
|
e = _wfreopen_s(&newfp, wname, wmode, file);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
free(wname);
|
ALLOCV_END(wtmp);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue