Permalink
Browse files

Fix JRUBY-6865

JRuby allows invalid attr_accessor (MRI raises exception)

Our test for local variable names (which applies in both JRuby and
MRI to attr names) did not check for trailing ?. I added that one
case.

Note that in MRI all of this passes through ID parsing, which has
logic for checking the type of ID I did not attempt to sort out.
There are other cases we probably let through that MRI does not.

Test submitted to MRI via ruby/ruby#171
  • Loading branch information...
1 parent 709cceb commit bef3b7040c09e8d85f7042cf7360d55dbd369ed4 @headius headius committed Aug 27, 2012
Showing with 10 additions and 1 deletion.
  1. +5 −0 src/org/jruby/RubyModule.java
  2. +5 −1 src/org/jruby/util/IdUtil.java
@@ -1346,6 +1346,11 @@ private void addAccessor(ThreadContext context, String internedName, Visibility
visibility = PRIVATE;
// FIXME warning
}
+
+ if (!(IdUtil.isLocal(internedName) || IdUtil.isConstant(internedName))) {
+ throw runtime.newNameError("invalid attribute name", internedName);
+ }
+
final String variableName = ("@" + internedName).intern();
if (readable) {
addMethod(internedName, new AttrReaderMethod(this, visibility, CallConfiguration.FrameNoneScopeNone, variableName));
@@ -57,12 +57,16 @@ public static boolean isInstanceVariable(String id) {
public static boolean isGlobal(String id) {
return id.length()>0 && id.charAt(0) == '$';
}
+
+ public static boolean isPredicate(String id) {
+ return id.endsWith("?");
+ }
/**
* rb_is_local_id and is_local_id
*/
public static boolean isLocal(String id) {
- return !isGlobal(id) && !isClassVariable(id) && !isInstanceVariable(id) && !isConstant(id);
+ return !isGlobal(id) && !isClassVariable(id) && !isInstanceVariable(id) && !isConstant(id) && !isPredicate(id);
}
public static boolean isAttrSet(String id) {

0 comments on commit bef3b70

Please sign in to comment.