mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	range.c: modify check
* range.c (range_initialize_copy): disallow to modify after initialized. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43433 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									2c6518c37d
								
							
						
					
					
						commit
						64f2b9f990
					
				
					 3 changed files with 25 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,8 @@
 | 
			
		|||
Sat Oct 26 19:08:00 2013  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* range.c (range_initialize_copy): disallow to modify after
 | 
			
		||||
	  initialized.
 | 
			
		||||
 | 
			
		||||
Sat Oct 26 17:48:54 2013  Tanaka Akira  <akr@fsij.org>
 | 
			
		||||
 | 
			
		||||
	* lib/open-uri.rb (meta_add_field): : Re-implemented.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										24
									
								
								range.c
									
										
									
									
									
								
							
							
						
						
									
										24
									
								
								range.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -82,6 +82,15 @@ rb_range_new(VALUE beg, VALUE end, int exclude_end)
 | 
			
		|||
    return range;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
range_modify(VALUE range)
 | 
			
		||||
{
 | 
			
		||||
    /* Ranges are immutable, so that they should be initialized only once. */
 | 
			
		||||
    if (RANGE_EXCL(range) != Qnil) {
 | 
			
		||||
	rb_name_error(idInitialize, "`initialize' called twice");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  call-seq:
 | 
			
		||||
 *     Range.new(begin, end, exclude_end=false)    -> rng
 | 
			
		||||
| 
						 | 
				
			
			@ -97,15 +106,19 @@ range_initialize(int argc, VALUE *argv, VALUE range)
 | 
			
		|||
    VALUE beg, end, flags;
 | 
			
		||||
 | 
			
		||||
    rb_scan_args(argc, argv, "21", &beg, &end, &flags);
 | 
			
		||||
    /* Ranges are immutable, so that they should be initialized only once. */
 | 
			
		||||
    if (RANGE_EXCL(range) != Qnil) {
 | 
			
		||||
	rb_name_error(idInitialize, "`initialize' called twice");
 | 
			
		||||
    }
 | 
			
		||||
    range_modify(range);
 | 
			
		||||
    range_init(range, beg, end, RBOOL(RTEST(flags)));
 | 
			
		||||
    return Qnil;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define range_initialize_copy rb_struct_init_copy /* :nodoc: */
 | 
			
		||||
/* :nodoc: */
 | 
			
		||||
static VALUE
 | 
			
		||||
range_initialize_copy(VALUE range, VALUE orig)
 | 
			
		||||
{
 | 
			
		||||
    range_modify(range);
 | 
			
		||||
    rb_struct_init_copy(range, orig);
 | 
			
		||||
    return range;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  call-seq:
 | 
			
		||||
| 
						 | 
				
			
			@ -1244,6 +1257,7 @@ range_loader(VALUE range, VALUE obj)
 | 
			
		|||
        rb_raise(rb_eTypeError, "not a dumped range object");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    range_modify(range);
 | 
			
		||||
    RANGE_SET_BEG(range, rb_ivar_get(obj, id_beg));
 | 
			
		||||
    RANGE_SET_END(range, rb_ivar_get(obj, id_end));
 | 
			
		||||
    RANGE_SET_EXCL(range, rb_ivar_get(obj, id_excl));
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -82,6 +82,7 @@ class TestRange < Test::Unit::TestCase
 | 
			
		|||
  def test_initialize_twice
 | 
			
		||||
    r = eval("1..2")
 | 
			
		||||
    assert_raise(NameError) { r.instance_eval { initialize 3, 4 } }
 | 
			
		||||
    assert_raise(NameError) { r.instance_eval { initialize_copy 3..4 } }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_uninitialized_range
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue