* string.c (str_byte_substr): return nil for negative length.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2011-03-02 14:20:06 +00:00
parent e1e8297a76
commit cddcffb8f9
3 changed files with 34 additions and 27 deletions

View File

@ -1,3 +1,7 @@
Wed Mar 2 23:19:56 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (str_byte_substr): return nil for negative length.
Wed Mar 2 21:15:00 2011 Tanaka Akira <akr@fsij.org>
* ext/openssl/ossl_pkey_dh.c: parenthesize macro arguments.

View File

@ -3990,35 +3990,37 @@ rb_str_setbyte(VALUE str, VALUE index, VALUE value)
static VALUE
str_byte_substr(VALUE str, long beg, long len)
{
char *p, *s = RSTRING_PTR(str), *e = s + RSTRING_LEN(str);
VALUE str2;
if (beg > RSTRING_LEN(str)) return Qnil;
if (beg < 0) {
beg += RSTRING_LEN(str);
if (beg < 0) return Qnil;
}
if (beg + len > RSTRING_LEN(str))
len = RSTRING_LEN(str) - beg;
if (len <= 0) {
len = 0;
p = 0;
}
else
p = s + beg;
char *p, *s = RSTRING_PTR(str);
long n = RSTRING_LEN(str);
VALUE str2;
if (len > RSTRING_EMBED_LEN_MAX && beg + len == RSTRING_LEN(str)) {
str2 = rb_str_new4(str);
str2 = str_new3(rb_obj_class(str2), str2);
RSTRING(str2)->as.heap.ptr += RSTRING(str2)->as.heap.len - len;
RSTRING(str2)->as.heap.len = len;
}
else {
str2 = rb_str_new5(str, p, len);
rb_enc_cr_str_copy_for_substr(str2, str);
OBJ_INFECT(str2, str);
}
if (beg > n || len < 0) return Qnil;
if (beg < 0) {
beg += n;
if (beg < 0) return Qnil;
}
if (beg + len > n)
len = n - beg;
if (len <= 0) {
len = 0;
p = 0;
}
else
p = s + beg;
return str2;
if (len > RSTRING_EMBED_LEN_MAX && beg + len == n) {
str2 = rb_str_new4(str);
str2 = str_new3(rb_obj_class(str2), str2);
RSTRING(str2)->as.heap.ptr += RSTRING(str2)->as.heap.len - len;
RSTRING(str2)->as.heap.len = len;
}
else {
str2 = rb_str_new5(str, p, len);
rb_enc_cr_str_copy_for_substr(str2, str);
OBJ_INFECT(str2, str);
}
return str2;
}
static VALUE

View File

@ -1961,6 +1961,7 @@ class TestString < Test::Unit::TestCase
assert_equal("", "hello".byteslice(5, 1))
assert_equal("o", "hello".byteslice(-1, 6))
assert_equal(nil, "hello".byteslice(-6, 1))
assert_equal(nil, "hello".byteslice(0, -1))
assert_equal("h", "hello".byteslice(0..0))
assert_equal("", "hello".byteslice(5..0))