Project

General

Profile

Feature #5072 ยป 0002-Avoid-inadvertent-symbol-creation-in-reflection-meth.patch

jeremyevans0 (Jeremy Evans), 07/22/2011 08:02 AM

View differences:

include/ruby/ruby.h
1098 1098
ID rb_intern2(const char*, long);
1099 1099
ID rb_intern_str(VALUE str);
1100 1100
const char *rb_id2name(ID);
1101
int rb_check_id(VALUE, ID*);
1101 1102
ID rb_to_id(VALUE);
1102 1103
VALUE rb_id2str(ID);
1103 1104

  
object.c
1833 1833
    else {
1834 1834
	rb_scan_args(argc, argv, "11", &name, &recur);
1835 1835
    }
1836
    id = rb_to_id(name);
1836
    if(!rb_check_id(name, &id))
1837
	return Qfalse;
1837 1838
    if (!rb_is_const_id(id)) {
1838 1839
	rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1839 1840
    }
......
1923 1924
static VALUE
1924 1925
rb_obj_ivar_defined(VALUE obj, VALUE iv)
1925 1926
{
1926
    ID id = rb_to_id(iv);
1927
    ID id;
1928
    if(!rb_check_id(iv, &id))
1929
	return Qfalse;
1927 1930

  
1928 1931
    if (!rb_is_instance_id(id)) {
1929 1932
	rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
......
2002 2005
static VALUE
2003 2006
rb_mod_cvar_defined(VALUE obj, VALUE iv)
2004 2007
{
2005
    ID id = rb_to_id(iv);
2008
    ID id;
2009
    if(!rb_check_id(iv, &id))
2010
	return Qfalse;
2006 2011

  
2007 2012
    if (!rb_is_class_id(id)) {
2008 2013
	rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
parse.y
10073 10073
    return is_junk_id(id);
10074 10074
}
10075 10075

  
10076
int
10077
rb_check_id(VALUE name, ID *id)
10078
{
10079
    VALUE tmp;
10080

  
10081
    switch (TYPE(name)) {
10082
      default:
10083
	tmp = rb_check_string_type(name);
10084
	if (NIL_P(tmp)) {
10085
	    tmp = rb_inspect(name);
10086
	    rb_raise(rb_eTypeError, "%s is not a symbol",
10087
		     RSTRING_PTR(tmp));
10088
	}
10089
	name = tmp;
10090
	/* fall through */
10091
      case T_STRING:
10092
        return st_lookup(global_symbols.sym_id, (st_data_t)name, id);
10093
      case T_SYMBOL:
10094
	*id = SYM2ID(name);
10095
	return 1;
10096
    }
10097
    return 0; /* not reached */
10098
}
10099

  
10076 10100
#endif /* !RIPPER */
10077 10101

  
10078 10102
static void
test/ruby/test_parse.rb
813 813
    assert_equal(':"foo=="', "foo==".intern.inspect)
814 814
  end
815 815

  
816
  def test_no_inadvertent_symbol_creation
817
    s = "gadzooks"
818
    {:respond_to? =>"#{s}1", :method_defined? =>"#{s}2",
819
     :public_method_defined? =>"#{s}3", :private_method_defined? =>"#{s}4",
820
     :protected_method_defined? =>"#{s}5", :const_defined? =>"A#{s}",
821
     :instance_variable_defined? =>"@#{s}", :class_variable_defined? =>"@@#{s}"
822
    }.each do |meth, str|
823
      Object.send(meth, str)
824
      assert !Symbol.all_symbols.any?{|sym| sym.to_s == str}
825
    end
826
  end
827

  
816 828
  def test_all_symbols
817 829
    x = Symbol.all_symbols
818 830
    assert_kind_of(Array, x)
vm_method.c
713 713
static VALUE
714 714
rb_mod_method_defined(VALUE mod, VALUE mid)
715 715
{
716
    if (!rb_method_boundp(mod, rb_to_id(mid), 1)) {
716
    ID id;
717
    if(!rb_check_id(mid, &id))
718
	return Qfalse;
719
    if (!rb_method_boundp(mod, id, 1)) {
717 720
	return Qfalse;
718 721
    }
719 722
    return Qtrue;
......
763 766
static VALUE
764 767
rb_mod_public_method_defined(VALUE mod, VALUE mid)
765 768
{
766
    return check_definition(mod, rb_to_id(mid), NOEX_PUBLIC);
769
    ID id;
770
    if(!rb_check_id(mid, &id))
771
	return Qfalse;
772
    return check_definition(mod, id, NOEX_PUBLIC);
767 773
}
768 774

  
769 775
/*
......
795 801
static VALUE
796 802
rb_mod_private_method_defined(VALUE mod, VALUE mid)
797 803
{
798
    return check_definition(mod, rb_to_id(mid), NOEX_PRIVATE);
804
    ID id;
805
    if(!rb_check_id(mid, &id))
806
	return Qfalse;
807
    return check_definition(mod, mid, NOEX_PRIVATE);
799 808
}
800 809

  
801 810
/*
......
827 836
static VALUE
828 837
rb_mod_protected_method_defined(VALUE mod, VALUE mid)
829 838
{
830
    return check_definition(mod, rb_to_id(mid), NOEX_PROTECTED);
839
    ID id;
840
    if(!rb_check_id(mid, &id))
841
	return Qfalse;
842
    return check_definition(mod, id, NOEX_PROTECTED);
831 843
}
832 844

  
833 845
int
......
1238 1250
    ID id;
1239 1251

  
1240 1252
    rb_scan_args(argc, argv, "11", &mid, &priv);
1241
    id = rb_to_id(mid);
1253
    if(!rb_check_id(mid, &id))
1254
	return Qfalse;
1242 1255
    if (basic_obj_respond_to(obj, id, !RTEST(priv)))
1243 1256
	return Qtrue;
1244 1257
    return Qfalse;
1245
-