Project

General

Profile

Feature #5079 ยป 0001-Remove-more-inadvertent-symbol-creation.patch

jeremyevans0 (Jeremy Evans), 07/23/2011 04:43 AM

View differences:

error.c
812 812
    rb_exc_raise(exc);
813 813
}
814 814

  
815
void
816
rb_name_error_str(VALUE str, const char *fmt, ...)
817
{
818
    VALUE exc, argv[2];
819
    va_list args;
820

  
821
    va_start(args, fmt);
822
    argv[0] = rb_vsprintf(fmt, args);
823
    va_end(args);
824

  
825
    argv[1] = str;
826
    exc = rb_class_new_instance(2, argv, rb_eNameError);
827
    rb_exc_raise(exc);
828
}
829

  
815 830
/*
816 831
 * call-seq:
817 832
 *   NameError.new(msg [, name])  -> name_error
include/ruby/intern.h
209 209
VALUE rb_exc_new3(VALUE, VALUE);
210 210
PRINTF_ARGS(NORETURN(void rb_loaderror(const char*, ...)), 1, 2);
211 211
PRINTF_ARGS(NORETURN(void rb_name_error(ID, const char*, ...)), 2, 3);
212
PRINTF_ARGS(NORETURN(void rb_name_error_str(VALUE, const char*, ...)), 2, 3);
212 213
NORETURN(void rb_invalid_str(const char*, const char*));
213 214
PRINTF_ARGS(void rb_compile_error(const char*, int, const char*, ...), 3, 4);
214 215
PRINTF_ARGS(void rb_compile_error_with_enc(const char*, int, void *, const char*, ...), 4, 5);
object.c
1833 1833
    else {
1834 1834
	rb_scan_args(argc, argv, "11", &name, &recur);
1835 1835
    }
1836
    if (!(id = rb_check_id(name)) && rb_is_const_name(name))
1837
	return Qfalse;
1836
    if (!(id = rb_check_id(name))) {
1837
	if(rb_is_const_name(name)) {
1838
	    return Qfalse;
1839
	} else {
1840
	    rb_name_error_str(name, "wrong constant name %s", RSTRING_PTR(name));
1841
	}
1842
    }
1838 1843
    if (!rb_is_const_id(id)) {
1839 1844
	rb_name_error(id, "wrong constant name %s", rb_id2name(id));
1840 1845
    }
......
1864 1869
static VALUE
1865 1870
rb_obj_ivar_get(VALUE obj, VALUE iv)
1866 1871
{
1867
    ID id = rb_to_id(iv);
1872
    ID id = rb_check_id(iv);
1868 1873

  
1874
    if (!id) {
1875
	if(rb_is_instance_name(iv)) {
1876
	    return Qnil;
1877
	} else {
1878
	    rb_name_error_str(iv, "`%s' is not allowed as an instance variable name", RSTRING_PTR(iv));
1879
	}
1880
    }
1869 1881
    if (!rb_is_instance_id(id)) {
1870 1882
	rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1871 1883
    }
......
1926 1938
{
1927 1939
    ID id = rb_check_id(iv);
1928 1940

  
1929
    if (!id && rb_is_instance_name(iv)) return Qfalse;
1941
    if (!id) {
1942
	if(rb_is_instance_name(iv)) {
1943
	    return Qfalse;
1944
	} else {
1945
	    rb_name_error_str(iv, "`%s' is not allowed as an instance variable name", RSTRING_PTR(iv));
1946
	}
1947
    }
1930 1948
    if (!rb_is_instance_id(id)) {
1931 1949
	rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1932 1950
    }
......
1950 1968
static VALUE
1951 1969
rb_mod_cvar_get(VALUE obj, VALUE iv)
1952 1970
{
1953
    ID id = rb_to_id(iv);
1971
    ID id = rb_check_id(iv);
1954 1972

  
1973
    if (!id) {
1974
	if(rb_is_class_name(iv)) {
1975
	    rb_name_error_str(iv, "uninitialized class variable %s in %s",
1976
	                      RSTRING_PTR(iv), rb_class2name(obj));
1977
	} else {
1978
	    rb_name_error_str(iv, "`%s' is not allowed as a class variable name", RSTRING_PTR(iv));
1979
	}
1980
    }
1955 1981
    if (!rb_is_class_id(id)) {
1956 1982
	rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
1957 1983
    }
......
2006 2032
{
2007 2033
    ID id = rb_check_id(iv);
2008 2034

  
2009
    if (!id && rb_is_class_name(iv)) return Qfalse;
2035
    if (!id) {
2036
	if(rb_is_class_name(iv)) {
2037
	    return Qfalse;
2038
	} else {
2039
	    rb_name_error_str(iv, "`%s' is not allowed as a class variable name", RSTRING_PTR(iv));
2040
	}
2041
    }
2010 2042
    if (!rb_is_class_id(id)) {
2011 2043
	rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
2012 2044
    }
proc.c
28 28

  
29 29
static VALUE bmcall(VALUE, VALUE);
30 30
static int method_arity(VALUE);
31
static ID attached;
31 32

  
32 33
/* Proc */
33 34

  
......
1139 1140
    return data->me.klass;
1140 1141
}
1141 1142

  
1143
static void
1144
rb_method_name_error(VALUE klass, VALUE str) {
1145
    const char *s0 = " class";
1146
    VALUE c = klass;
1147

  
1148
    if (FL_TEST(c, FL_SINGLETON)) {
1149
	VALUE obj = rb_ivar_get(klass, attached);
1150

  
1151
	switch (TYPE(obj)) {
1152
	  case T_MODULE:
1153
	  case T_CLASS:
1154
	    c = obj;
1155
	    s0 = "";
1156
	}
1157
    }
1158
    else if (TYPE(c) == T_MODULE) {
1159
	s0 = " module";
1160
    }
1161
    rb_name_error_str(str, "undefined method `%s' for%s `%s'",
1162
		  RSTRING_PTR(str), s0, rb_class2name(c));
1163
}
1164

  
1142 1165
/*
1143 1166
 *  call-seq:
1144 1167
 *     obj.method(sym)    -> method
......
1170 1193
VALUE
1171 1194
rb_obj_method(VALUE obj, VALUE vid)
1172 1195
{
1173
    return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, FALSE);
1196
    ID id = rb_check_id(vid);
1197
    if (!id) {
1198
	rb_method_name_error(CLASS_OF(obj), vid);
1199
    }
1200
    return mnew(CLASS_OF(obj), obj, id, rb_cMethod, FALSE);
1174 1201
}
1175 1202

  
1176 1203
/*
......
1183 1210
VALUE
1184 1211
rb_obj_public_method(VALUE obj, VALUE vid)
1185 1212
{
1186
    return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, TRUE);
1213
    ID id = rb_check_id(vid);
1214
    if (!id) {
1215
	rb_method_name_error(CLASS_OF(obj), vid);
1216
    }
1217
    return mnew(CLASS_OF(obj), obj, id, rb_cMethod, TRUE);
1187 1218
}
1188 1219

  
1189 1220
/*
......
1220 1251
static VALUE
1221 1252
rb_mod_instance_method(VALUE mod, VALUE vid)
1222 1253
{
1223
    return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, FALSE);
1254
    ID id = rb_check_id(vid);
1255
    if (!id) {
1256
	rb_method_name_error(mod, vid);
1257
    }
1258
    return mnew(mod, Qundef, id, rb_cUnboundMethod, FALSE);
1224 1259
}
1225 1260

  
1226 1261
/*
......
1233 1268
static VALUE
1234 1269
rb_mod_public_instance_method(VALUE mod, VALUE vid)
1235 1270
{
1236
    return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, TRUE);
1271
    ID id = rb_check_id(vid);
1272
    if (!id) {
1273
	rb_method_name_error(mod, vid);
1274
    }
1275
    return mnew(mod, Qundef, id, rb_cUnboundMethod, TRUE);
1237 1276
}
1238 1277

  
1239 1278
/*
......
2234 2273
    rb_define_method(rb_cBinding, "dup", binding_dup, 0);
2235 2274
    rb_define_method(rb_cBinding, "eval", bind_eval, -1);
2236 2275
    rb_define_global_function("binding", rb_f_binding, 0);
2276
    attached = rb_intern("__attached__");
2237 2277
}
2238 2278

  
test/ruby/test_symbol.rb
159 159
      assert !Symbol.all_symbols.any? {|sym| sym.to_s == str}, msg
160 160
    end
161 161
  end
162

  
163
  def test_no_inadvertent_symbol_creation2
164
    feature50XX = '[ruby-core:383XX]'
165
    c = Class.new
166
    s = "gadzoooks"
167
    {:instance_variable_get => ["@#{s}1", nil],
168
     :class_variable_get => ["@@#{s}1", NameError],
169
     :remove_instance_variable => ["@#{s}2", NameError],
170
     :remove_class_variable => ["@@#{s}2", NameError],
171
     :remove_const => ["A#{s}", NameError],
172
     :method => ["#{s}1", NameError],
173
     :public_method => ["#{s}2", NameError],
174
     :instance_method => ["#{s}3", NameError],
175
     :public_instance_method => ["#{s}4", NameError],
176
    }.each do |meth, arr|
177
      str, ret = arr
178
      msg = "#{meth}(#{str}) #{feature50XX}"
179
      if ret.is_a?(Class) && (ret < Exception)
180
        assert_raises(ret){c.send(meth, str)}
181
      else
182
        assert(c.send(meth, str) == ret, msg)
183
      end
184
      assert !Symbol.all_symbols.any? {|sym| sym.to_s == str}, msg
185
    end
186
  end
162 187
end
variable.c
1299 1299
rb_obj_remove_instance_variable(VALUE obj, VALUE name)
1300 1300
{
1301 1301
    VALUE val = Qnil;
1302
    const ID id = rb_to_id(name);
1302
    const ID id = rb_check_id(name);
1303 1303
    st_data_t n, v;
1304 1304
    struct st_table *iv_index_tbl;
1305 1305
    st_data_t index;
......
1307 1307
    if (!OBJ_UNTRUSTED(obj) && rb_safe_level() >= 4)
1308 1308
	rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable");
1309 1309
    rb_check_frozen(obj);
1310
    if (!id) {
1311
	if (rb_is_instance_name(name)) {
1312
	    rb_name_error_str(name, "instance variable %s not defined", RSTRING_PTR(name));
1313
	} else {
1314
	    rb_name_error_str(name, "`%s' is not allowed as an instance variable name", RSTRING_PTR(name));
1315
	}
1316
    }
1310 1317
    if (!rb_is_instance_id(id)) {
1311 1318
	rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
1312 1319
    }
......
1677 1684
VALUE
1678 1685
rb_mod_remove_const(VALUE mod, VALUE name)
1679 1686
{
1680
    const ID id = rb_to_id(name);
1687
    const ID id = rb_check_id(name);
1681 1688

  
1689
    if (!id) {
1690
	if (rb_is_const_name(name)) {
1691
	    rb_name_error_str(name, "constant %s::%s not defined",
1692
	                      rb_class2name(mod), RSTRING_PTR(name));
1693
	} else {
1694
	    rb_name_error_str(name, "`%s' is not allowed as a constant name", RSTRING_PTR(name));
1695
	}
1696
    }
1682 1697
    if (!rb_is_const_id(id)) {
1683 1698
	rb_name_error(id, "`%s' is not allowed as a constant name", rb_id2name(id));
1684 1699
    }
......
2189 2204
VALUE
2190 2205
rb_mod_remove_cvar(VALUE mod, VALUE name)
2191 2206
{
2192
    const ID id = rb_to_id(name);
2207
    const ID id = rb_check_id(name);
2193 2208
    st_data_t val, n = id;
2194 2209

  
2210
    if (!id) {
2211
	if (rb_is_class_name(name)) {
2212
	    rb_name_error_str(name, "class variable %s not defined for %s",
2213
	                      RSTRING_PTR(name), rb_class2name(mod));
2214
	} else {
2215
	    rb_name_error_str(name, "wrong class variable name %s", RSTRING_PTR(name));
2216
	}
2217
    }
2195 2218
    if (!rb_is_class_id(id)) {
2196 2219
	rb_name_error(id, "wrong class variable name %s", rb_id2name(id));
2197 2220
    }
2198
-