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

ruby-changes:46664

From: nobu <ko1@a...>
Date: Thu, 18 May 2017 20:29:46 +0900 (JST)
Subject: [ruby-changes:46664] nobu:r58780 (trunk): potential memory leak

nobu	2017-05-18 20:29:42 +0900 (Thu, 18 May 2017)

  New Revision: 58780

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

  Log:
    potential memory leak
    
    * dir.c (rb_dir_getwd): get rid of potential memory leak.
    
    * util.c (ruby_getcwd): ditto.

  Modified files:
    trunk/dir.c
    trunk/util.c
Index: dir.c
===================================================================
--- dir.c	(revision 58779)
+++ dir.c	(revision 58780)
@@ -1054,9 +1054,14 @@ rb_dir_getwd(void) https://github.com/ruby/ruby/blob/trunk/dir.c#L1054
     VALUE cwd;
     rb_encoding *fs = rb_filesystem_encoding();
     int fsenc = rb_enc_to_index(fs);
+    VALUE path_guard;
 
+#undef RUBY_UNTYPED_DATA_WARNING
+#define RUBY_UNTYPED_DATA_WARNING 0
     if (fsenc == ENCINDEX_US_ASCII) fsenc = ENCINDEX_ASCII;
+    path_guard = Data_Wrap_Struct((VALUE)0, NULL, RUBY_DEFAULT_FREE, NULL);
     path = my_getcwd();
+    DATA_PTR(path_guard) = path;
 #ifdef _WIN32
     cwd = rb_str_conv_enc(rb_utf8_str_new_cstr(path), NULL, fs);
 #else
@@ -1068,6 +1073,7 @@ rb_dir_getwd(void) https://github.com/ruby/ruby/blob/trunk/dir.c#L1073
 #endif
     rb_enc_associate_index(cwd, fsenc);
 #endif
+    DATA_PTR(path_guard) = 0;
 
     xfree(path);
     return cwd;
Index: util.c
===================================================================
--- util.c	(revision 58779)
+++ util.c	(revision 58780)
@@ -511,7 +511,10 @@ ruby_getcwd(void) https://github.com/ruby/ruby/blob/trunk/util.c#L511
     char *buf = xmalloc(2);
     strcpy(buf, ".");
 #elif defined HAVE_GETCWD
+# undef RUBY_UNTYPED_DATA_WARNING
+# define RUBY_UNTYPED_DATA_WARNING 0
 # if defined NO_GETCWD_MALLOC
+    VALUE guard = Data_Wrap_Struct((VALUE)0, NULL, RUBY_DEFAULT_FREE, NULL);
     int size = 200;
     char *buf = xmalloc(size);
 
@@ -519,17 +522,22 @@ ruby_getcwd(void) https://github.com/ruby/ruby/blob/trunk/util.c#L522
 	int e = errno;
 	if (e != ERANGE) {
 	    xfree(buf);
+	    DATA_PTR(guard) = NULL;
 	    rb_syserr_fail(e, "getcwd");
 	}
 	size *= 2;
+	DATA_PTR(guard) = buf;
 	buf = xrealloc(buf, size);
     }
 # else
+    VALUE guard = Data_Wrap_Struct((VALUE)0, NULL, free, NULL);
     char *buf, *cwd = getcwd(NULL, 0);
+    DATA_PTR(guard) = cwd;
     if (!cwd) rb_sys_fail("getcwd");
     buf = ruby_strdup(cwd);	/* allocate by xmalloc */
     free(cwd);
 # endif
+    DATA_PTR(RB_GC_GUARD(guard)) = NULL;
 #else
 # ifndef PATH_MAX
 #  define PATH_MAX 8192

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

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