Skip to content

Commit 98657d4

Browse files
committed
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
1 parent b65989f commit 98657d4

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Mon Feb 16 23:08:22 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* string.c (rb_str_s_alloc, rb_str_replace): use null_str as well as
4+
rb_string_value so that extension libraries do not segfault.
5+
[ruby-core:19971]
6+
7+
* string.c (rb_str_replace): reduced unnecessary malloc and copy.
8+
19
Mon Feb 16 22:45:41 2009 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
210

311
* test/rinda/test_rinda.rb: fixed fails occasionally [ruby-dev:37119].

string.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ rb_obj_as_string(obj)
313313
return str;
314314
}
315315

316+
static VALUE rb_str_s_alloc _((VALUE));
316317
static VALUE rb_str_replace _((VALUE, VALUE));
317318

318319
VALUE
@@ -539,7 +540,20 @@ rb_str_associated(str)
539540
}
540541

541542
static const char null_str[] = "";
542-
#define null_str ((char *)null_str)
543+
#define make_null_str(s) do { \
544+
FL_SET(s, ELTS_SHARED); \
545+
RSTRING(s)->ptr = (char *)null_str; \
546+
RSTRING(s)->aux.shared = 0; \
547+
} while (0)
548+
549+
static VALUE
550+
rb_str_s_alloc(klass)
551+
VALUE klass;
552+
{
553+
VALUE str = str_alloc(klass);
554+
make_null_str(str);
555+
return str;
556+
}
543557

544558
VALUE
545559
rb_string_value(ptr)
@@ -551,8 +565,7 @@ rb_string_value(ptr)
551565
*ptr = s;
552566
}
553567
if (!RSTRING(s)->ptr) {
554-
FL_SET(s, ELTS_SHARED);
555-
RSTRING(s)->ptr = null_str;
568+
make_null_str(s);
556569
}
557570
return s;
558571
}
@@ -583,8 +596,7 @@ rb_check_string_type(str)
583596
{
584597
str = rb_check_convert_type(str, T_STRING, "String", "to_str");
585598
if (!NIL_P(str) && !RSTRING(str)->ptr) {
586-
FL_SET(str, ELTS_SHARED);
587-
RSTRING(str)->ptr = null_str;
599+
make_null_str(str);
588600
}
589601
return str;
590602
}
@@ -2308,9 +2320,18 @@ rb_str_replace(str, str2)
23082320
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
23092321
}
23102322
else {
2311-
rb_str_modify(str);
2312-
rb_str_resize(str, RSTRING(str2)->len);
2313-
memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
2323+
if (str_independent(str)) {
2324+
rb_str_resize(str, RSTRING(str2)->len);
2325+
memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
2326+
if (!RSTRING(str)->ptr) {
2327+
make_null_str(str);
2328+
}
2329+
}
2330+
else {
2331+
RSTRING(str)->ptr = RSTRING(str2)->ptr;
2332+
RSTRING(str)->len = RSTRING(str2)->len;
2333+
str_make_independent(str);
2334+
}
23142335
if (FL_TEST(str2, STR_ASSOC)) {
23152336
FL_SET(str, STR_ASSOC);
23162337
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
@@ -4908,7 +4929,7 @@ Init_String()
49084929
rb_cString = rb_define_class("String", rb_cObject);
49094930
rb_include_module(rb_cString, rb_mComparable);
49104931
rb_include_module(rb_cString, rb_mEnumerable);
4911-
rb_define_alloc_func(rb_cString, str_alloc);
4932+
rb_define_alloc_func(rb_cString, rb_str_s_alloc);
49124933
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
49134934
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
49144935
rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define RUBY_RELEASE_DATE "2009-02-16"
33
#define RUBY_VERSION_CODE 187
44
#define RUBY_RELEASE_CODE 20090216
5-
#define RUBY_PATCHLEVEL 120
5+
#define RUBY_PATCHLEVEL 121
66

77
#define RUBY_VERSION_MAJOR 1
88
#define RUBY_VERSION_MINOR 8

0 commit comments

Comments
 (0)