Security Advisories (24)
CVE-2011-2728 (2012-12-21)

The bsd_glob function in the File::Glob module for Perl before 5.14.2 allows context-dependent attackers to cause a denial of service (crash) via a glob expression with the GLOB_ALTDIRFUNC flag, which triggers an uninitialized pointer dereference.

CVE-2020-12723 (2020-06-05)

regcomp.c in Perl before 5.30.3 allows a buffer overflow via a crafted regular expression because of recursive S_study_chunk calls.

CVE-2020-10878 (2020-06-05)

Perl before 5.30.3 has an integer overflow related to mishandling of a "PL_regkind[OP(n)] == NOTHING" situation. A crafted regular expression could lead to malformed bytecode with a possibility of instruction injection.

CVE-2020-10543 (2020-06-05)

Perl before 5.30.3 on 32-bit platforms allows a heap-based buffer overflow because nested regular expression quantifiers have an integer overflow.

CVE-2018-6913 (2018-04-17)

Heap-based buffer overflow in the pack function in Perl before 5.26.2 allows context-dependent attackers to execute arbitrary code via a large item count.

CVE-2018-18314 (2018-12-07)

Perl before 5.26.3 has a buffer overflow via a crafted regular expression that triggers invalid write operations.

CVE-2018-18313 (2018-12-07)

Perl before 5.26.3 has a buffer over-read via a crafted regular expression that triggers disclosure of sensitive information from process memory.

CVE-2018-18312 (2018-12-05)

Perl before 5.26.3 and 5.28.0 before 5.28.1 has a buffer overflow via a crafted regular expression that triggers invalid write operations.

CVE-2018-18311 (2018-12-07)

Perl before 5.26.3 and 5.28.x before 5.28.1 has a buffer overflow via a crafted regular expression that triggers invalid write operations.

CVE-2015-8853 (2016-05-25)

The (1) S_reghop3, (2) S_reghop4, and (3) S_reghopmaybe3 functions in regexec.c in Perl before 5.24.0 allow context-dependent attackers to cause a denial of service (infinite loop) via crafted utf-8 data, as demonstrated by "a\x80."

CVE-2013-1667 (2013-03-14)

The rehash mechanism in Perl 5.8.2 through 5.16.x allows context-dependent attackers to cause a denial of service (memory consumption and crash) via a crafted hash key.

CVE-2010-4777 (2014-02-10)

The Perl_reg_numbered_buff_fetch function in Perl 5.10.0, 5.12.0, 5.14.0, and other versions, when running with debugging enabled, allows context-dependent attackers to cause a denial of service (assertion failure and application exit) via crafted input that is not properly handled when using certain regular expressions, as demonstrated by causing SpamAssassin and OCSInventory to crash.

CVE-2010-1158 (2010-04-20)

Integer overflow in the regular expression engine in Perl 5.8.x allows context-dependent attackers to cause a denial of service (stack consumption and application crash) by matching a crafted regular expression against a long string.

CVE-2009-3626 (2009-10-29)

Perl 5.10.1 allows context-dependent attackers to cause a denial of service (application crash) via a UTF-8 character with a large, invalid codepoint, which is not properly handled during a regular-expression match.

CVE-2005-3962 (2005-12-01)

Integer overflow in the format string functionality (Perl_sv_vcatpvfn) in Perl 5.9.2 and 5.8.6 Perl allows attackers to overwrite arbitrary memory and possibly execute arbitrary code via format string specifiers with large values, which causes an integer wrap and leads to a buffer overflow, as demonstrated using format string vulnerabilities in Perl applications.

CVE-2012-5195 (2012-12-18)

Heap-based buffer overflow in the Perl_repeatcpy function in util.c in Perl 5.12.x before 5.12.5, 5.14.x before 5.14.3, and 5.15.x before 15.15.5 allows context-dependent attackers to cause a denial of service (memory consumption and crash) or possibly execute arbitrary code via the 'x' string repeat operator.

CVE-2016-2381 (2016-04-08)

Perl might allow context-dependent attackers to bypass the taint protection mechanism in a child process via duplicate environment variables in envp.

CVE-2013-7422 (2015-08-16)

Integer underflow in regcomp.c in Perl before 5.20, as used in Apple OS X before 10.10.5 and other products, allows context-dependent attackers to execute arbitrary code or cause a denial of service (application crash) via a long digit string associated with an invalid backreference within a regular expression.

CVE-2011-1487 (2011-04-11)

The (1) lc, (2) lcfirst, (3) uc, and (4) ucfirst functions in Perl 5.10.x, 5.11.x, and 5.12.x through 5.12.3, and 5.13.x through 5.13.11, do not apply the taint attribute to the return value upon processing tainted input, which might allow context-dependent attackers to bypass the taint protection mechanism via a crafted string.

CVE-2023-47100

In Perl before 5.38.2, S_parse_uniprop_string in regcomp.c can write to unallocated space because a property name associated with a \p{...} regular expression construct is mishandled. The earliest affected version is 5.30.0.

CVE-2024-56406 (2025-04-13)

A heap buffer overflow vulnerability was discovered in Perl. When there are non-ASCII bytes in the left-hand-side of the `tr` operator, `S_do_trans_invmap` can overflow the destination pointer `d`.    $ perl -e '$_ = "\x{FF}" x 1000000; tr/\xFF/\x{100}/;'    Segmentation fault (core dumped) It is believed that this vulnerability can enable Denial of Service and possibly Code Execution attacks on platforms that lack sufficient defenses.

CVE-2023-47039 (2023-10-30)

Perl for Windows relies on the system path environment variable to find the shell (cmd.exe). When running an executable which uses Windows Perl interpreter, Perl attempts to find and execute cmd.exe within the operating system. However, due to path search order issues, Perl initially looks for cmd.exe in the current working directory. An attacker with limited privileges can exploit this behavior by placing cmd.exe in locations with weak permissions, such as C:\ProgramData. By doing so, when an administrator attempts to use this executable from these compromised locations, arbitrary code can be executed.

CVE-2016-1238 (2016-08-02)

(1) cpan/Archive-Tar/bin/ptar, (2) cpan/Archive-Tar/bin/ptardiff, (3) cpan/Archive-Tar/bin/ptargrep, (4) cpan/CPAN/scripts/cpan, (5) cpan/Digest-SHA/shasum, (6) cpan/Encode/bin/enc2xs, (7) cpan/Encode/bin/encguess, (8) cpan/Encode/bin/piconv, (9) cpan/Encode/bin/ucmlint, (10) cpan/Encode/bin/unidump, (11) cpan/ExtUtils-MakeMaker/bin/instmodsh, (12) cpan/IO-Compress/bin/zipdetails, (13) cpan/JSON-PP/bin/json_pp, (14) cpan/Test-Harness/bin/prove, (15) dist/ExtUtils-ParseXS/lib/ExtUtils/xsubpp, (16) dist/Module-CoreList/corelist, (17) ext/Pod-Html/bin/pod2html, (18) utils/c2ph.PL, (19) utils/h2ph.PL, (20) utils/h2xs.PL, (21) utils/libnetcfg.PL, (22) utils/perlbug.PL, (23) utils/perldoc.PL, (24) utils/perlivp.PL, and (25) utils/splain.PL in Perl 5.x before 5.22.3-RC2 and 5.24 before 5.24.1-RC2 do not properly remove . (period) characters from the end of the includes directory array, which might allow local users to gain privileges via a Trojan horse module under the current working directory.

CVE-2015-8608 (2017-02-07)

The VDir::MapPathA and VDir::MapPathW functions in Perl 5.22 allow remote attackers to cause a denial of service (out-of-bounds read) and possibly execute arbitrary code via a crafted (1) drive letter or (2) pInName argument.

NAME

Test::Builder - Backend for building test libraries

SYNOPSIS

package My::Test::Module;
use Test::Builder;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(ok);

my $Test = Test::Builder->new;
$Test->output('my_logfile');

sub import {
    my($self) = shift;
    my $pack = caller;

    $Test->exported_to($pack);
    $Test->plan(@_);

    $self->export_to_level(1, $self, 'ok');
}

sub ok {
    my($test, $name) = @_;

    $Test->ok($test, $name);
}

DESCRIPTION

Test::Simple and Test::More have proven to be popular testing modules, but they're not always flexible enough. Test::Builder provides the a building block upon which to write your own test libraries which can work together.

Construction

new
my $Test = Test::Builder->new;

Returns a Test::Builder object representing the current state of the test.

Since you only run one test per program new always returns the same Test::Builder object. No matter how many times you call new(), you're getting the same object. This is called a singleton. This is done so that multiple modules share such global information as the test counter and where test output is going.

If you want a completely new Test::Builder object different from the singleton, use create.

create
my $Test = Test::Builder->create;

Ok, so there can be more than one Test::Builder object and this is how you get it. You might use this instead of new() if you're testing a Test::Builder based module, but otherwise you probably want new.

NOTE: the implementation is not complete. level, for example, is still shared amongst all Test::Builder objects, even ones created using this method. Also, the method name may change in the future.

reset
$Test->reset;

Reinitializes the Test::Builder singleton to its original state. Mostly useful for tests run in persistent environments where the same test might be run multiple times in the same process.

Setting up tests

These methods are for setting up tests and declaring how many there are. You usually only want to call one of these methods.

exported_to
my $pack = $Test->exported_to;
$Test->exported_to($pack);

Tells Test::Builder what package you exported your functions to. This is important for getting TODO tests right.

plan
$Test->plan('no_plan');
$Test->plan( skip_all => $reason );
$Test->plan( tests => $num_tests );

A convenient way to set up your tests. Call this and Test::Builder will print the appropriate headers and take the appropriate actions.

If you call plan(), don't call any of the other methods below.

expected_tests
my $max = $Test->expected_tests;
$Test->expected_tests($max);

Gets/sets the # of tests we expect this test to run and prints out the appropriate headers.

no_plan
$Test->no_plan;

Declares that this test will run an indeterminate # of tests.

has_plan
$plan = $Test->has_plan

Find out whether a plan has been defined. $plan is either undef (no plan has been set), no_plan (indeterminate # of tests) or an integer (the number of expected tests).

skip_all
$Test->skip_all;
$Test->skip_all($reason);

Skips all the tests, using the given $reason. Exits immediately with 0.

Running tests

These actually run the tests, analogous to the functions in Test::More.

$name is always optional.

ok
$Test->ok($test, $name);

Your basic test. Pass if $test is true, fail if $test is false. Just like Test::Simple's ok().

is_eq
$Test->is_eq($got, $expected, $name);

Like Test::More's is(). Checks if $got eq $expected. This is the string version.

is_num
$Test->is_num($got, $expected, $name);

Like Test::More's is(). Checks if $got == $expected. This is the numeric version.

isnt_eq
$Test->isnt_eq($got, $dont_expect, $name);

Like Test::More's isnt(). Checks if $got ne $dont_expect. This is the string version.

isnt_num
$Test->is_num($got, $dont_expect, $name);

Like Test::More's isnt(). Checks if $got ne $dont_expect. This is the numeric version.

like
$Test->like($this, qr/$regex/, $name);
$Test->like($this, '/$regex/', $name);

Like Test::More's like(). Checks if $this matches the given $regex.

You'll want to avoid qr// if you want your tests to work before 5.005.

unlike
$Test->unlike($this, qr/$regex/, $name);
$Test->unlike($this, '/$regex/', $name);

Like Test::More's unlike(). Checks if $this does not match the given $regex.

maybe_regex
$Test->maybe_regex(qr/$regex/);
$Test->maybe_regex('/$regex/');

Convenience method for building testing functions that take regular expressions as arguments, but need to work before perl 5.005.

Takes a quoted regular expression produced by qr//, or a string representing a regular expression.

Returns a Perl value which may be used instead of the corresponding regular expression, or undef if it's argument is not recognised.

For example, a version of like(), sans the useful diagnostic messages, could be written as:

sub laconic_like {
    my ($self, $this, $regex, $name) = @_;
    my $usable_regex = $self->maybe_regex($regex);
    die "expecting regex, found '$regex'\n"
        unless $usable_regex;
    $self->ok($this =~ m/$usable_regex/, $name);
}
cmp_ok
$Test->cmp_ok($this, $type, $that, $name);

Works just like Test::More's cmp_ok().

$Test->cmp_ok($big_num, '!=', $other_big_num);
BAIL_OUT
$Test->BAIL_OUT($reason);

Indicates to the Test::Harness that things are going so badly all testing should terminate. This includes running any additional test scripts.

It will exit with 255.

skip
$Test->skip;
$Test->skip($why);

Skips the current test, reporting $why.

todo_skip
$Test->todo_skip;
$Test->todo_skip($why);

Like skip(), only it will declare the test as failing and TODO. Similar to

print "not ok $tnum # TODO $why\n";

Test style

level
$Test->level($how_high);

How far up the call stack should $Test look when reporting where the test failed.

Defaults to 1.

Setting $Test::Builder::Level overrides. This is typically useful localized:

{
    local $Test::Builder::Level = 2;
    $Test->ok($test);
}
use_numbers
$Test->use_numbers($on_or_off);

Whether or not the test should output numbers. That is, this if true:

ok 1
ok 2
ok 3

or this if false

ok
ok
ok

Most useful when you can't depend on the test output order, such as when threads or forking is involved.

Test::Harness will accept either, but avoid mixing the two styles.

Defaults to on.

no_diag
$Test->no_diag($no_diag);

If set true no diagnostics will be printed. This includes calls to diag().

no_ending
$Test->no_ending($no_ending);

Normally, Test::Builder does some extra diagnostics when the test ends. It also changes the exit code as described below.

If this is true, none of that will be done.

no_header
$Test->no_header($no_header);

If set to true, no "1..N" header will be printed.

Output

Controlling where the test output goes.

It's ok for your test to change where STDOUT and STDERR point to, Test::Builder's default output settings will not be affected.

diag
$Test->diag(@msgs);

Prints out the given @msgs. Like print, arguments are simply appended together.

Normally, it uses the failure_output() handle, but if this is for a TODO test, the todo_output() handle is used.

Output will be indented and marked with a # so as not to interfere with test output. A newline will be put on the end if there isn't one already.

We encourage using this rather than calling print directly.

Returns false. Why? Because diag() is often used in conjunction with a failing test (ok() || diag()) it "passes through" the failure.

return ok(...) || diag(...);
_print_diag
$Test->_print_diag(@msg);

Like _print, but prints to the current diagnostic filehandle.

output
$Test->output($fh);
$Test->output($file);

Where normal "ok/not ok" test output should go.

Defaults to STDOUT.

failure_output
$Test->failure_output($fh);
$Test->failure_output($file);

Where diagnostic output on test failures and diag() should go.

Defaults to STDERR.

todo_output
$Test->todo_output($fh);
$Test->todo_output($file);

Where diagnostics about todo test failures and diag() should go.

Defaults to STDOUT.

Test Status and Info

current_test
my $curr_test = $Test->current_test;
$Test->current_test($num);

Gets/sets the current test number we're on. You usually shouldn't have to set this.

If set forward, the details of the missing tests are filled in as 'unknown'. if set backward, the details of the intervening tests are deleted. You can erase history if you really want to.

summary
my @tests = $Test->summary;

A simple summary of the tests so far. True for pass, false for fail. This is a logical pass/fail, so todos are passes.

Of course, test #1 is $tests[0], etc...

details
my @tests = $Test->details;

Like summary(), but with a lot more detail.

$tests[$test_num - 1] = 
        { 'ok'       => is the test considered a pass?
          actual_ok  => did it literally say 'ok'?
          name       => name of the test (if any)
          type       => type of test (if any, see below).
          reason     => reason for the above (if any)
        };

'ok' is true if Test::Harness will consider the test to be a pass.

'actual_ok' is a reflection of whether or not the test literally printed 'ok' or 'not ok'. This is for examining the result of 'todo' tests.

'name' is the name of the test.

'type' indicates if it was a special test. Normal tests have a type of ''. Type can be one of the following:

skip        see skip()
todo        see todo()
todo_skip   see todo_skip()
unknown     see below

Sometimes the Test::Builder test counter is incremented without it printing any test output, for example, when current_test() is changed. In these cases, Test::Builder doesn't know the result of the test, so it's type is 'unkown'. These details for these tests are filled in. They are considered ok, but the name and actual_ok is left undef.

For example "not ok 23 - hole count # TODO insufficient donuts" would result in this structure:

$tests[22] =    # 23 - 1, since arrays start from 0.
  { ok        => 1,   # logically, the test passed since it's todo
    actual_ok => 0,   # in absolute terms, it failed
    name      => 'hole count',
    type      => 'todo',
    reason    => 'insufficient donuts'
  };
todo
my $todo_reason = $Test->todo;
my $todo_reason = $Test->todo($pack);

todo() looks for a $TODO variable in your tests. If set, all tests will be considered 'todo' (see Test::More and Test::Harness for details). Returns the reason (ie. the value of $TODO) if running as todo tests, false otherwise.

todo() is about finding the right package to look for $TODO in. It uses the exported_to() package to find it. If that's not set, it's pretty good at guessing the right package to look at based on $Level.

Sometimes there is some confusion about where todo() should be looking for the $TODO variable. If you want to be sure, tell it explicitly what $pack to use.

caller
my $package = $Test->caller;
my($pack, $file, $line) = $Test->caller;
my($pack, $file, $line) = $Test->caller($height);

Like the normal caller(), except it reports according to your level().

EXIT CODES

If all your tests passed, Test::Builder will exit with zero (which is normal). If anything failed it will exit with how many failed. If you run less (or more) tests than you planned, the missing (or extras) will be considered failures. If no tests were ever run Test::Builder will throw a warning and exit with 255. If the test died, even after having successfully completed all its tests, it will still be considered a failure and will exit with 255.

So the exit codes are...

0                   all tests successful
255                 test died or all passed but wrong # of tests run
any other number    how many failed (including missing or extras)

If you fail more than 254 tests, it will be reported as 254.

THREADS

In perl 5.8.0 and later, Test::Builder is thread-safe. The test number is shared amongst all threads. This means if one thread sets the test number using current_test() they will all be effected.

Test::Builder is only thread-aware if threads.pm is loaded before Test::Builder.

EXAMPLES

CPAN can provide the best examples. Test::Simple, Test::More, Test::Exception and Test::Differences all use Test::Builder.

SEE ALSO

Test::Simple, Test::More, Test::Harness

AUTHORS

Original code by chromatic, maintained by Michael G Schwern <schwern@pobox.com>

COPYRIGHT

Copyright 2002, 2004 by chromatic <chromatic@wgz.org> and Michael G Schwern <schwern@pobox.com>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html