From 862e4599a8cf853d575ab60a3f0744d76580f7a8 Mon Sep 17 00:00:00 2001 From: Yves Orton Date: Mon, 13 Mar 2023 12:35:15 +0100 Subject: [PATCH] Test2::API - harden T2_FORMATTER env access. There were two problems. The first is accessing the %ENV hash when it might locked. Using Test2::Util::_env_get() solves that problem. The second issue is that the patterns might match "+" with no extra string, leaving the formatter as the empty string. The third problem is the defaulting for the formatter could end up not being triggered and the formatter being left as the empty string. This fixes all three issues. I noticed the second two when I fixing the first and did it wrong accidentally. --- lib/Test2/API.pm | 5 +++-- lib/Test2/API/Instance.pm | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/Test2/API.pm b/lib/Test2/API.pm index 3f7b79b79..68d5dc676 100644 --- a/lib/Test2/API.pm +++ b/lib/Test2/API.pm @@ -3,7 +3,7 @@ use strict; use warnings; use Time::HiRes qw/time/; -use Test2::Util qw/USE_THREADS/; +use Test2::Util qw/USE_THREADS _env_get/; BEGIN { $ENV{TEST_ACTIVE} ||= 1; @@ -287,7 +287,8 @@ sub test2_ipc_get_timeout { $INST->ipc_timeout() } sub test2_ipc_enable_shm { 0 } sub test2_formatter { - if ($ENV{T2_FORMATTER} && $ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) { + my $env_var = _env_get('T2_FORMATTER', ''); + if ($env_var =~ m/^(\+)?(.+)$/) { my $formatter = $1 ? $2 : "Test2::Formatter::$2"; my $file = pkg_to_file($formatter); require $file; diff --git a/lib/Test2/API/Instance.pm b/lib/Test2/API/Instance.pm index 39924fb3a..678366620 100644 --- a/lib/Test2/API/Instance.pm +++ b/lib/Test2/API/Instance.pm @@ -163,10 +163,10 @@ sub _finalize { unless ($self->{+FORMATTER}) { my ($formatter, $source); - if ($ENV{T2_FORMATTER}) { + if (my $env_val = _env_get('T2_FORMATTER')) { $source = "set by the 'T2_FORMATTER' environment variable"; - if ($ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) { + if ($env_val =~ m/^(\+)?(.+)$/) { $formatter = $1 ? $2 : "Test2::Formatter::$2" } else { @@ -177,7 +177,8 @@ sub _finalize { ($formatter) = @{$self->{+FORMATTERS}}; $source = "Most recently added"; } - else { + + if (!$formatter) { $formatter = 'Test2::Formatter::TAP'; $source = 'default formatter'; }