From: Zefram Date: Sun, 11 Sep 2011 18:17:14 +0000 (+0100) Subject: initialize earlier to help with string evals X-Git-Tag: 0.006007~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8ec78a85abda5a1bab263a1b1b49b979a3539c29;p=p5sagit%2FDevel-Declare.git initialize earlier to help with string evals --- diff --git a/Changes b/Changes index 98e6e9f..cc32696 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,9 @@ Changes for Devel-Declare - Depend on B::Hooks::OP::Check version 0.19, which fixes a serious bug in how it interacts with other modules that hook ops. + - Initialize immediately upon loading the module, so that "was Devel::Declare + loaded soon enough" errors in string eval can be fixed by loading the + module earlier without having to also actually use the module earlier. - Adjust toke_scan_str logic to always show a positive effective length of string source. - Return undef from toke_scan_str if string was unterminated. diff --git a/Declare.xs b/Declare.xs index 65902ff..63ee08b 100644 --- a/Declare.xs +++ b/Declare.xs @@ -485,16 +485,10 @@ STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) { #endif /* !DD_CONST_VIA_RV2CV */ -static int initialized = 0; - -MODULE = Devel::Declare PACKAGE = Devel::Declare - -PROTOTYPES: DISABLE - -void -setup() - CODE: - if (!initialized++) { +STATIC void dd_initialize(pTHX) { + static int initialized = 0; + if (!initialized) { + initialized = 1; #if DD_GROW_VIA_BLOCKHOOK static BHK bhk; #if PERL_VERSION_GE(5,13,6) @@ -511,7 +505,22 @@ setup() hook_op_check(OP_CONST, dd_ck_const, NULL); #endif /* !DD_CONST_VIA_RV2CV */ } - filter_add(dd_filter_realloc, NULL); +} + +MODULE = Devel::Declare PACKAGE = Devel::Declare + +PROTOTYPES: DISABLE + +void +initialize() + CODE: + dd_initialize(aTHX); + +void +setup() + CODE: + dd_initialize(aTHX); + filter_add(dd_filter_realloc, NULL); char* get_linestr() diff --git a/lib/Devel/Declare.pm b/lib/Devel/Declare.pm index 3f3eb76..6b64ccc 100644 --- a/lib/Devel/Declare.pm +++ b/lib/Devel/Declare.pm @@ -20,6 +20,8 @@ bootstrap Devel::Declare; @ISA = (); +initialize(); + sub import { my ($class, %args) = @_; my $target = caller; diff --git a/t/early2.t b/t/early2.t new file mode 100644 index 0000000..750a6b5 --- /dev/null +++ b/t/early2.t @@ -0,0 +1,27 @@ +use strict; +use warnings; +use Test::More tests => 2; + +use Devel::Declare; +eval q[ + BEGIN { + *class = sub (&) { $_[0]->() }; + Devel::Declare->setup_for(__PACKAGE__, { + class => { + const => sub { + my ($kw, $off) = @_; + $off += Devel::Declare::toke_move_past_token($off); + $off += Devel::Declare::toke_skipspace($off); + die unless substr(Devel::Declare::get_linestr(), $off, 1) eq '{'; + my $l = Devel::Declare::get_linestr(); + substr $l, $off + 1, 0, 'pass q[injected];' . (';' x 1000); + Devel::Declare::set_linestr($l); + }, + }, + }); + } + class {}; +]; +is $@, ""; + +1;