Skip to content

Commit 705ee85

Browse files
committed
merge revision(s) 17470:17472:
* array.c (rb_ary_store, rb_ary_splice): not depend on unspecified behavior at integer overflow. * string.c (str_buf_cat): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_5@17473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 6024365 commit 705ee85

File tree

3 files changed

+27
-37
lines changed

3 files changed

+27
-37
lines changed

array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ rb_ary_store(ary, idx, val)
370370
if (new_capa < ARY_DEFAULT_SIZE) {
371371
new_capa = ARY_DEFAULT_SIZE;
372372
}
373-
else if (new_capa >= ARY_MAX_SIZE - idx) {
373+
if (new_capa >= ARY_MAX_SIZE - idx) {
374374
new_capa = (ARY_MAX_SIZE - idx) / 2;
375375
}
376376
new_capa += idx;
@@ -971,10 +971,10 @@ rb_ary_splice(ary, beg, len, rpl)
971971
rb_ary_modify(ary);
972972

973973
if (beg >= RARRAY(ary)->len) {
974-
len = beg + rlen;
975-
if (len < 0 || len > ARY_MAX_SIZE) {
974+
if (beg > ARY_MAX_SIZE - rlen) {
976975
rb_raise(rb_eIndexError, "index %ld too big", beg);
977976
}
977+
len = beg + rlen;
978978
if (len >= RARRAY(ary)->aux.capa) {
979979
REALLOC_N(RARRAY(ary)->ptr, VALUE, len);
980980
RARRAY(ary)->aux.capa = len;

string.c

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -684,18 +684,14 @@ rb_str_resize(str, len)
684684
return str;
685685
}
686686

687-
VALUE
688-
rb_str_buf_cat(str, ptr, len)
687+
static VALUE
688+
str_buf_cat(str, ptr, len)
689689
VALUE str;
690690
const char *ptr;
691691
long len;
692692
{
693693
long capa, total;
694694

695-
if (len == 0) return str;
696-
if (len < 0) {
697-
rb_raise(rb_eArgError, "negative string size (or size too big)");
698-
}
699695
rb_str_modify(str);
700696
if (FL_TEST(str, STR_ASSOC)) {
701697
FL_UNSET(str, STR_ASSOC);
@@ -704,9 +700,16 @@ rb_str_buf_cat(str, ptr, len)
704700
else {
705701
capa = RSTRING(str)->aux.capa;
706702
}
703+
if (RSTRING(str)->len >= LONG_MAX - len) {
704+
rb_raise(rb_eArgError, "string sizes too big");
705+
}
707706
total = RSTRING(str)->len+len;
708707
if (capa <= total) {
709708
while (total > capa) {
709+
if (capa + 1 >= LONG_MAX / 2) {
710+
capa = total;
711+
break;
712+
}
710713
capa = (capa + 1) * 2;
711714
}
712715
RESIZE_CAPA(str, capa);
@@ -718,6 +721,19 @@ rb_str_buf_cat(str, ptr, len)
718721
return str;
719722
}
720723

724+
VALUE
725+
rb_str_buf_cat(str, ptr, len)
726+
VALUE str;
727+
const char *ptr;
728+
long len;
729+
{
730+
if (len == 0) return str;
731+
if (len < 0) {
732+
rb_raise(rb_eArgError, "negative string size (or size too big)");
733+
}
734+
return str_buf_cat(str, ptr, len);
735+
}
736+
721737
VALUE
722738
rb_str_buf_cat2(str, ptr)
723739
VALUE str;
@@ -759,33 +775,7 @@ VALUE
759775
rb_str_buf_append(str, str2)
760776
VALUE str, str2;
761777
{
762-
long capa, len;
763-
764-
rb_str_modify(str);
765-
if (FL_TEST(str, STR_ASSOC)) {
766-
FL_UNSET(str, STR_ASSOC);
767-
capa = RSTRING(str)->aux.capa = RSTRING(str)->len;
768-
}
769-
else {
770-
capa = RSTRING(str)->aux.capa;
771-
}
772-
len = RSTRING(str)->len+RSTRING(str2)->len;
773-
if (len < 0 || (capa+1) > LONG_MAX / 2) {
774-
rb_raise(rb_eArgError, "string sizes too big");
775-
}
776-
if (capa <= len) {
777-
while (len > capa) {
778-
capa = (capa + 1) * 2;
779-
}
780-
RESIZE_CAPA(str, capa);
781-
}
782-
memcpy(RSTRING(str)->ptr + RSTRING(str)->len,
783-
RSTRING(str2)->ptr, RSTRING(str2)->len);
784-
RSTRING(str)->len += RSTRING(str2)->len;
785-
RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; /* sentinel */
786-
OBJ_INFECT(str, str2);
787-
788-
return str;
778+
return str_buf_cat(str, RSTRING(str2)->ptr, RSTRING(str2)->len);
789779
}
790780

791781
VALUE

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define RUBY_RELEASE_DATE "2008-06-20"
33
#define RUBY_VERSION_CODE 185
44
#define RUBY_RELEASE_CODE 20080620
5-
#define RUBY_PATCHLEVEL 229
5+
#define RUBY_PATCHLEVEL 230
66

77
#define RUBY_VERSION_MAJOR 1
88
#define RUBY_VERSION_MINOR 8

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy