@@ -881,7 +881,8 @@ private IRubyObject stepCommon(ThreadContext context, IRubyObject step, Block bl
881881 private void fixnumEndlessStep (ThreadContext context , IRubyObject step , Block block ) {
882882 long i = begin .convertToInteger ().getLongValue ();
883883 long unit = step .convertToInteger ().getLongValue ();
884- while (i < Long .MAX_VALUE ) {
884+ // avoid overflow
885+ while (i <= Long .MAX_VALUE - unit ) {
885886 block .yield (context , asFixnum (context , i ));
886887 i += unit ;
887888 }
@@ -894,22 +895,26 @@ private void fixnumEndlessStep(ThreadContext context, IRubyObject step, Block bl
894895 private void fixnumStep (ThreadContext context , long step , Block block ) {
895896 long end = fix2long (this .end );
896897 long i , unit = step ;
898+ // avoid overflow
899+ long shortEnd = end - unit ;
897900 if (unit < 0 ) {
898- if (!isExclusive )
899- end -= 1 ;
900901 i = fix2long (begin );
901- while (i > end ) {
902- block . yield ( context , asFixnum ( context , i ));
902+ if (i > end ) block . yield ( context , asFixnum ( context , i ));
903+ while ( i > shortEnd ) {
903904 i += unit ;
905+ block .yield (context , asFixnum (context , i ));
904906 }
907+ if (!isExclusive && i == shortEnd )
908+ block .yield (context , asFixnum (context , i + unit ));
905909 } else {
906- if (!isExclusive )
907- end += 1 ;
908910 i = fix2long (begin );
909- while (i < end ) {
910- block . yield ( context , asFixnum ( context , i ));
911+ if (i < end ) block . yield ( context , asFixnum ( context , i ));
912+ while ( i < shortEnd ) {
911913 i += unit ;
914+ block .yield (context , asFixnum (context , i ));
912915 }
916+ if (!isExclusive && i == shortEnd )
917+ block .yield (context , asFixnum (context , i + unit ));
913918 }
914919 }
915920
0 commit comments