Bug #20468
closedSegfault on safe navigation in for target
Description
for foo&.bar in []; end
Updated by nobu (Nobuyoshi Nakada) 7 months ago
I can't imagine what it should do when foo == nil
.
Should be a syntax error probably?
Updated by nobu (Nobuyoshi Nakada) 7 months ago
Hmmm, may be possible to interpret that code like as foo&.then {|recv| for recv.bar in []; end}
.
Just an idea.
Updated by nobu (Nobuyoshi Nakada) 7 months ago
- Assignee set to matz (Yukihiro Matsumoto)
Updated by nobu (Nobuyoshi Nakada) 7 months ago
- Assignee deleted (
matz (Yukihiro Matsumoto))
I found that a for
loop variable is looser than I expected.
Not only a safe navigation call, even a constant can be used.
I don't think a constant is useful as a loop variable that is expected to be re-assigned.
Updated by Eregon (Benoit Daloze) 7 months ago
Given for foo.bar in []; end
is valid and does for.bar = element
,
I think for foo&.bar in []; end
could simply do for&.bar = element
which is the same as for.bar = element unless foo.nil?
(NIL_P(foo)
to be precise).
Changing the syntax for for
variables sounds more risky and likely to unexpectedly disallow some new lhs
forms when the lhs
rule is updated, and the for_var
rule is forgotten to be updated too.
Updated by Eregon (Benoit Daloze) 7 months ago
IOW I think the segfault could be fixed here without changing what is valid syntax.
And given this segfault probably affects older versions too there it seems better to fix the segfault than to change syntax.
Updated by zverok (Victor Shepelev) 7 months ago
Agree with @Eregon (Benoit Daloze). The code should be more or less equivalent to:
for temp in [1, 2, 3]
foo&.bar = temp
end
...which is totally valid and foo&.bar =
is a no-op (semantically).
Updated by kddnewton (Kevin Newton) 7 months ago
@nobu (Nobuyoshi Nakada) it's the same possibilities in for
, rescue
, and multi-write. As in:
for (foo, @foo, @@foo, $foo, Foo, Foo::Foo, foo.foo, foo[foo]) in bar; end
begin; rescue => foo; end
begin; rescue => @foo; end
begin; rescue => @@foo; end
begin; rescue => $foo; end
begin; rescue => Foo; end
begin; rescue => Foo::Foo; end
begin; rescue => foo.foo; end
begin; rescue => foo[foo]; end
(foo, @foo, @@foo, $foo, Foo, Foo::Foo, foo.foo, foo[foo]) = bar
Updated by nobu (Nobuyoshi Nakada) 6 months ago
- Status changed from Open to Closed
Applied in changeset git|2dd46bb82ffc4dff01d7ea70922f0e407acafb4e.
[Bug #20468] Fix safe navigation in for
variable
Updated by nobu (Nobuyoshi Nakada) 6 months ago
- Has duplicate Bug #20561: Segfault with attr_writer, safe navigation and an "in" loop added
Updated by Eregon (Benoit Daloze) 5 months ago
- Backport changed from 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN to 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED
Updated by k0kubun (Takashi Kokubun) 5 months ago
- Backport changed from 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED to 3.1: REQUIRED, 3.2: REQUIRED, 3.3: DONE
ruby_3_3 8a2e41d34b135046957e1195a5d4f4967a82a965 merged revision(s) 2dd46bb82ffc4dff01d7ea70922f0e407acafb4e.
Updated by nagachika (Tomoyuki Chikanaga) 4 months ago
- Backport changed from 3.1: REQUIRED, 3.2: REQUIRED, 3.3: DONE to 3.1: REQUIRED, 3.2: DONE, 3.3: DONE
ruby_3_2 c10c73fd16f1b7c9b658afee2b1b53ecfaed4fa4 merged revision(s) 2dd46bb82ffc4dff01d7ea70922f0e407acafb4e.