From 66de9ef3496793095176fb9cfa12480d7386b379 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 22 Jun 2006 01:39:44 +0000 Subject: [PATCH] * string.c (rb_str_aref): "abc"[3] should not return an empty string but nil. [ruby-dev:28786] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10363 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ string.c | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/ChangeLog b/ChangeLog index 5bdc6cdc12..a62b4fe4b7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Jun 22 10:31:39 2006 Yukihiro Matsumoto + + * string.c (rb_str_aref): "abc"[3] should not return an empty + string but nil. [ruby-dev:28786] + Thu Jun 22 05:15:58 2006 Tanaka Akira * ext/socket/socket.c (sock_s_socketpair): try GC only once. diff --git a/string.c b/string.c index f254915f9b..136235580c 100644 --- a/string.c +++ b/string.c @@ -1478,6 +1478,12 @@ rb_str_aref(VALUE str, VALUE indx) idx = FIX2LONG(indx); num_index: + if (idx < 0) { + idx = RSTRING(str)->len + idx; + } + if (idx < 0 || RSTRING(str)->len <= idx) { + return Qnil; + } return rb_str_substr(str, idx, 1); case T_REGEXP: