summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-16 14:08:52 +0000
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-16 14:08:52 +0000
commit98657d4e68f2c7948a0e66a8357cd2a9db73322c (patch)
tree9b5ca4705a44a4f5da7d86655e898dcdc51d0a48
parentb65989fec7dda7d48fdfeb4e1a100181f3158326 (diff)
merge revision(s) 20287:
* string.c (rb_str_s_alloc, rb_str_replace): use null_str as well as rb_string_value so that extension libraries do not segfault. [ruby-core:19971] * string.c (rb_str_replace): reduced unnecessary malloc and copy. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@22353 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--string.c39
-rw-r--r--version.h2
3 files changed, 39 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index ef3ad1e2a2..8cf351b9ff 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Mon Feb 16 23:08:22 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * string.c (rb_str_s_alloc, rb_str_replace): use null_str as well as
+ rb_string_value so that extension libraries do not segfault.
+ [ruby-core:19971]
+
+ * string.c (rb_str_replace): reduced unnecessary malloc and copy.
+
Mon Feb 16 22:45:41 2009 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
* test/rinda/test_rinda.rb: fixed fails occasionally [ruby-dev:37119].
diff --git a/string.c b/string.c
index e0cd9c58e3..9466b4b33c 100644
--- a/string.c
+++ b/string.c
@@ -313,6 +313,7 @@ rb_obj_as_string(obj)
return str;
}
+static VALUE rb_str_s_alloc _((VALUE));
static VALUE rb_str_replace _((VALUE, VALUE));
VALUE
@@ -539,7 +540,20 @@ rb_str_associated(str)
}
static const char null_str[] = "";
-#define null_str ((char *)null_str)
+#define make_null_str(s) do { \
+ FL_SET(s, ELTS_SHARED); \
+ RSTRING(s)->ptr = (char *)null_str; \
+ RSTRING(s)->aux.shared = 0; \
+ } while (0)
+
+static VALUE
+rb_str_s_alloc(klass)
+ VALUE klass;
+{
+ VALUE str = str_alloc(klass);
+ make_null_str(str);
+ return str;
+}
VALUE
rb_string_value(ptr)
@@ -551,8 +565,7 @@ rb_string_value(ptr)
*ptr = s;
}
if (!RSTRING(s)->ptr) {
- FL_SET(s, ELTS_SHARED);
- RSTRING(s)->ptr = null_str;
+ make_null_str(s);
}
return s;
}
@@ -583,8 +596,7 @@ rb_check_string_type(str)
{
str = rb_check_convert_type(str, T_STRING, "String", "to_str");
if (!NIL_P(str) && !RSTRING(str)->ptr) {
- FL_SET(str, ELTS_SHARED);
- RSTRING(str)->ptr = null_str;
+ make_null_str(str);
}
return str;
}
@@ -2308,9 +2320,18 @@ rb_str_replace(str, str2)
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
}
else {
- rb_str_modify(str);
- rb_str_resize(str, RSTRING(str2)->len);
- memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
+ if (str_independent(str)) {
+ rb_str_resize(str, RSTRING(str2)->len);
+ memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
+ if (!RSTRING(str)->ptr) {
+ make_null_str(str);
+ }
+ }
+ else {
+ RSTRING(str)->ptr = RSTRING(str2)->ptr;
+ RSTRING(str)->len = RSTRING(str2)->len;
+ str_make_independent(str);
+ }
if (FL_TEST(str2, STR_ASSOC)) {
FL_SET(str, STR_ASSOC);
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
@@ -4908,7 +4929,7 @@ Init_String()
rb_cString = rb_define_class("String", rb_cObject);
rb_include_module(rb_cString, rb_mComparable);
rb_include_module(rb_cString, rb_mEnumerable);
- rb_define_alloc_func(rb_cString, str_alloc);
+ rb_define_alloc_func(rb_cString, rb_str_s_alloc);
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
diff --git a/version.h b/version.h
index d87b2edb61..0f7580ffdc 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@
#define RUBY_RELEASE_DATE "2009-02-16"
#define RUBY_VERSION_CODE 187
#define RUBY_RELEASE_CODE 20090216
-#define RUBY_PATCHLEVEL 120
+#define RUBY_PATCHLEVEL 121
#define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 8