ruby-changes:44078
From: nobu <ko1@a...>
Date: Tue, 13 Sep 2016 21:12:02 +0900 (JST)
Subject: [ruby-changes:44078] nobu:r56151 (trunk): string.c: STR_EMBEDABLE_P
nobu 2016-09-13 21:11:57 +0900 (Tue, 13 Sep 2016) New Revision: 56151 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56151 Log: string.c: STR_EMBEDABLE_P * string.c (STR_EMBEDABLE_P): extract the predicate macro to tell if the given length is capable in an embedded string, and fix possible integer overflow. Modified files: trunk/ChangeLog trunk/string.c Index: ChangeLog =================================================================== --- ChangeLog (revision 56150) +++ ChangeLog (revision 56151) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Sep 13 21:11:56 2016 Nobuyoshi Nakada <nobu@r...> + + * string.c (STR_EMBEDABLE_P): extract the predicate macro to tell + if the given length is capable in an embedded string, and fix + possible integer overflow. + Tue Sep 13 18:37:08 2016 Koichi Sasada <ko1@a...> * test/ruby/test_exception.rb: fix thread issues. Index: string.c =================================================================== --- string.c (revision 56150) +++ string.c (revision 56151) @@ -165,6 +165,9 @@ VALUE rb_cSymbol; https://github.com/ruby/ruby/blob/trunk/string.c#L165 #define SHARABLE_SUBSTRING_P(beg, len, end) 1 #endif +#define STR_EMBEDABLE_P(len, termlen) \ + ((len) <= RSTRING_EMBED_LEN_MAX + 1 - (termlen)) + static VALUE str_replace_shared_without_enc(VALUE str2, VALUE str); static VALUE str_new_shared(VALUE klass, VALUE str); static VALUE str_new_frozen(VALUE klass, VALUE orig); @@ -1068,7 +1071,7 @@ str_replace_shared_without_enc(VALUE str https://github.com/ruby/ruby/blob/trunk/string.c#L1071 long len; RSTRING_GETMEM(str, ptr, len); - if (len+termlen <= RSTRING_EMBED_LEN_MAX+1) { + if (STR_EMBEDABLE_P(len, termlen)) { char *ptr2 = RSTRING(str2)->as.ary; STR_SET_EMBED(str2); memcpy(ptr2, RSTRING_PTR(str), len); @@ -2524,14 +2527,14 @@ rb_str_resize(VALUE str, long len) https://github.com/ruby/ruby/blob/trunk/string.c#L2527 const int termlen = TERM_LEN(str); if (STR_EMBED_P(str)) { if (len == slen) return str; - if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) { + if (STR_EMBEDABLE_P(len, termlen)) { STR_SET_EMBED_LEN(str, len); TERM_FILL(RSTRING(str)->as.ary + len, termlen); return str; } str_make_independent_expand(str, slen, len - slen, termlen); } - else if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) { + else if (STR_EMBEDABLE_P(len, termlen)) { char *ptr = STR_HEAP_PTR(str); STR_SET_EMBED(str); if (slen > len) slen = len; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/