mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	enum.c: respect method visibility
* enum.c (ary_inject_op): should respect method visibility, do not optimize uncallable method. [ruby-core:81349] [Bug #13592] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58871 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									da9d5db8bd
								
							
						
					
					
						commit
						5e25bfa2fb
					
				
					 2 changed files with 23 additions and 3 deletions
				
			
		
							
								
								
									
										7
									
								
								enum.c
									
										
									
									
									
								
							
							
						
						
									
										7
									
								
								enum.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -678,8 +678,9 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
 | 
			
		|||
 | 
			
		||||
    id = SYM2ID(op);
 | 
			
		||||
    if (id == idPLUS) {
 | 
			
		||||
        if ((FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) &&
 | 
			
		||||
             rb_method_basic_definition_p(rb_cInteger, idPLUS)) {
 | 
			
		||||
	if (RB_INTEGER_TYPE_P(v) &&
 | 
			
		||||
	    rb_method_basic_definition_p(rb_cInteger, idPLUS) &&
 | 
			
		||||
	    rb_obj_respond_to(v, idPLUS, FALSE)) {
 | 
			
		||||
            n = 0;
 | 
			
		||||
            for (; i < RARRAY_LEN(ary); i++) {
 | 
			
		||||
                e = RARRAY_AREF(ary, i);
 | 
			
		||||
| 
						 | 
				
			
			@ -705,7 +706,7 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
    for (; i < RARRAY_LEN(ary); i++) {
 | 
			
		||||
        v = rb_funcall(v, id, 1, RARRAY_AREF(ary, i));
 | 
			
		||||
        v = rb_funcallv_public(v, id, 1, &RARRAY_CONST_PTR(ary)[i]);
 | 
			
		||||
    }
 | 
			
		||||
    return v;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -234,6 +234,25 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
    end;
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_inject_array_op_private
 | 
			
		||||
    assert_separately([], "#{<<~"end;"}\n""end")
 | 
			
		||||
    all_assertions_foreach("", *%i[+ * / - %]) do |op|
 | 
			
		||||
      bug = '[ruby-core:81349] [Bug #13592] should respect visibility'
 | 
			
		||||
      assert_raise_with_message(NoMethodError, /private method/, bug) do
 | 
			
		||||
        begin
 | 
			
		||||
          Integer.class_eval do
 | 
			
		||||
            private op
 | 
			
		||||
          end
 | 
			
		||||
          [1,2,3].inject(op)
 | 
			
		||||
        ensure
 | 
			
		||||
          Integer.class_eval do
 | 
			
		||||
            public op
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end;
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_partition
 | 
			
		||||
    assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 })
 | 
			
		||||
    cond = ->(x, i) { x % 2 == 1 }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue