Skip to content

Commit 1f08d9d

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_6@22351 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 0863678 commit 1f08d9d

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-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 22:59:48 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:42:50 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: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ rb_obj_as_string(obj)
306306
return str;
307307
}
308308

309+
static VALUE rb_str_s_alloc _((VALUE));
309310
static VALUE rb_str_replace _((VALUE, VALUE));
310311

311312
VALUE
@@ -531,7 +532,21 @@ rb_str_associated(str)
531532
return Qfalse;
532533
}
533534

534-
static char *null_str = "";
535+
static const char null_str[] = "";
536+
#define make_null_str(s) do { \
537+
FL_SET(s, ELTS_SHARED); \
538+
RSTRING(s)->ptr = (char *)null_str; \
539+
RSTRING(s)->aux.shared = 0; \
540+
} while (0)
541+
542+
static VALUE
543+
rb_str_s_alloc(klass)
544+
VALUE klass;
545+
{
546+
VALUE str = str_alloc(klass);
547+
make_null_str(str);
548+
return str;
549+
}
535550

536551
VALUE
537552
rb_string_value(ptr)
@@ -543,8 +558,7 @@ rb_string_value(ptr)
543558
*ptr = s;
544559
}
545560
if (!RSTRING(s)->ptr) {
546-
FL_SET(s, ELTS_SHARED);
547-
RSTRING(s)->ptr = null_str;
561+
make_null_str(s);
548562
}
549563
return s;
550564
}
@@ -575,8 +589,7 @@ rb_check_string_type(str)
575589
{
576590
str = rb_check_convert_type(str, T_STRING, "String", "to_str");
577591
if (!NIL_P(str) && !RSTRING(str)->ptr) {
578-
FL_SET(str, ELTS_SHARED);
579-
RSTRING(str)->ptr = null_str;
592+
make_null_str(str);
580593
}
581594
return str;
582595
}
@@ -2256,9 +2269,18 @@ rb_str_replace(str, str2)
22562269
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
22572270
}
22582271
else {
2259-
rb_str_modify(str);
2260-
rb_str_resize(str, RSTRING(str2)->len);
2261-
memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
2272+
if (str_independent(str)) {
2273+
rb_str_resize(str, RSTRING(str2)->len);
2274+
memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
2275+
if (!RSTRING(str)->ptr) {
2276+
make_null_str(str);
2277+
}
2278+
}
2279+
else {
2280+
RSTRING(str)->ptr = RSTRING(str2)->ptr;
2281+
RSTRING(str)->len = RSTRING(str2)->len;
2282+
str_make_independent(str);
2283+
}
22622284
if (FL_TEST(str2, STR_ASSOC)) {
22632285
FL_SET(str, STR_ASSOC);
22642286
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
@@ -4642,7 +4664,7 @@ Init_String()
46424664
rb_cString = rb_define_class("String", rb_cObject);
46434665
rb_include_module(rb_cString, rb_mComparable);
46444666
rb_include_module(rb_cString, rb_mEnumerable);
4645-
rb_define_alloc_func(rb_cString, str_alloc);
4667+
rb_define_alloc_func(rb_cString, rb_str_s_alloc);
46464668
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
46474669
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
46484670
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 186
44
#define RUBY_RELEASE_CODE 20090216
5-
#define RUBY_PATCHLEVEL 334
5+
#define RUBY_PATCHLEVEL 335
66

77
#define RUBY_VERSION_MAJOR 1
88
#define RUBY_VERSION_MINOR 8

0 commit comments

Comments
 (0)