[前][次][番号順一覧][スレッド一覧]

ruby-changes:39656

From: nobu <ko1@a...>
Date: Wed, 2 Sep 2015 13:14:54 +0900 (JST)
Subject: [ruby-changes:39656] nobu:r51737 (trunk): win32: use ALLOCV

nobu	2015-09-02 13:14:42 +0900 (Wed, 02 Sep 2015)

  New Revision: 51737

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51737

  Log:
    win32: use ALLOCV
    
    * win32/file.c (rb_readlink): use ALLOCV to get rid potential
      memory leak by NoMemoryError in ALLOCV.
    
    * win32/win32.c (w32_readlink): allocate WCHAR path name and
      reparse buffer together.

  Modified files:
    trunk/win32/file.c
    trunk/win32/win32.c
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 51736)
+++ win32/win32.c	(revision 51737)
@@ -4813,27 +4813,23 @@ rb_w32_read_reparse_point(const WCHAR *p https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L4813
 static ssize_t
 w32_readlink(UINT cp, const char *path, char *buf, size_t bufsize)
 {
-    WCHAR *wpath, *wname;
     VALUE wtmp;
-    size_t size = rb_w32_reparse_buffer_size(bufsize);
-    rb_w32_reparse_buffer_t *rp = ALLOCV(wtmp, size);
-    DWORD len;
+    DWORD len = MultiByteToWideChar(cp, 0, path, -1, NULL, 0);
+    size_t size = rb_w32_reparse_buffer_size(len);
+    WCHAR *wname, *wpath = ALLOCV(wtmp, size + sizeof(WCHAR) * len);
+    rb_w32_reparse_buffer_t *rp = (void *)(wpath + len);
     ssize_t ret;
     int e;
 
-    wpath = mbstr_to_wstr(cp, path, -1, NULL);
-    if (!wpath) {
-	ALLOCV_END(wtmp);
-	return -1;
-    }
+    MultiByteToWideChar(cp, 0, path, -1, wpath, len);
     e = rb_w32_read_reparse_point(wpath, rp, size, &wname, &len);
-    free(wpath);
     if (e && e != ERROR_MORE_DATA) {
 	ALLOCV_END(wtmp);
 	errno = map_errno(e);
 	return -1;
     }
     ret = WideCharToMultiByte(cp, 0, wname, len, buf, bufsize, NULL, NULL);
+    ALLOCV_END(wtmp);
     if (e) {
 	ret = bufsize;
     }
Index: win32/file.c
===================================================================
--- win32/file.c	(revision 51736)
+++ win32/file.c	(revision 51737)
@@ -663,7 +663,7 @@ VALUE https://github.com/ruby/ruby/blob/trunk/win32/file.c#L663
 rb_readlink(VALUE path)
 {
     DWORD len;
-    VALUE wtmp = 0, str;
+    VALUE wtmp = 0, wpathbuf, str;
     rb_w32_reparse_buffer_t rbuf, *rp = &rbuf;
     WCHAR *wpath, *wbuf;
     rb_encoding *enc;
@@ -677,16 +677,17 @@ rb_readlink(VALUE path) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L677
 	path = fix_string_encoding(path, enc);
 	cp = CP_UTF8;
     }
-    wpath = mbstr_to_wstr(cp, RSTRING_PTR(path),
-			  RSTRING_LEN(path)+rb_enc_mbminlen(enc), NULL);
-    if (!wpath) rb_memerror();
+    len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), NULL, 0);
+    wpath = ALLOCV_N(WCHAR, wpathbuf, len+1);
+    MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), wpath, len);
+    wpath[len] = L'\0';
     e = rb_w32_read_reparse_point(wpath, rp, sizeof(rbuf), &wbuf, &len);
     if (e == ERROR_MORE_DATA) {
 	size_t size = rb_w32_reparse_buffer_size(len + 1);
 	rp = ALLOCV(wtmp, size);
 	e = rb_w32_read_reparse_point(wpath, rp, size, &wbuf, &len);
     }
-    free(wpath);
+    ALLOCV_END(wpathbuf);
     if (e) {
 	ALLOCV_END(wtmp);
 	rb_syserr_fail_path(rb_w32_map_errno(e), path);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]