8000 Fix JRUBY-6865 · jedld/jruby@bef3b70 · GitHub
[go: up one dir, main page]

Skip to content

Commit bef3b70

Browse files
committed
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
1 parent 709cceb commit bef3b70

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/org/jruby/RubyModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,11 @@ private void addAccessor(ThreadContext context, String internedName, Visibility
13461346
visibility = PRIVATE;
13471347
// FIXME warning
13481348
}
1349+
1350+
if (!(IdUtil.isLocal(internedName) || IdUtil.isConstant(internedName))) {
1351+
throw runtime.newNameError("invalid attribute name", internedName);
1352+
}
1353+
13491354
final String variableName = ("@" + internedName).intern();
13501355
if (readable) {
13511356
addMethod(internedName, new AttrReaderMethod(this, visibility, CallConfiguration.FrameNoneScopeNone, variableName));

src/org/jruby/util/IdUtil.java

Lines changed: 5 additions & 1 deletion
< 8FDB /tr>
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ public static boolean isInstanceVariable(String id) {
5757
public static boolean isGlobal(String id) {
5858
return id.length()>0 && id.charAt(0) == '$';
5959
}
60+
61+
public static boolean isPredicate(String id) {
62+
return id.endsWith("?");
63+
}
6064

6165
/**
6266
* rb_is_local_id and is_local_id
6367
*/
6468
public static boolean isLocal(String id) {
65-
return !isGlobal(id) && !isClassVariable(id) && !isInstanceVariable(id) && !isConstant(id);
69+
return !isGlobal(id) && !isClassVariable(id) && !isInstanceVariable(id) && !isConstant(id) && !isPredicate(id);
6670
}
6771

6872
public static boolean isAttrSet(String id) {

0 commit comments

Comments
 (0)
0