@@ -684,18 +684,14 @@ rb_str_resize(str, len)
684
684
return str ;
685
685
}
686
686
687
- VALUE
688
- rb_str_buf_cat (str , ptr , len )
687
+ static VALUE
688
+ str_buf_cat (str , ptr , len )
689
689
VALUE str ;
690
690
const char * ptr ;
691
691
long len ;
692
692
{
693
693
long capa , total ;
694
694
695
- if (len == 0 ) return str ;
696
- if (len < 0 ) {
697
- rb_raise (rb_eArgError , "negative string size (or size too big)" );
698
- }
699
695
rb_str_modify (str );
700
696
if (FL_TEST (str , STR_ASSOC )) {
701
697
FL_UNSET (str , STR_ASSOC );
@@ -704,9 +700,16 @@ rb_str_buf_cat(str, ptr, len)
704
700
else {
705
701
capa = RSTRING (str )-> aux .capa ;
706
702
}
703
+ if (RSTRING (str )-> len >= LONG_MAX - len ) {
704
+ rb_raise (rb_eArgError , "string sizes too big" );
705
+ }
707
706
total = RSTRING (str )-> len + len ;
708
707
if (capa <= total ) {
709
708
while (total > capa ) {
709
+ if (capa + 1 >= LONG_MAX / 2 ) {
710
+ capa = total ;
711
+ break ;
712
+ }
710
713
capa = (capa + 1 ) * 2 ;
711
714
}
712
715
RESIZE_CAPA (str , capa );
@@ -718,6 +721,19 @@ rb_str_buf_cat(str, ptr, len)
718
721
return str ;
719
722
}
720
723
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
+
721
737
VALUE
722
738
rb_str_buf_cat2 (str , ptr )
723
739
VALUE str ;
@@ -759,33 +775,7 @@ VALUE
759
775
rb_str_buf_append (str , str2 )
760
776
VALUE str , str2 ;
761
777
{
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 );
789
779
}
790
780
791
781
VALUE
0 commit comments