rc icon indicating copy to clipboard operation
rc copied to clipboard

Uninsulated subshell when you return

Open Nomarian opened this issue 3 years ago • 3 comments

fn isdir { @{ cd $^*;return $status } }
isdir /

will set directory to / instead of being insulated from the command

Not only that, but when you return, it leaves you inside the subshell.

Nomarian avatar Nov 30 '20 01:11 Nomarian

Crazy edge case. I will look at this when I get a chance.

rakitzis avatar Nov 30 '20 01:11 rakitzis

It seems that this would not happen without the return statement inside fn/@ -- is that correct?

borkovic avatar Dec 29 '20 19:12 borkovic

Well, yeah, I've never used 9rc seriously but since it doesn't have return, you have to use exit. this makes you write the code correctly

fn isdir { @{ cd $^* >[2] /dev/null ;exit $status } }

Funnily enough, I think this might be a useful feature. create a subshell in a function and control it with other functions. its kind of dangerous but, might be nice.

Edit: Nope, scratch that, inconsistent behavior, suppose you have the script:

fn subshell {
 @{ return 0 }
}

echo hello
subshell
echo good
echo bye

and you run it

; rc subshells.brc 
hello
good
rc: read: Bad file descriptor
good
bye

but with cat

; cat subshells.brc | rc
hello
good
bye
good
bye

Nomarian avatar Dec 30 '20 17:12 Nomarian

fn foo {
    echo xxx
    return >/dev/null
    echo yyy
}
foo
xxx
^D
yyy

This leaves you in the subshell created for the redirection. Hitting Ctrl-D exits it and the parent shell continues executing the function.

xyb3rt avatar Jun 12 '23 10:06 xyb3rt

This is an unintended side-effect of the strategy described in #62 . Best to keep this in mind while designing a fix.

rakitzis avatar Jun 12 '23 13:06 rakitzis

A possible fix for the original issue is to be more strict about where we'll allow break, continue and return. They already result in an error when outside a function. Can we also consider subshells in a function definition to be outside of the function?

xyb3rt avatar Jun 13 '23 07:06 xyb3rt