ruby-changes:18972
From: nobu <ko1@a...>
Date: Wed, 2 Mar 2011 23:52:19 +0900 (JST)
Subject: [ruby-changes:18972] Ruby:r31007 (trunk): * string.c (str_byte_substr): return nil for negative length.
nobu 2011-03-02 23:20:06 +0900 (Wed, 02 Mar 2011) New Revision: 31007 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31007 Log: * string.c (str_byte_substr): return nil for negative length. Modified files: trunk/ChangeLog trunk/string.c trunk/test/ruby/test_string.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 31006) +++ ChangeLog (revision 31007) @@ -1,3 +1,7 @@ +Wed Mar 2 23:19:56 2011 Nobuyoshi Nakada <nobu@r...> + + * string.c (str_byte_substr): return nil for negative length. + Wed Mar 2 21:15:00 2011 Tanaka Akira <akr@f...> * ext/openssl/ossl_pkey_dh.c: parenthesize macro arguments. Index: string.c =================================================================== --- string.c (revision 31006) +++ string.c (revision 31007) @@ -3990,35 +3990,37 @@ 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 Index: test/ruby/test_string.rb =================================================================== --- test/ruby/test_string.rb (revision 31006) +++ test/ruby/test_string.rb (revision 31007) @@ -1961,6 +1961,7 @@ 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)) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/