-
Notifications
You must be signed in to change notification settings - Fork 571
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
Safe: wrap_code_ref hides all exceptions from the wrapped code #17161
Comments
From mathias@cpan.orgThis is a bug report for perl from mathias@cpan.org, Here is a small instroductory example: $ perl -MSafe -e 'Safe->new()->reval("sub { die 1 }")->(); print "Should Which prints: Should not be here () What we see is that code refs returned by Safe::reval (which have been Reading the code of wrap_code_ref I think that the intent was for the The fix is, inside wrap_code_ref, to wrap the call to $sub->(@args) However there are two possible ways to handle this: either keep the The second approach (setting $@ but not raising an exception), also has The proposed patch applies these fixes: Flags: Site configuration information for perl 5.30.0: Configured by Mathias at Sat Sep 28 12:50:24 CEST 2019. Summary of my perl5 (revision 5 version 30 subversion 0) configuration: Platform: @INC for perl 5.30.0: Environment for perl 5.30.0: Attachment(s): |
From mathias@cpan.orgsafe.patch--- Safe.pm 2019-09-28 13:02:38.305230800 +0200
+++ Safe2.pm 2019-09-28 13:13:01.406258300 +0200
@@ -3,7 +3,7 @@ package Safe;
use 5.003_11;
use Scalar::Util qw(reftype refaddr);
-$Safe::VERSION = "2.40";
+$Safe::VERSION = "2.41";
# *** Don't declare any lexicals above this point ***
#
@@ -423,23 +423,22 @@ sub wrap_code_ref {
my $ret = sub {
my @args = @_; # lexical to close over
- my $sub_with_args = sub { $sub->(@args) };
+ my $sub_with_args = sub { eval { $sub->(@args) } };
my @subret;
my $error;
do {
- local $@; # needed due to perl_call_sv(sv, G_EVAL|G_KEEPERR)
my $sg = sub_generation();
- @subret = (wantarray)
- ? Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args)
- : scalar Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args);
- $error = $@;
+ if (defined wantarray) {
+ @subret = (wantarray)
+ ? Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args)
+ : scalar Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args);
+ } else {
+ Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args);
+ }
_clean_stash($obj->{Root}.'::') if $sg != sub_generation();
};
- if ($error) { # rethrow exception
- $error =~ s/\t\(in cleanup\) //; # prefix added by G_KEEPERR
- die $error;
- }
+ $obj->wrap_code_refs_within(@subret);
return (wantarray) ? @subret : $subret[0];
};
@@ -750,7 +749,12 @@ package namespace adjusted and the opmas
Note that the opmask doesn't affect the already compiled code, it only affects
any I<further> compilation that the already compiled code may try to perform.
-This is particularly useful when applied to code references returned from reval().
+In addition, exception raised in the wrapped code will be trapped and $@ will be
+set instead. Any code reference returned by the wrapped code will itself be
+wrapped similarly.
+
+This is particularly useful when applied to code references returned from
+reval() (which is done automatically).
(It also provides a kind of workaround for RT#60374: "Safe.pm sort {} bug with
-Dusethreads". See L<http://rt.perl.org/rt3//Public/Bug/Display.html?id=60374>
|
From @jkeenanOn Sat, 28 Sep 2019 11:36:36 GMT, mathias@cpan.org wrote:
The patch fails tests in Perl 5 blead. I created a local branch from perl 5 blead and applied your patch. I got the following test failures: ##### # Failed test 'successful closure call should not alter $@' # Failed test at t/safewrap.t line 27. # Failed test at t/safewrap.t line 39. Test Summary Report ../dist/Safe/t/safesort.t (Wstat: 256 Tests: 10 Failed: 1) Can you investigate further? Thank you very much. -- |
The RT System itself - Status changed from 'new' to 'open' |
Migrated from rt.perl.org#134459 (status was 'open')
Searchable as RT134459$
The text was updated successfully, but these errors were encountered: