From a9f251bdb5484ada8519bc9471c23d29fd543f00 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Fri, 7 May 2021 10:33:52 +0200 Subject: [PATCH] harden(core/options): use package Pod::Simple::Search than legacy Pod::Find (#2775) --- centreon-plugins/centreon/plugins/options.pm | 42 +++++++++++++++++--- centreon-plugins/centreon/plugins/script.pm | 11 ++--- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/centreon-plugins/centreon/plugins/options.pm b/centreon-plugins/centreon/plugins/options.pm index ea6af4787..36176dce8 100644 --- a/centreon-plugins/centreon/plugins/options.pm +++ b/centreon-plugins/centreon/plugins/options.pm @@ -21,7 +21,6 @@ package centreon::plugins::options; use Pod::Usage; -use Pod::Find qw(pod_where); use strict; use warnings; @@ -32,6 +31,7 @@ sub new { my $self = {}; bless $self, $class; + $self->{pod_where_loaded} = 0; $self->{sanity} = 0; $self->{options_stored} = {}; $self->{options} = {}; @@ -76,14 +76,16 @@ sub display_help { my $stdout; foreach (@{$self->{pod_package}}) { - + my $where = $self->pod_where(package => $_->{package}); + { local *STDOUT; open STDOUT, '>', \$stdout; - my $where = pod_where({-inc => 1}, $_->{package}); - pod2usage(-exitval => 'NOEXIT', -input => $where, - -verbose => 99, - -sections => $_->{sections}) if (defined($where)); + pod2usage( + -exitval => 'NOEXIT', -input => $where, + -verbose => 99, + -sections => $_->{sections} + ) if (defined($where)); } $self->{output}->add_option_msg(long_msg => $stdout) if (defined($stdout)); @@ -151,6 +153,34 @@ sub parse_options { $SIG{__WARN__} = $save_warn_handler if ($self->{sanity} == 1); } +sub pod_where { + my ($self, %options) = @_; + + if ($self->{pod_where_loaded} == 0) { + $self->{pod_where_loaded} = 1; + my ($code) = centreon::plugins::misc::mymodule_load( + module => 'Pod::Find', + no_quit => 1 + ); + if ($code) { + $code = centreon::plugins::misc::mymodule_load( + module => 'Pod::Simple::Search', + no_quit => 1 + ); + die "Cannot load module 'Pod::Simple::Search'" if ($code); + $self->{pod_where_loaded} = 2; + $self->{pod_simple_search} = Pod::Simple::Search->new(); + $self->{pod_simple_search}->inc(1); + } + } + + if ($self->{pod_where_loaded} == 1) { + return Pod::Find::pod_where({-inc => 1}, $options{package}); + } + + return $self->{pod_simple_search}->find($options{package}); +} + sub get_option { my ($self, %options) = @_; diff --git a/centreon-plugins/centreon/plugins/script.pm b/centreon-plugins/centreon/plugins/script.pm index 2e5e2290f..3992744b5 100644 --- a/centreon-plugins/centreon/plugins/script.pm +++ b/centreon-plugins/centreon/plugins/script.pm @@ -25,7 +25,6 @@ use warnings; use centreon::plugins::output; use centreon::plugins::misc; use Pod::Usage; -use Pod::Find qw(pod_where); my %handlers = (DIE => {}, ALRM => {}); @@ -162,9 +161,9 @@ sub display_local_help { if ($self->{help}) { local *STDOUT; open STDOUT, '>', \$stdout; - + if ($alternative_fatpacker == 0) { - pod2usage(-exitval => 'NOEXIT', -input => pod_where({-inc => 1}, __PACKAGE__)); + pod2usage(-exitval => 'NOEXIT', -input => $self->{options}->pod_where(package => __PACKAGE__)); } else { my $pp = __PACKAGE__ . '.pm'; $pp =~ s{::}{/}g; @@ -418,8 +417,10 @@ sub run { $self->check_relaunch(); (undef, $self->{plugin}) = - centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $self->{plugin}, - error_msg => 'Cannot load module --plugin.'); + centreon::plugins::misc::mymodule_load( + output => $self->{output}, module => $self->{plugin}, + error_msg => 'Cannot load module --plugin.' + ); my $plugin = $self->{plugin}->new(options => $self->{options}, output => $self->{output}); $plugin->init( help => $self->{help},