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

Skip to content

Commit 254d8e5

Browse files
headiusdekellum
authored andcommitted
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 57b4637 commit 254d8e5

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
@@ -1307,6 +1307,11 @@ private void addAccessor(ThreadContext context, String internedName, Visibility
13071307
visibility = PRIVATE;
13081308
// FIXME warning
13091309
}
1310+
1311+
if (!(IdUtil.isLocal(internedName) || IdUtil.isConstant(internedName))) {
1312+
throw runtime.newNameError("invalid attribute name", internedName);
1313+
}
1314+
13101315
final String variableName = ("@" + internedName).intern();
13111316
if (readable) {
13121317
addMethod(internedName, new JavaMethodZero(this, visibility, CallConfiguration.FrameNoneScopeNone) {

src/org/jruby/util/IdUtil.java

Lines changed: 5 additions & 1 deletion
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