aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-09-13 12:11:57 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-09-13 12:11:57 +0000
commit506e11a886d71f368f171c4bba61ca987f67b355 (patch)
tree398fa7eb0314cee06d4a77745255afe2704dd2cc
parenta5674b00bdef0c3442745d0b554f09a532f9a71a (diff)
downloadruby-506e11a886d71f368f171c4bba61ca987f67b355.tar.gz
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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--string.c9
2 files changed, 12 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 56c7c99..18cd288 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Sep 13 21:11:56 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@atdot.net>
* test/ruby/test_exception.rb: fix thread issues.
diff --git a/string.c b/string.c
index 450a559..726938c 100644
--- a/string.c
+++ b/string.c
@@ -165,6 +165,9 @@ VALUE rb_cSymbol;
#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 str2, VALUE str)
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)
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;