Bug #11718
closedConstant access on `nil`
Description
It is possible to access top-level constants by doing nil::CONSTANT
, which looks like a bug according to Matz. Here are a couple of examples:
$ ruby -ve "Foo = 123; p nil::Foo"
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
123
$ ruby -ve "class A; Foo = 456; end; p nil::A::Foo"
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
456
Thanks!
Files
Updated by Hanmac (Hans Mackowiak) about 9 years ago
hm it might be that nil::Foo
is parsed as ::Foo
which is doing the top-level access.
but i am not deep enough in the parser to fix that.
Updated by Eregon (Benoit Daloze) about 9 years ago
I think this should really raise an error like:
$ ruby -e 'p nil::String'
-e:1:in `<main>': nil is not a class/module (TypeError)
Updated by Hanmac (Hans Mackowiak) about 9 years ago
yeah that should raise an error. A little test shows that this exist a while way back:
p nil::String
for 1.9.3:
String
for 1.8.7:
# is not a class/module (TypeError)
Updated by elia (Elia Schito) almost 9 years ago
looks like it's somehow intended behavior
https://github.com/ruby/ruby/blob/trunk/insns.def#L179-L190
/**
@c variable
@e
Get constant variable id. If klass is Qnil, constants
are searched in the current scope. If klass is Qfalse, constants
are searched as top level constants. Otherwise, get constant under klass
class or module.
@j 定数 id の値を得る。
klass が Qnil なら、そのスコープで得られる定数の値を得る。
Qfalse なら、トップレベルスコープを得る。
それ以外なら、klass クラスの下の定数を得る。
*/
apparently introduced here: https://github.com/ruby/ruby/commit/d84f9b16946bca06ce0557ebe99152d7d445c9ec to resolve bug #10943
Updated by Eregon (Benoit Daloze) almost 9 years ago
I don't think it's intended, that commit should not introduce new behavior except on singleton class constants.
@ko1 (Koichi Sasada): Could you confirm this is a bug?
Updated by ko1 (Koichi Sasada) almost 9 years ago
This is a (intentional) bug. Because I expect nobody reveal such code :p
Updated by ko1 (Koichi Sasada) almost 9 years ago
- Assignee set to ko1 (Koichi Sasada)
Updated by ko1 (Koichi Sasada) almost 9 years ago
- This is a bug, so that anyone should not rely on this behavior.
- This is a bug, so that this bug should be fixed
- But not critical, so 2.3 can remain this behavior. It should be fix in a future.
Updated by Hanmac (Hans Mackowiak) almost 9 years ago
@Koichi: good that you classify it, for a moment i thought that might be an feature ;P
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
This bug is still present in the master branch. Attached is a patch that fixes it. It adds a second argument to the getconstant instruction. It would probably be better for performance to add a separate instruction for the case where this is needed, but I'm not sure it is worth it. With the patch:
nil::Object
# TypeError (nil is not a class/module)
Updated by ko1 (Koichi Sasada) over 5 years ago
:+1: great patch! could you commit it?
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
I think it can be an operand of getconst
than an argument on the stack, while it is always a constant.
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
nobu: I agree, making it an operand instead of a stack argument makes more sense. I'm currently testing a patch for that and will commit if it passes.
Updated by jeremyevans (Jeremy Evans) over 5 years ago
- Status changed from Open to Closed
Applied in changeset git|fbcd0652944568c43a6ae427960d909d62ce6a8d.
Remove support for nil::Constant
This was an intentional bug added in 1.9.
The approach taken here is to add a second operand to the
getconstant instruction for whether nil should be allowed and
treated as current scope.
Fixes [Bug #11718]
Updated by ko1 (Koichi Sasada) over 5 years ago
I think it can be an operand of getconst than an argument on the stack, while it is always a constant.
No. It should not be an new operand because we need to change tools which depends on current bytecode.