Project

General

Profile

Actions

Bug #20468

closed

Segfault on safe navigation in for target

Added by kddnewton (Kevin Newton) 7 months ago. Updated 4 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:117763]

Description

for foo&.bar in []; end

Related issues 1 (0 open1 closed)

Has duplicate Ruby master - Bug #20561: Segfault with attr_writer, safe navigation and an "in" loopClosedActions

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.

https://github.com/ruby/ruby/pull/10723

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
Actions #9

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

Actions #10

Updated by nobu (Nobuyoshi Nakada) 6 months ago

  • Has duplicate Bug #20561: Segfault with attr_writer, safe navigation and an "in" loop added
Actions #11

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) 4 months ago

  • Backport changed from 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED to 3.1: REQUIRED, 3.2: REQUIRED, 3.3: DONE

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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0