Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short script 'misusing' an iterator crashes Rakudo #1340

Closed
dogbert17 opened this issue Dec 25, 2017 · 3 comments · May be fixed by Raku/nqp#731
Closed

Short script 'misusing' an iterator crashes Rakudo #1340

dogbert17 opened this issue Dec 25, 2017 · 3 comments · May be fixed by Raku/nqp#731
Labels
tests needed Issue is generally resolved but tests were not written yet

Comments

@dogbert17
Copy link

The Problem

The following snippet causes Rakudo to SEGV:

my SetHash $a = ('0/2', '2/2').SetHash; 
my @result = (); 
find-strongest($a); 
sub find-strongest(SetHash $a) { 
  my $keys = $a.keys; 
  for $keys.List -> $item { 
    my @split-item = $item.comb; 
    $a{$item}:delete; 
    find-strongest($a); 
  }
}

Expected Behavior

Some kind of nice error message :-)

Actual Behavior

SEGV

Steps to Reproduce

See code snippet above

Environment

Camelia on irc, see https://irclog.perlgeek.de/perl6-dev/2017-12-24#i_15619001 for more information.

@zoffixznet
Copy link
Contributor

zoffixznet commented Jan 1, 2018

Golfed to:

<Zoffix_> m: use nqp; my $h := nqp::hash("a", 42); my $i := nqp::iterator($h); nqp::deletekey($h, "a"); nqp::shift($i)
<camelia> rakudo-moar bf6451dac: OUTPUT: «(signal SEGV)»

The SEGV looks to be inside 3rdparty/uthash.h. I poked at it a bit, but didn't manage to fix.

After SEGV is fixed, we'll get could not find candidate in postfix:<{ }> because .pull-one from .keys would return a null. That IMO falls under DIHWIDT as the user is trying to iterate over the .keys Seq while reducing the number of available keys.

To workaround the DIHWIDT, simply reify the .keys Seq right away, by shoving keys into an Array or stick an .eager on it:

my SetHash $a = ('0/2', '2/2').SetHash;
my @result = ();
find-strongest($a);
sub find-strongest(SetHash $a) {
  for $a.keys.eager -> $item {
    my @split-item = $item.comb;
    $a{$item}:delete;
    find-strongest($a);
  }
}

@dogbert17
Copy link
Author

Seems to have been fixed by commit 8fd029a

@Altai-man Altai-man added the tests needed Issue is generally resolved but tests were not written yet label Oct 22, 2020
@ab5tract
Copy link
Collaborator

No longer an issue:

> set-base; r 'use nqp; my $h := nqp::hash("a", 42); my $i := nqp::iterator($h); nqp::deletekey($h, "a"); nqp::shift($i)'

> set-rakuast; > r 'use nqp; my $h := nqp::hash("a", 42); my $i := nqp::iterator($h); nqp::deletekey($h, "a"); nqp::shift($i)'

> ./rakudo-m -v
Welcome to Rakudo™ v2024.04-92-g24057e69c.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2024.04-6-g84121f0de.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests needed Issue is generally resolved but tests were not written yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants