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

ruby-changes:22443

From: nobu <ko1@a...>
Date: Wed, 8 Feb 2012 22:30:24 +0900 (JST)
Subject: [ruby-changes:22443] nobu:r34492 (trunk, ruby_1_9_3): * string.c (rb_str_modify_expand): fix memory leak.

nobu	2012-02-08 22:30:04 +0900 (Wed, 08 Feb 2012)

  New Revision: 34492

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

  Log:
    * string.c (rb_str_modify_expand): fix memory leak.

  Modified files:
    branches/ruby_1_9_3/ChangeLog
    branches/ruby_1_9_3/ext/-test-/string/modify.c
    branches/ruby_1_9_3/include/ruby/intern.h
    branches/ruby_1_9_3/string.c
    trunk/ChangeLog
    trunk/ext/-test-/string/modify.c
    trunk/include/ruby/intern.h
    trunk/string.c

Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 34491)
+++ include/ruby/intern.h	(revision 34492)
@@ -712,6 +712,7 @@
 VALUE rb_str_substr(VALUE, long, long);
 VALUE rb_str_subseq(VALUE, long, long);
 void rb_str_modify(VALUE);
+void rb_str_modify_expand(VALUE, long);
 VALUE rb_str_freeze(VALUE);
 void rb_str_set_len(VALUE, long);
 VALUE rb_str_resize(VALUE, long);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 34491)
+++ ChangeLog	(revision 34492)
@@ -1,3 +1,7 @@
+Wed Feb  8 22:29:59 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_modify_expand): fix memory leak.
+
 Wed Feb  8 14:06:59 2012  Hiroshi Nakamura  <nahi@r...>
 
 	* ext/openssl/ossl_ssl.c: Add SSL constants and allow to unset SSL
Index: string.c
===================================================================
--- string.c	(revision 34491)
+++ string.c	(revision 34492)
@@ -1328,12 +1328,20 @@
     if (expand < 0) {
 	rb_raise(rb_eArgError, "negative expanding string size");
     }
-    if (!str_independent(str) ||
-	(expand > 0 &&
-	 (!STR_EMBED_P(str) ||
-	  RSTRING_LEN(str) + expand > RSTRING_EMBED_LEN_MAX))) {
+    if (!str_independent(str)) {
 	str_make_independent_expand(str, expand);
     }
+    else if (expand > 0) {
+	long len = RSTRING_LEN(str);
+	long capa = len + expand;
+	if (!STR_EMBED_P(str)) {
+	    REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa+1);
+	    RSTRING(str)->as.heap.aux.capa = capa;
+	}
+	else if (capa > RSTRING_EMBED_LEN_MAX) {
+	    str_make_independent_expand(str, expand);
+	}
+    }
     ENC_CODERANGE_CLEAR(str);
 }
 
Index: ext/-test-/string/modify.c
===================================================================
--- ext/-test-/string/modify.c	(revision 34491)
+++ ext/-test-/string/modify.c	(revision 34492)
@@ -7,8 +7,16 @@
     return str;
 }
 
+VALUE
+bug_str_modify_expand(VALUE str, VALUE expand)
+{
+    rb_str_modify_expand(str, NUM2LONG(expand));
+    return str;
+}
+
 void
 Init_modify(VALUE klass)
 {
     rb_define_method(klass, "modify!", bug_str_modify, 0);
+    rb_define_method(klass, "modify_expand!", bug_str_modify_expand, 1);
 }
Index: ruby_1_9_3/include/ruby/intern.h
===================================================================
--- ruby_1_9_3/include/ruby/intern.h	(revision 34491)
+++ ruby_1_9_3/include/ruby/intern.h	(revision 34492)
@@ -691,6 +691,7 @@
 VALUE rb_str_substr(VALUE, long, long);
 VALUE rb_str_subseq(VALUE, long, long);
 void rb_str_modify(VALUE);
+void rb_str_modify_expand(VALUE, long);
 VALUE rb_str_freeze(VALUE);
 void rb_str_set_len(VALUE, long);
 VALUE rb_str_resize(VALUE, long);
Index: ruby_1_9_3/ChangeLog
===================================================================
--- ruby_1_9_3/ChangeLog	(revision 34491)
+++ ruby_1_9_3/ChangeLog	(revision 34492)
@@ -1,3 +1,7 @@
+Wed Feb  8 22:29:59 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_modify_expand): fix memory leak.
+
 Wed Feb  8 10:58:45 2012  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/readline/readline.c (readline_attempted_completion_function):
Index: ruby_1_9_3/string.c
===================================================================
--- ruby_1_9_3/string.c	(revision 34491)
+++ ruby_1_9_3/string.c	(revision 34492)
@@ -1328,12 +1328,20 @@
     if (expand < 0) {
 	rb_raise(rb_eArgError, "negative expanding string size");
     }
-    if (!str_independent(str) ||
-	(expand > 0 &&
-	 (!STR_EMBED_P(str) ||
-	  RSTRING_LEN(str) + expand > RSTRING_EMBED_LEN_MAX))) {
+    if (!str_independent(str)) {
 	str_make_independent_expand(str, expand);
     }
+    else if (expand > 0) {
+	long len = RSTRING_LEN(str);
+	long capa = len + expand;
+	if (!STR_EMBED_P(str)) {
+	    REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa+1);
+	    RSTRING(str)->as.heap.aux.capa = capa;
+	}
+	else if (capa > RSTRING_EMBED_LEN_MAX) {
+	    str_make_independent_expand(str, expand);
+	}
+    }
     ENC_CODERANGE_CLEAR(str);
 }
 
Index: ruby_1_9_3/ext/-test-/string/modify.c
===================================================================
--- ruby_1_9_3/ext/-test-/string/modify.c	(revision 34491)
+++ ruby_1_9_3/ext/-test-/string/modify.c	(revision 34492)
@@ -7,8 +7,16 @@
     return str;
 }
 
+VALUE
+bug_str_modify_expand(VALUE str, VALUE expand)
+{
+    rb_str_modify_expand(str, NUM2LONG(expand));
+    return str;
+}
+
 void
 Init_modify(VALUE klass)
 {
     rb_define_method(klass, "modify!", bug_str_modify, 0);
+    rb_define_method(klass, "modify_expand!", bug_str_modify_expand, 1);
 }

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

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